프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 04fcadf1

이력 | 보기 | 이력해설 | 다운로드 (189 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
                GC.Collect();
161
            }
162
        }
163
        private void RunEquipmentModeling()
164
        {
165
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Equipments.Count);
166
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
167
            foreach (Equipment item in document.Equipments)
168
            {
169
                try
170
                {
171
                    EquipmentModeling(item);
172
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.Equipments.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
173
                }
174
                catch (Exception ex)
175
                {
176
                    Log.Write("Error in EquipmentModeling");
177
                    Log.Write("UID : " + item.UID);
178
                    Log.Write(ex.Message);
179
                    Log.Write(ex.StackTrace);
180
                }
181
            }
182
        }
183
        private void RunSymbolModeling()
184
        {
185
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
186
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
187
            prioritySymbols = GetPrioritySymbol();
188
            foreach (var item in prioritySymbols)
189
            {
190
                try
191
                {
192
                    SymbolModelingBySymbol(item);
193
                }
194
                catch (Exception ex)
195
                {
196
                    Log.Write("Error in SymbolModelingByPriority");
197
                    Log.Write("UID : " + item.UID);
198
                    Log.Write(ex.Message);
199
                    Log.Write(ex.StackTrace);
200
                }
201
            }
202
        }
203
        private void RunLineModeling()
204
        {
205
            List<Line> AllLine = document.LINES.ToList();
206
            List<Line> stepLast_Line = document.LINES.FindAll(x => x.CONNECTORS.FindAll(y => y.ConnectedObject != null && y.ConnectedObject.GetType() == typeof(Symbol)).Count == 2 &&
207
            !SPPIDUtil.IsBranchedLine(document, x));
208
            List<Line> step1_Line = AllLine.FindAll(x => !stepLast_Line.Contains(x));
209

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

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

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

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

    
285
                LMRelationships relationships = new LMRelationships();
286
                relationships.Collect(dataSource, Filter: filter);
287

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

    
487
                        SetFlowDirectionByLine(line.SPPID.ModelItemId);
488

    
489
                        ReleaseCOMObjects(modelItem);
490
                    }
491
                }
492

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

    
505
                SetFlowDirectionByLine(modelId);
506

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

    
520
                SetFlowDirectionByLine(modelId);
521

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

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

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

    
551
                                ReleaseCOMObjects(connector);
552
                            }
553
                        }
554
                    }
555
                    ReleaseCOMObjects(modelItem);
556
                }
557

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

    
568
                        SetSymbolFlowDirection(lineIndex, symbolIndex, sFlowDirection, symbolModelItem);
569

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

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

    
583
                        symbolModelItem.Commit();
584
                        ReleaseCOMObjects(symbolModelItem);
585
                    }
586
                }
587

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

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

    
743
            LMDrawings drawings = new LMDrawings();
744
            drawings.Collect(dataSource, Filter: filter);
745

    
746
            currentDrawing = ((dynamic)drawings).Nth(1);
747
        }
748

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

    
759
            List<string> drawingNameList = new List<string>();
760
            List<string> drawingNumberList = new List<string>();
761

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

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

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

    
794
            ReleaseCOMObjects(drawings);
795
        }
796

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

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

    
834
            LMSymbol _LMSymbol = null;
835

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

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

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

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

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

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

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

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

    
879
            if (_LMSymbol != null)
880
            {
881
                _LMSymbol.Commit();
882

    
883
                // ConnCheck
884
                List<string> ids = new List<string>();
885
                foreach (LMConnector item in _LMSymbol.Connect1Connectors)
886
                {
887
                    if (item.get_ItemStatus() == "Active" && !ids.Contains(item.Id))
888
                        ids.Add(item.Id);
889
                    ReleaseCOMObjects(item);
890
                }
891
                foreach (LMConnector item in _LMSymbol.Connect2Connectors)
892
                {
893
                    if (item.get_ItemStatus() == "Active" && !ids.Contains(item.Id))
894
                        ids.Add(item.Id);
895
                    ReleaseCOMObjects(item);
896
                }
897

    
898
                int createdSymbolCount = document.SYMBOLS.FindAll(i => i.CONNECTORS.Find(j => j.CONNECTEDITEM == symbol.UID) != null && !string.IsNullOrEmpty(i.SPPID.RepresentationId)).Count;
899
                if (targetSymbol == null && ids.Count != createdSymbolCount)
900
                {
901
                    double currentX = _LMSymbol.get_XCoordinate();
902
                    double currentY = _LMSymbol.get_YCoordinate();
903

    
904

    
905
                }
906

    
907
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
908
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
909
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
910

    
911
                foreach (var item in symbol.ChildSymbols)
912
                    CreateChildSymbol(item, _LMSymbol, symbol);
913

    
914
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
915
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
916

    
917
                double[] range = null;
918
                GetSPPIDSymbolRange(symbol, ref range);
919
                symbol.SPPID.SPPID_Min_X = range[0];
920
                symbol.SPPID.SPPID_Min_Y = range[1];
921
                symbol.SPPID.SPPID_Max_X = range[2];
922
                symbol.SPPID.SPPID_Max_Y = range[3];
923

    
924
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
925
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
926
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
927
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
928

    
929
                ReleaseCOMObjects(_LMSymbol);
930
            }
931
        }
932

    
933
        private void RemoveSymbol(Symbol symbol)
934
        {
935
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
936
            {
937
                LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
938
                if (_LMSymbol != null)
939
                {
940
                    _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
941
                    ReleaseCOMObjects(_LMSymbol);
942
                }
943
            }
944

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

    
955
        private void RemoveSymbol(List<Symbol> symbols)
956
        {
957
            foreach (var symbol in symbols)
958
            {
959
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
960
                {
961
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
962
                    if (_LMSymbol != null)
963
                    {
964
                        _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
965
                        ReleaseCOMObjects(_LMSymbol);
966
                    }
967
                }
968

    
969
                symbol.SPPID.RepresentationId = string.Empty;
970
                symbol.SPPID.ModelItemID = string.Empty;
971
                symbol.SPPID.SPPID_X = double.NaN;
972
                symbol.SPPID.SPPID_Y = double.NaN;
973
                symbol.SPPID.SPPID_Min_X = double.NaN;
974
                symbol.SPPID.SPPID_Min_Y = double.NaN;
975
                symbol.SPPID.SPPID_Max_X = double.NaN;
976
                symbol.SPPID.SPPID_Max_Y = double.NaN;
977
            }
978
        }
979

    
980
        /// <summary>
981
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
982
        /// </summary>
983
        /// <param name="targetConnector"></param>
984
        /// <param name="targetSymbol"></param>
985
        /// <param name="x"></param>
986
        /// <param name="y"></param>
987
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
988
        {
989
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
990

    
991
            double[] range = null;
992
            List<double[]> points = new List<double[]>();
993
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
994
            double x1 = range[0];
995
            double y1 = range[1];
996
            double x2 = range[2];
997
            double y2 = range[3];
998

    
999
            // Origin 기준 Connector의 위치차이
1000
            double sceneX = 0;
1001
            double sceneY = 0;
1002
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
1003
            double originX = 0;
1004
            double originY = 0;
1005
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
1006
            double gapX = originX - sceneX;
1007
            double gapY = originY - sceneY;
1008

    
1009
            // SPPID Symbol과 ID2 심볼의 크기 차이
1010
            double sizeWidth = 0;
1011
            double sizeHeight = 0;
1012
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
1013
            if (sizeWidth == 0 || sizeHeight == 0)
1014
                throw new Exception("Check symbol size! \r\nUID : " + targetSymbol.UID);
1015

    
1016
            double percentX = (x2 - x1) / sizeWidth;
1017
            double percentY = (y2 - y1) / sizeHeight;
1018

    
1019
            double SPPIDgapX = gapX * percentX;
1020
            double SPPIDgapY = gapY * percentY;
1021

    
1022
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
1023
            double distance = double.MaxValue;
1024
            double[] resultPoint;
1025
            foreach (var point in points)
1026
            {
1027
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
1028
                if (distance > result)
1029
                {
1030
                    distance = result;
1031
                    resultPoint = point;
1032
                    x = point[0];
1033
                    y = point[1];
1034
                }
1035
            }
1036

    
1037
            ReleaseCOMObjects(_TargetItem);
1038
        }
1039

    
1040
        private void GetTargetLineConnectorPoint(Connector targetConnector, Line targetLine, ref double x, ref double y)
1041
        {
1042
            int index = targetLine.CONNECTORS.IndexOf(targetConnector);
1043
            if (index == 0)
1044
            {
1045
                x = targetLine.SPPID.START_X;
1046
                y = targetLine.SPPID.START_Y;
1047
            }
1048
            else
1049
            {
1050
                x = targetLine.SPPID.END_X;
1051
                y = targetLine.SPPID.END_Y;
1052
            }
1053
        }
1054

    
1055
        /// <summary>
1056
        /// SPPID Symbol의 Range를 구한다.
1057
        /// </summary>
1058
        /// <param name="symbol"></param>
1059
        /// <param name="range"></param>
1060
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
1061
        {
1062
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1063
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1064
            double x1 = 0;
1065
            double y1 = 0;
1066
            double x2 = 0;
1067
            double y2 = 0;
1068
            symbol2d.Range(out x1, out y1, out x2, out y2);
1069
            range = new double[] { x1, y1, x2, y2 };
1070

    
1071
            for (int i = 1; i < int.MaxValue; i++)
1072
            {
1073
                double connX = 0;
1074
                double connY = 0;
1075
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
1076
                    points.Add(new double[] { connX, connY });
1077
                else
1078
                    break;
1079
            }
1080

    
1081
            foreach (var childSymbol in symbol.ChildSymbols)
1082
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
1083

    
1084
            ReleaseCOMObjects(_TargetItem);
1085
        }
1086

    
1087
        private void GetSPPIDSymbolRange(Symbol symbol, ref double[] range)
1088
        {
1089
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1090
            if (_TargetItem != null)
1091
            {
1092
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1093
                double x1 = 0;
1094
                double y1 = 0;
1095
                double x2 = 0;
1096
                double y2 = 0;
1097
                symbol2d.Range(out x1, out y1, out x2, out y2);
1098
                range = new double[] { x1, y1, x2, y2 };
1099

    
1100
                foreach (var childSymbol in symbol.ChildSymbols)
1101
                    GetSPPIDChildSymbolRange(childSymbol, ref range);
1102

    
1103
                ReleaseCOMObjects(_TargetItem);
1104
            }
1105
        }
1106

    
1107
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range)
1108
        {
1109
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
1110
            foreach (var symbol in symbols)
1111
            {
1112
                LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1113
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1114
                double x1 = 0;
1115
                double y1 = 0;
1116
                double x2 = 0;
1117
                double y2 = 0;
1118
                symbol2d.Range(out x1, out y1, out x2, out y2);
1119

    
1120
                tempRange[0] = Math.Min(tempRange[0], x1);
1121
                tempRange[1] = Math.Min(tempRange[1], y1);
1122
                tempRange[2] = Math.Max(tempRange[2], x2);
1123
                tempRange[3] = Math.Max(tempRange[3], y2);
1124

    
1125
                foreach (var childSymbol in symbol.ChildSymbols)
1126
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
1127

    
1128
                ReleaseCOMObjects(_TargetItem);
1129
            }
1130

    
1131
            range = tempRange;
1132
        }
1133

    
1134
        /// <summary>
1135
        /// Child Modeling 된 Symbol의 Range를 구한다.
1136
        /// </summary>
1137
        /// <param name="childSymbol"></param>
1138
        /// <param name="range"></param>
1139
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
1140
        {
1141
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1142
            if (_ChildSymbol != null)
1143
            {
1144
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1145
                double x1 = 0;
1146
                double y1 = 0;
1147
                double x2 = 0;
1148
                double y2 = 0;
1149
                symbol2d.Range(out x1, out y1, out x2, out y2);
1150
                range[0] = Math.Min(range[0], x1);
1151
                range[1] = Math.Min(range[1], y1);
1152
                range[2] = Math.Max(range[2], x2);
1153
                range[3] = Math.Max(range[3], y2);
1154

    
1155
                for (int i = 1; i < int.MaxValue; i++)
1156
                {
1157
                    double connX = 0;
1158
                    double connY = 0;
1159
                    if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
1160
                        points.Add(new double[] { connX, connY });
1161
                    else
1162
                        break;
1163
                }
1164

    
1165
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1166
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
1167

    
1168
                ReleaseCOMObjects(_ChildSymbol);
1169
            }
1170
        }
1171

    
1172
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range)
1173
        {
1174
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1175
            if (_ChildSymbol != null)
1176
            {
1177
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1178
                double x1 = 0;
1179
                double y1 = 0;
1180
                double x2 = 0;
1181
                double y2 = 0;
1182
                symbol2d.Range(out x1, out y1, out x2, out y2);
1183
                range[0] = Math.Min(range[0], x1);
1184
                range[1] = Math.Min(range[1], y1);
1185
                range[2] = Math.Max(range[2], x2);
1186
                range[3] = Math.Max(range[3], y2);
1187

    
1188
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1189
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
1190
                ReleaseCOMObjects(_ChildSymbol);
1191
            }
1192
        }
1193

    
1194
        /// <summary>
1195
        /// Label Symbol Modeling
1196
        /// </summary>
1197
        /// <param name="symbol"></param>
1198
        private void LabelSymbolModeling(Symbol symbol)
1199
        {
1200
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1201
            {
1202
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
1203
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
1204
                    return;
1205
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
1206

    
1207
                string symbolUID = itemAttribute.VALUE;
1208
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
1209
                if (targetItem != null &&
1210
                    (targetItem.GetType() == typeof(Symbol) ||
1211
                    targetItem.GetType() == typeof(Equipment)))
1212
                {
1213
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
1214
                    string sRep = null;
1215
                    if (targetItem.GetType() == typeof(Symbol))
1216
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
1217
                    else if (targetItem.GetType() == typeof(Equipment))
1218
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
1219
                    if (!string.IsNullOrEmpty(sRep))
1220
                    {
1221
                        // LEADER Line 검사
1222
                        bool leaderLine = false;
1223
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1224
                        if (symbolMapping != null)
1225
                            leaderLine = symbolMapping.LEADERLINE;
1226

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

    
1231
                        //Leader 선 센터로
1232
                        if (_LMLabelPresist != null)
1233
                        {
1234
                            // Target Item에 Label의 Attribute Input
1235
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1236

    
1237
                            string OID = _LMLabelPresist.get_GraphicOID().ToString();
1238
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
1239
                            if (dependency != null)
1240
                            {
1241
                                bool result = false;
1242
                                foreach (var attributes in dependency.AttributeSets)
1243
                                {
1244
                                    foreach (var attribute in attributes)
1245
                                    {
1246
                                        string name = attribute.Name;
1247
                                        string value = attribute.GetValue().ToString();
1248
                                        if (name == "DrawingItemType" && value == "LabelPersist")
1249
                                        {
1250
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
1251
                                            {
1252
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
1253
                                                {
1254
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
1255
                                                    double prevX = _TargetItem.get_XCoordinate();
1256
                                                    double prevY = _TargetItem.get_YCoordinate();
1257
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
1258
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
1259
                                                    result = true;
1260
                                                    break;
1261
                                                }
1262
                                            }
1263
                                        }
1264

    
1265
                                        if (result)
1266
                                            break;
1267
                                    }
1268

    
1269
                                    if (result)
1270
                                        break;
1271
                                }
1272
                            }
1273

    
1274
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1275
                            _LMLabelPresist.Commit();
1276
                            ReleaseCOMObjects(_LMLabelPresist);
1277
                        }
1278

    
1279
                        ReleaseCOMObjects(_TargetItem);
1280
                    }
1281
                }
1282
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
1283
                {
1284
                    Line targetLine = targetItem as Line;
1285
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
1286
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
1287
                    if (connectedLMConnector != null)
1288
                    {
1289
                        // LEADER Line 검사
1290
                        bool leaderLine = false;
1291
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1292
                        if (symbolMapping != null)
1293
                            leaderLine = symbolMapping.LEADERLINE;
1294

    
1295
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1296
                        if (_LMLabelPresist != null)
1297
                        {
1298
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1299
                            _LMLabelPresist.Commit();
1300
                            ReleaseCOMObjects(_LMLabelPresist);
1301
                        }
1302
                        ReleaseCOMObjects(connectedLMConnector);
1303
                    }
1304

    
1305
                    foreach (var item in connectorVertices)
1306
                        if (item.Key != null)
1307
                            ReleaseCOMObjects(item.Key);
1308
                }
1309
            }
1310
        }
1311

    
1312
        /// <summary>
1313
        /// Equipment를 실제로 Modeling 메서드
1314
        /// </summary>
1315
        /// <param name="equipment"></param>
1316
        private void EquipmentModeling(Equipment equipment)
1317
        {
1318
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
1319
                return;
1320

    
1321
            LMSymbol _LMSymbol = null;
1322
            LMSymbol targetItem = null;
1323
            string mappingPath = equipment.SPPID.MAPPINGNAME;
1324
            double x = equipment.SPPID.ORIGINAL_X;
1325
            double y = equipment.SPPID.ORIGINAL_Y;
1326
            int mirror = 0;
1327
            double angle = equipment.ANGLE;
1328

    
1329
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
1330

    
1331
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
1332
            if (connector != null)
1333
            {
1334
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
1335
                if (connEquipment != null)
1336
                {
1337
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1338
                        EquipmentModeling(connEquipment);
1339

    
1340
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1341
                    {
1342
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
1343
                        if (targetItem != null)
1344
                        {
1345
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
1346
                        }
1347
                        else
1348
                        {
1349
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1350
                        }
1351
                    }
1352
                    else
1353
                    {
1354
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1355
                    }
1356
                }
1357
                else
1358
                {
1359
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1360
                }
1361
            }
1362
            else
1363
            {
1364
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1365
            }
1366

    
1367
            if (_LMSymbol != null)
1368
            {
1369
                _LMSymbol.Commit();
1370
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1371
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
1372
                ReleaseCOMObjects(_LMSymbol);
1373
            }
1374

    
1375
            if (targetItem != null)
1376
            {
1377
                ReleaseCOMObjects(targetItem);
1378
            }
1379

    
1380
            ReleaseCOMObjects(_LMSymbol);
1381
        }
1382

    
1383
        /// <summary>
1384
        /// 첫 진입점
1385
        /// </summary>
1386
        /// <param name="symbol"></param>
1387
        private void SymbolModelingBySymbol(Symbol symbol)
1388
        {
1389
            SymbolModeling(symbol, null);
1390
            List<object> endObjects = new List<object>();
1391
            endObjects.Add(symbol);
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
                    endObjects.Add(connItem);
1399
                    if (connItem.GetType() == typeof(Symbol))
1400
                    {
1401
                        Symbol connSymbol = connItem as Symbol;
1402
                        if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1403
                        {
1404
                            SymbolModeling(connSymbol, symbol);
1405
                        }
1406
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1407
                        SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1408
                    }
1409
                    else if (connItem.GetType() == typeof(Line))
1410
                    {
1411
                        Line connLine = connItem as Line;
1412
                        SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1413
                    }
1414
                }
1415
            }
1416
        }
1417

    
1418
        private void SymbolModelingByNeerSymbolLoop(Symbol symbol, List<object> endObjects)
1419
        {
1420
            foreach (var connector in symbol.CONNECTORS)
1421
            {
1422
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1423
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1424
                {
1425
                    if (!endObjects.Contains(connItem))
1426
                    {
1427
                        endObjects.Add(connItem);
1428
                        if (connItem.GetType() == typeof(Symbol))
1429
                        {
1430
                            Symbol connSymbol = connItem as Symbol;
1431
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1432
                            {
1433
                                SymbolModeling(connSymbol, symbol);
1434
                            }
1435
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1436
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1437
                        }
1438
                        else if (connItem.GetType() == typeof(Line))
1439
                        {
1440
                            Line connLine = connItem as Line;
1441
                            SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1442
                        }
1443
                    }
1444
                }
1445
            }
1446
        }
1447

    
1448
        private void SymbolModelingByNeerLineLoop(Line line, List<object> endObjects, Symbol prevSymbol)
1449
        {
1450
            foreach (var connector in line.CONNECTORS)
1451
            {
1452
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1453
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1454
                {
1455
                    if (!endObjects.Contains(connItem))
1456
                    {
1457
                        endObjects.Add(connItem);
1458
                        if (connItem.GetType() == typeof(Symbol))
1459
                        {
1460
                            Symbol connSymbol = connItem as Symbol;
1461
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1462
                            {
1463
                                List<Symbol> group = new List<Symbol>();
1464
                                SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
1465
                                Symbol priority = prioritySymbols.Find(x => group.Contains(x));
1466
                                List<Symbol> endModelingGroup = new List<Symbol>();
1467
                                if (priority != null)
1468
                                {
1469
                                    SymbolGroupModeling(priority, group);
1470

    
1471
                                    // Range 겹치는지 확인해야함
1472
                                    double[] prevRange = null;
1473
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1474
                                    double[] groupRange = null;
1475
                                    GetSPPIDSymbolRange(group, ref groupRange);
1476

    
1477
                                    double distanceX = 0;
1478
                                    double distanceY = 0;
1479
                                    bool overlapX = false;
1480
                                    bool overlapY = false;
1481
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1482
                                    SPPIDUtil.CalcOverlap(prevRange, groupRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1483
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1484
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1485
                                    {
1486
                                        RemoveSymbol(group);
1487
                                        foreach (var _temp in group)
1488
                                            SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, distanceX, distanceY);
1489

    
1490
                                        SymbolGroupModeling(priority, group);
1491
                                    }
1492
                                }
1493
                                else
1494
                                {
1495
                                    SymbolModeling(connSymbol, null);
1496
                                    // Range 겹치는지 확인해야함
1497
                                    double[] prevRange = null;
1498
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1499
                                    double[] connRange = null;
1500
                                    GetSPPIDSymbolRange(connSymbol, ref connRange);
1501

    
1502
                                    double distanceX = 0;
1503
                                    double distanceY = 0;
1504
                                    bool overlapX = false;
1505
                                    bool overlapY = false;
1506
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1507
                                    SPPIDUtil.CalcOverlap(prevRange, connRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1508
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1509
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1510
                                    {
1511
                                        RemoveSymbol(connSymbol);
1512
                                        SPPIDUtil.CalcNewCoordinateForSymbol(connSymbol, prevSymbol, distanceX, distanceY);
1513

    
1514
                                        SymbolModeling(connSymbol, null);
1515
                                    }
1516
                                }
1517
                            }
1518
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1519
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1520
                        }
1521
                        else if (connItem.GetType() == typeof(Line))
1522
                        {
1523
                            Line connLine = connItem as Line;
1524
                            if (!SPPIDUtil.IsBranchLine(connLine, line))
1525
                                SymbolModelingByNeerLineLoop(connLine, endObjects, prevSymbol);
1526
                        }
1527
                    }
1528
                }
1529
            }
1530
        }
1531

    
1532
        private void SymbolGroupModeling(Symbol firstSymbol, List<Symbol> group)
1533
        {
1534
            List<Symbol> endModelingGroup = new List<Symbol>();
1535
            SymbolModeling(firstSymbol, null);
1536
            endModelingGroup.Add(firstSymbol);
1537
            while (endModelingGroup.Count != group.Count)
1538
            {
1539
                foreach (var _symbol in group)
1540
                {
1541
                    if (!endModelingGroup.Contains(_symbol))
1542
                    {
1543
                        foreach (var _connector in _symbol.CONNECTORS)
1544
                        {
1545
                            Symbol _connSymbol = SPPIDUtil.FindObjectByUID(document, _connector.CONNECTEDITEM) as Symbol;
1546
                            if (_connSymbol != null && endModelingGroup.Contains(_connSymbol))
1547
                            {
1548
                                SymbolModeling(_symbol, _connSymbol);
1549
                                endModelingGroup.Add(_symbol);
1550
                                break;
1551
                            }
1552
                        }
1553
                    }
1554
                }
1555
            }
1556
        }
1557

    
1558
        /// <summary>
1559
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
1560
        /// </summary>
1561
        /// <param name="childSymbol"></param>
1562
        /// <param name="parentSymbol"></param>
1563
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol, Symbol parent)
1564
        {
1565
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
1566
            double x1 = 0;
1567
            double x2 = 0;
1568
            double y1 = 0;
1569
            double y2 = 0;
1570
            symbol2d.Range(out x1, out y1, out x2, out y2);
1571

    
1572
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
1573
            if (_LMSymbol != null)
1574
            {
1575
                _LMSymbol.Commit();
1576
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1577
                foreach (var item in childSymbol.ChildSymbols)
1578
                    CreateChildSymbol(item, _LMSymbol, parent);
1579
            }
1580

    
1581

    
1582
            ReleaseCOMObjects(_LMSymbol);
1583
        }
1584
        double index = 0;
1585
        private void NewLineModeling(Line line, bool isBranchModeling = false)
1586
        {
1587
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) || (BranchLines.Contains(line) && !isBranchModeling))
1588
                return;
1589

    
1590
            List<Line> group = new List<Line>();
1591
            GetConnectedLineGroup(line, group);
1592
            LineCoordinateCorrection(group);
1593

    
1594
            foreach (var groupLine in group)
1595
            {
1596
                if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
1597
                {
1598
                    BranchLines.Add(groupLine);
1599
                    continue;
1600
                }
1601

    
1602
                bool diagonal = false;
1603
                if (groupLine.SlopeType != SlopeType.HORIZONTAL && groupLine.SlopeType != SlopeType.VERTICAL)
1604
                    diagonal = true;
1605
                _LMAItem _LMAItem = _placement.PIDCreateItem(groupLine.SPPID.MAPPINGNAME);
1606
                LMSymbol _LMSymbolStart = null;
1607
                LMSymbol _LMSymbolEnd = null;
1608
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1609
                foreach (var connector in groupLine.CONNECTORS)
1610
                {
1611
                    double x = 0;
1612
                    double y = 0;
1613
                    GetTargetLineConnectorPoint(connector, groupLine, ref x, ref y);
1614
                    if (connector.ConnectedObject == null)
1615
                    {
1616
                        placeRunInputs.AddPoint(x, y);
1617
                    }
1618
                    else if (connector.ConnectedObject.GetType() == typeof(Symbol))
1619
                    {
1620
                        Symbol targetSymbol = connector.ConnectedObject as Symbol;
1621
                        GetTargetSymbolConnectorPoint(targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == groupLine), targetSymbol, ref x, ref y);
1622
                        if (groupLine.CONNECTORS.IndexOf(connector) == 0)
1623
                        {
1624
                            _LMSymbolStart = GetTargetSymbol(targetSymbol, groupLine);
1625
                            if (_LMSymbolStart != null)
1626
                                placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y, diagonal);
1627
                            else
1628
                                placeRunInputs.AddPoint(x, y);
1629
                        }
1630
                        else
1631
                        {
1632
                            _LMSymbolEnd = GetTargetSymbol(targetSymbol, groupLine);
1633
                            if (_LMSymbolEnd != null)
1634
                                placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y, diagonal);
1635
                            else
1636
                                placeRunInputs.AddPoint(x, y);
1637
                        }
1638
                    }
1639
                    else if (connector.ConnectedObject.GetType() == typeof(Line))
1640
                    {
1641
                        Line targetLine = connector.ConnectedObject as Line;
1642
                        if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
1643
                        {
1644
                            LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
1645
                            if (targetConnector != null)
1646
                            {
1647
                                placeRunInputs.AddConnectorTarget(targetConnector, x, y, diagonal);
1648
                                ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
1649
                            }
1650
                            else
1651
                            {
1652
                                placeRunInputs.AddPoint( x, y);
1653
                                ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
1654
                            }
1655
                        }
1656
                        else
1657
                        {
1658
                            if (groupLine.CONNECTORS.IndexOf(connector) == 0)
1659
                            {
1660
                                index += 0.01;
1661
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1662
                                    placeRunInputs.AddPoint(x, -0.1 - index);
1663
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
1664
                                    placeRunInputs.AddPoint(-0.1 - index, y);
1665
                                else
1666
                                {
1667
                                    Line nextLine = groupLine.CONNECTORS[0].ConnectedObject as Line;
1668
                                    if (SPPIDUtil.CalcAngle(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y) < 45)
1669
                                        placeRunInputs.AddPoint(-0.1 - index, y);
1670
                                    else
1671
                                        placeRunInputs.AddPoint(x, -0.1 - index);
1672
                                }
1673
                            }
1674

    
1675
                            placeRunInputs.AddPoint(x, y);
1676

    
1677
                            if (groupLine.CONNECTORS.IndexOf(connector) == 1)
1678
                            {
1679
                                index += 0.01;
1680
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1681
                                    placeRunInputs.AddPoint(x, -0.1 - index);
1682
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
1683
                                    placeRunInputs.AddPoint(-0.1 - index, y);
1684
                                else
1685
                                {
1686
                                    Line nextLine = groupLine.CONNECTORS[1].ConnectedObject as Line;
1687
                                    if (SPPIDUtil.CalcAngle(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y) < 45)
1688
                                        placeRunInputs.AddPoint(-0.1 - index, y);
1689
                                    else
1690
                                        placeRunInputs.AddPoint(x, -0.1 - index);
1691
                                }
1692
                            }
1693
                        }
1694
                    }
1695
                }
1696

    
1697
                LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1698
                if (_lMConnector != null)
1699
                {
1700
                    _lMConnector.Commit();
1701
                    groupLine.SPPID.ModelItemId = _lMConnector.ModelItemID;
1702
                    bool bRemodelingStart = false;
1703
                    if (_LMSymbolStart != null)
1704
                        NeedReModeling(groupLine, _LMSymbolStart, ref bRemodelingStart);
1705
                    bool bRemodelingEnd = false;
1706
                    if (_LMSymbolEnd != null)
1707
                        NeedReModeling(groupLine, _LMSymbolEnd, ref bRemodelingEnd);
1708

    
1709
                    if (bRemodelingStart || bRemodelingEnd)
1710
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
1711

    
1712
                    FlowMarkModeling(groupLine);
1713
                    LineNumberModelingOnlyOne(groupLine);
1714

    
1715
                    ReleaseCOMObjects(_lMConnector);
1716
                }
1717
                else if (!isBranchModeling)
1718
                {
1719
                    Log.Write("Main Line Modeling : " + groupLine.UID);
1720
                }
1721

    
1722
                List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
1723
                x.ConnectedObject != null &&
1724
                x.ConnectedObject.GetType() == typeof(Line) &&
1725
                !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
1726
                .Select(x => x.ConnectedObject)
1727
                .ToList();
1728

    
1729
                foreach (var item in removeLines)
1730
                    RemoveLineForModeling(item as Line);
1731

    
1732
                if (_LMAItem != null)
1733
                    ReleaseCOMObjects(_LMAItem);
1734
                if (placeRunInputs != null)
1735
                    ReleaseCOMObjects(placeRunInputs);
1736
                if (_LMSymbolStart != null)
1737
                    ReleaseCOMObjects(_LMSymbolStart);
1738
                if (_LMSymbolEnd != null)
1739
                    ReleaseCOMObjects(_LMSymbolEnd);
1740

    
1741
                if (isBranchModeling && BranchLines.Contains(groupLine))
1742
                    BranchLines.Remove(groupLine);
1743
            }
1744
        }
1745

    
1746
        private void RemoveLineForModeling(Line line)
1747
        {
1748
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1749
            if (modelItem != null)
1750
            {
1751
                foreach (LMRepresentation rep in modelItem.Representations)
1752
                {
1753
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1754
                    {
1755
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1756
                        dynamic OID = rep.get_GraphicOID().ToString();
1757
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1758
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1759
                        int verticesCount = lineStringGeometry.VertexCount;
1760
                        double[] vertices = null;
1761
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1762
                        for (int i = 0; i < verticesCount; i++)
1763
                        {
1764
                            double x = 0;
1765
                            double y = 0;
1766
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1767
                            if (verticesCount == 2 && (x < 0 || y < 0))
1768
                                _placement.PIDRemovePlacement(rep);
1769
                        }
1770
                        ReleaseCOMObjects(_LMConnector);
1771
                    }
1772
                }
1773

    
1774
                ReleaseCOMObjects(modelItem);
1775
            }
1776
        }
1777

    
1778
        private void GetConnectedLineGroup(Line line, List<Line> group)
1779
        {
1780
            if (!group.Contains(line))
1781
                group.Add(line);
1782
            foreach (var connector in line.CONNECTORS)
1783
            {
1784
                if (connector.ConnectedObject != null &&
1785
                    connector.ConnectedObject.GetType() == typeof(Line) &&
1786
                    !group.Contains(connector.ConnectedObject) &&
1787
                    string.IsNullOrEmpty(((Line)connector.ConnectedObject).SPPID.ModelItemId))
1788
                {
1789
                    Line connLine = connector.ConnectedObject as Line;
1790
                    if (!SPPIDUtil.IsBranchLine(connLine, line))
1791
                    {
1792
                        if (line.CONNECTORS.IndexOf(connector) == 0)
1793
                            group.Insert(0, connLine);
1794
                        else
1795
                            group.Add(connLine);
1796
                        GetConnectedLineGroup(connLine, group);
1797
                    }
1798
                }
1799
            }
1800
        }
1801

    
1802
        private void LineCoordinateCorrection(List<Line> group)
1803
        {
1804
            // 순서대로 전 Item 기준 정렬
1805
            LineCoordinateCorrectionByStart(group);
1806

    
1807
            // 역으로 심볼이 있을 경우 좌표 보정
1808
            LineCoordinateCorrectionForLastLine(group);
1809
        }
1810

    
1811
        private void LineCoordinateCorrectionByStart(List<Line> group)
1812
        {
1813
            for (int i = 0; i < group.Count; i++)
1814
            {
1815
                Line line = group[i];
1816
                if (i == 0)
1817
                {
1818
                    Connector symbolConnector = line.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1819
                    if (symbolConnector != null)
1820
                        LineCoordinateCorrectionByConnItem(line, symbolConnector.ConnectedObject);
1821
                }
1822
                else if (i != 0)
1823
                {
1824
                    LineCoordinateCorrectionByConnItem(line, group[i - 1]);
1825
                }
1826
            }
1827
        }
1828

    
1829
        private void LineCoordinateCorrectionForLastLine(List<Line> group)
1830
        {
1831
            Line checkLine = group[group.Count - 1];
1832
            Connector lastSymbolConnector = checkLine.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1833
            if (lastSymbolConnector != null)
1834
            {
1835
                LineCoordinateCorrectionByConnItem(checkLine, lastSymbolConnector.ConnectedObject);
1836
                for (int i = group.Count - 2; i >= 0; i--)
1837
                {
1838
                    Line line = group[i + 1];
1839
                    Line prevLine = group[i];
1840

    
1841
                    // 같으면 보정
1842
                    if (line.SlopeType == prevLine.SlopeType)
1843
                        LineCoordinateCorrectionByConnItem(prevLine, line);
1844
                    else
1845
                    {
1846
                        if (line.SlopeType == SlopeType.HORIZONTAL)
1847
                        {
1848
                            double prevX = 0;
1849
                            double prevY = 0;
1850
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1851
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(line, prevLine, prevX);
1852

    
1853
                            double x = 0;
1854
                            double y = 0;
1855
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1856
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(prevLine, line, y);
1857
                        }
1858
                        else if (line.SlopeType == SlopeType.VERTICAL)
1859
                        {
1860
                            double prevX = 0;
1861
                            double prevY = 0;
1862
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1863
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(line, prevLine, prevY);
1864

    
1865
                            double x = 0;
1866
                            double y = 0;
1867
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1868
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
1869
                        }
1870
                        break;
1871
                    }
1872
                }
1873
            }
1874
        }
1875

    
1876
        private void LineCoordinateCorrectionByConnItem(Line line, object connItem)
1877
        {
1878
            double x = 0;
1879
            double y = 0;
1880
            if (connItem.GetType() == typeof(Symbol))
1881
            {
1882
                Symbol targetSymbol = connItem as Symbol;
1883
                Connector targetConnector = targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line);
1884
                if (targetConnector != null)
1885
                    GetTargetSymbolConnectorPoint(targetConnector, targetSymbol, ref x, ref y);
1886
                else
1887
                    throw new Exception("Target symbol UID : " + targetSymbol.UID + "\r\nLine UID : " + line.UID);
1888
            }
1889
            else if (connItem.GetType() == typeof(Line))
1890
            {
1891
                Line targetLine = connItem as Line;
1892
                GetTargetLineConnectorPoint(targetLine.CONNECTORS.Find(z => z.ConnectedObject == line), targetLine, ref x, ref y);
1893
            }
1894

    
1895
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
1896
        }
1897

    
1898
        private void ChangeLineSPPIDCoordinateByConnector(Line line, object connItem, double x, double y, bool changeOtherCoordinate = true)
1899
        {
1900
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1901
            int index = line.CONNECTORS.IndexOf(connector);
1902
            if (index == 0)
1903
            {
1904
                line.SPPID.START_X = x;
1905
                line.SPPID.START_Y = y;
1906
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1907
                    line.SPPID.END_Y = y;
1908
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1909
                    line.SPPID.END_X = x;
1910
            }
1911
            else
1912
            {
1913
                line.SPPID.END_X = x;
1914
                line.SPPID.END_Y = y;
1915
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1916
                    line.SPPID.START_Y = y;
1917
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1918
                    line.SPPID.START_X = x;
1919
            }
1920
        }
1921

    
1922
        private void ChangeLineSPPIDCoordinateByConnectorOnlyX(Line line, object connItem, double x)
1923
        {
1924
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1925
            int index = line.CONNECTORS.IndexOf(connector);
1926
            if (index == 0)
1927
            {
1928
                line.SPPID.START_X = x;
1929
                if (line.SlopeType == SlopeType.VERTICAL)
1930
                    line.SPPID.END_X = x;
1931
            }
1932
            else
1933
            {
1934
                line.SPPID.END_X = x;
1935
                if (line.SlopeType == SlopeType.VERTICAL)
1936
                    line.SPPID.START_X = x;
1937
            }
1938
        }
1939

    
1940
        private void ChangeLineSPPIDCoordinateByConnectorOnlyY(Line line, object connItem, double y)
1941
        {
1942
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1943
            int index = line.CONNECTORS.IndexOf(connector);
1944
            if (index == 0)
1945
            {
1946
                line.SPPID.START_Y = y;
1947
                if (line.SlopeType == SlopeType.HORIZONTAL)
1948
                    line.SPPID.END_Y = y;
1949
            }
1950
            else
1951
            {
1952
                line.SPPID.END_Y = y;
1953
                if (line.SlopeType == SlopeType.HORIZONTAL)
1954
                    line.SPPID.START_Y = y;
1955
            }
1956
        }
1957

    
1958
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
1959
        {
1960
            if (symbol != null)
1961
            {
1962
                string repID = symbol.AsLMRepresentation().Id;
1963
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
1964
                string lineUID = line.UID;
1965

    
1966
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
1967
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
1968
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
1969

    
1970
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
1971
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
1972
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
1973

    
1974
                if (startSpecBreak != null || startEndBreak != null)
1975
                    result = true;
1976
            }
1977
        }
1978

    
1979
        /// <summary>
1980
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
1981
        /// </summary>
1982
        /// <param name="lines"></param>
1983
        /// <param name="prevLMConnector"></param>
1984
        /// <param name="startSymbol"></param>
1985
        /// <param name="endSymbol"></param>
1986
        private void ReModelingLine(Line line, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
1987
        {
1988
            string symbolPath = string.Empty;
1989
            #region get symbol path
1990
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
1991
            symbolPath = GetSPPIDFileName(modelItem);
1992
            ReleaseCOMObjects(modelItem);
1993
            #endregion
1994
            bool diagonal = false;
1995
            if (line.SlopeType != SlopeType.HORIZONTAL && line.SlopeType != SlopeType.VERTICAL)
1996
                diagonal = true;
1997
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1998
            LMConnector newConnector = null;
1999
            dynamic OID = prevLMConnector.get_GraphicOID().ToString();
2000
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2001
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2002
            int verticesCount = lineStringGeometry.VertexCount;
2003
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2004

    
2005
            List<double[]> vertices = new List<double[]>();
2006
            for (int i = 1; i <= verticesCount; i++)
2007
            {
2008
                double x = 0;
2009
                double y = 0;
2010
                lineStringGeometry.GetVertex(i, ref x, ref y);
2011
                vertices.Add(new double[] { x, y });
2012
            }
2013

    
2014
            for (int i = 0; i < vertices.Count; i++)
2015
            {
2016
                double[] points = vertices[i];
2017
                // 시작 심볼이 있고 첫번째 좌표일 때
2018
                if (startSymbol != null && i == 0)
2019
                {
2020
                    if (bStart)
2021
                    {
2022
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
2023
                        if (slopeType == SlopeType.HORIZONTAL)
2024
                            placeRunInputs.AddPoint(points[0], -0.1);
2025
                        else if (slopeType == SlopeType.VERTICAL)
2026
                            placeRunInputs.AddPoint(-0.1, points[1]);
2027
                        else
2028
                            placeRunInputs.AddPoint(points[0], -0.1);
2029

    
2030
                        placeRunInputs.AddPoint(points[0], points[1]);
2031
                    }
2032
                    else
2033
                    {
2034
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1], diagonal);
2035
                    }
2036
                }
2037
                // 마지막 심볼이 있고 마지막 좌표일 때
2038
                else if (endSymbol != null && i == vertices.Count - 1)
2039
                {
2040
                    if (bEnd)
2041
                    {
2042
                        placeRunInputs.AddPoint(points[0], points[1]);
2043

    
2044
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
2045
                        if (slopeType == SlopeType.HORIZONTAL)
2046
                            placeRunInputs.AddPoint(points[0], -0.1);
2047
                        else if (slopeType == SlopeType.VERTICAL)
2048
                            placeRunInputs.AddPoint(-0.1, points[1]);
2049
                        else
2050
                            placeRunInputs.AddPoint(points[0], -0.1);
2051
                    }
2052
                    else
2053
                    {
2054
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1], diagonal);
2055
                    }
2056
                }
2057
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
2058
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
2059
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1], diagonal);
2060
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
2061
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
2062
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1], diagonal);
2063
                else
2064
                    placeRunInputs.AddPoint(points[0], points[1]);
2065
            }
2066

    
2067
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
2068
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2069

    
2070
            ReleaseCOMObjects(placeRunInputs);
2071
            ReleaseCOMObjects(_LMAItem);
2072
            ReleaseCOMObjects(modelItem);
2073

    
2074
            if (newConnector != null)
2075
            {
2076
                newConnector.Commit();
2077
                if (startSymbol != null && bStart)
2078
                {
2079
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2080
                    placeRunInputs = new PlaceRunInputs();
2081
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
2082
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
2083
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2084
                    if (_LMConnector != null)
2085
                    {
2086
                        _LMConnector.Commit();
2087
                        RemoveConnectorForReModelingLine(newConnector);
2088
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
2089
                        ReleaseCOMObjects(_LMConnector);
2090
                    }
2091
                    ReleaseCOMObjects(placeRunInputs);
2092
                    ReleaseCOMObjects(_LMAItem);
2093
                }
2094

    
2095
                if (endSymbol != null && bEnd)
2096
                {
2097
                    if (startSymbol != null)
2098
                    {
2099
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
2100
                        newConnector = dicVertices.First().Key;
2101
                    }
2102

    
2103
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2104
                    placeRunInputs = new PlaceRunInputs();
2105
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2106
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2107
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2108
                    if (_LMConnector != null)
2109
                    {
2110
                        _LMConnector.Commit();
2111
                        RemoveConnectorForReModelingLine(newConnector);
2112
                        ZeroLengthModelItemIDReverse.Add(_LMConnector.ModelItemID);
2113
                        ReleaseCOMObjects(_LMConnector);
2114
                    }
2115
                    ReleaseCOMObjects(placeRunInputs);
2116
                    ReleaseCOMObjects(_LMAItem);
2117
                }
2118

    
2119
                line.SPPID.ModelItemId = newConnector.ModelItemID;
2120
                ReleaseCOMObjects(newConnector);
2121
            }
2122

    
2123
            ReleaseCOMObjects(modelItem);
2124
        }
2125

    
2126
        /// <summary>
2127
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
2128
        /// </summary>
2129
        /// <param name="connector"></param>
2130
        private void RemoveConnectorForReModelingLine(LMConnector connector)
2131
        {
2132
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
2133
            foreach (var item in dicVertices)
2134
            {
2135
                if (item.Value.Count == 2)
2136
                {
2137
                    bool result = false;
2138
                    foreach (var point in item.Value)
2139
                    {
2140
                        if (point[0] < 0 || point[1] < 0)
2141
                        {
2142
                            result = true;
2143
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
2144
                            break;
2145
                        }
2146
                    }
2147

    
2148
                    if (result)
2149
                        break;
2150
                }
2151
            }
2152
            foreach (var item in dicVertices)
2153
                ReleaseCOMObjects(item.Key);
2154
        }
2155

    
2156
        /// <summary>
2157
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
2158
        /// </summary>
2159
        /// <param name="symbol"></param>
2160
        /// <param name="line"></param>
2161
        /// <returns></returns>
2162
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
2163
        {
2164
            LMSymbol _LMSymbol = null;
2165
            foreach (var connector in symbol.CONNECTORS)
2166
            {
2167
                if (connector.CONNECTEDITEM == line.UID)
2168
                {
2169
                    if (connector.Index == 0)
2170
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2171
                    else
2172
                    {
2173
                        ChildSymbol child = null;
2174
                        foreach (var childSymbol in symbol.ChildSymbols)
2175
                        {
2176
                            if (childSymbol.Connectors.Contains(connector))
2177
                                child = childSymbol;
2178
                            else
2179
                                child = GetChildSymbolByConnector(childSymbol, connector);
2180

    
2181
                            if (child != null)
2182
                                break;
2183
                        }
2184

    
2185
                        if (child != null)
2186
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
2187
                    }
2188

    
2189
                    break;
2190
                }
2191
            }
2192

    
2193
            return _LMSymbol;
2194
        }
2195

    
2196
        /// <summary>
2197
        /// Connector를 가지고 있는 ChildSymbol Object 반환
2198
        /// </summary>
2199
        /// <param name="item"></param>
2200
        /// <param name="connector"></param>
2201
        /// <returns></returns>
2202
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
2203
        {
2204
            foreach (var childSymbol in item.ChildSymbols)
2205
            {
2206
                if (childSymbol.Connectors.Contains(connector))
2207
                    return childSymbol;
2208
                else
2209
                    return GetChildSymbolByConnector(childSymbol, connector);
2210
            }
2211

    
2212
            return null;
2213
        }
2214

    
2215
        /// <summary>
2216
        /// EndBreak 모델링 메서드
2217
        /// </summary>
2218
        /// <param name="endBreak"></param>
2219
        private void EndBreakModeling(EndBreak endBreak)
2220
        {
2221
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
2222
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
2223
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
2224

    
2225
            if (targetLMConnector != null)
2226
            {
2227
                double[] point = GetSegmentPoint(ownerObj, connectedItem, targetLMConnector);
2228
                Array array = null;
2229
                if (point != null)
2230
                    array = new double[] { 0, point[0], point[1] };
2231
                else
2232
                    array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
2233
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
2234
                if (_LmLabelPersist != null)
2235
                {
2236
                    _LmLabelPersist.Commit();
2237
                    endBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2238
                    if (_LmLabelPersist.ModelItemObject != null)
2239
                        endBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2240
                    endBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
2241
                    ReleaseCOMObjects(_LmLabelPersist);
2242
                }
2243
                ReleaseCOMObjects(targetLMConnector);
2244
            }
2245
        }
2246

    
2247
        private LMConnector ReModelingLMConnector(LMConnector connector, string changeSymbolPath = null)
2248
        {
2249
            string symbolPath = string.Empty;
2250
            #region get symbol path
2251
            if (string.IsNullOrEmpty(changeSymbolPath))
2252
            {
2253
                LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
2254
                symbolPath = GetSPPIDFileName(modelItem);
2255
                ReleaseCOMObjects(modelItem);
2256
            }
2257
            else
2258
                symbolPath = changeSymbolPath;
2259
            
2260
            #endregion
2261

    
2262
            LMConnector newConnector = null;
2263
            dynamic OID = connector.get_GraphicOID().ToString();
2264
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2265
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2266
            int verticesCount = lineStringGeometry.VertexCount;
2267
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2268
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2269

    
2270
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
2271
            {
2272
                double[] vertices = null;
2273
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2274
                double x = 0;
2275
                double y = 0;
2276
                lineStringGeometry.GetVertex(1, ref x, ref y);
2277

    
2278
                string flowDirection = string.Empty;
2279
                LMAAttribute flowAttribute = connector.ModelItemObject.Attributes["FlowDirection"];
2280
                if (flowAttribute != null && !DBNull.Value.Equals(flowAttribute.get_Value()))
2281
                    flowDirection = flowAttribute.get_Value().ToString();
2282

    
2283
                if (flowDirection == "End 1 is downstream (Outlet)")
2284
                {
2285
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
2286
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
2287
                    flowDirection = "End 1 is upstream (Inlet)";
2288
                }
2289
                else
2290
                {
2291
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
2292
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
2293
                }
2294

    
2295
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2296
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2297
                newConnector.Commit();
2298
                ZeroLengthSymbolToSymbolModelItemID.Add(newConnector.ModelItemID);
2299
                if (!string.IsNullOrEmpty(flowDirection))
2300
                    newConnector.ModelItemObject.Attributes["FlowDirection"].set_Value(flowDirection);
2301
            }
2302
            else
2303
            {
2304
                List<double[]> vertices = new List<double[]>();
2305
                for (int i = 1; i <= verticesCount; i++)
2306
                {
2307
                    double x = 0;
2308
                    double y = 0;
2309
                    lineStringGeometry.GetVertex(i, ref x, ref y);
2310
                    vertices.Add(new double[] { x, y });
2311
                }
2312

    
2313
                for (int i = 0; i < vertices.Count; i++)
2314
                {
2315
                    double[] points = vertices[i];
2316
                    if (i == 0)
2317
                    {
2318
                        if (connector.ConnectItem1SymbolObject != null)
2319
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, points[0], points[1]);
2320
                        else
2321
                            placeRunInputs.AddPoint(points[0], points[1]);
2322
                    }
2323
                    else if (i == vertices.Count - 1)
2324
                    {
2325
                        if (connector.ConnectItem2SymbolObject != null)
2326
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, points[0], points[1]);
2327
                        else
2328
                            placeRunInputs.AddPoint(points[0], points[1]);
2329
                    }
2330
                    else
2331
                        placeRunInputs.AddPoint(points[0], points[1]);
2332
                }
2333

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

    
2336
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2337
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2338
                newConnector.Commit();
2339
                foreach (var line in lines)
2340
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
2341
            }
2342

    
2343

    
2344
            return newConnector;
2345
        }
2346

    
2347
        /// <summary>
2348
        /// SpecBreak Modeling 메서드
2349
        /// </summary>
2350
        /// <param name="specBreak"></param>
2351
        private void SpecBreakModeling(SpecBreak specBreak)
2352
        {
2353
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
2354
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
2355

    
2356
            if (upStreamObj != null &&
2357
                downStreamObj != null)
2358
            {
2359
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2360

    
2361
                if (targetLMConnector != null)
2362
                {
2363
                    foreach (var attribute in specBreak.ATTRIBUTES)
2364
                    {
2365
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
2366
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
2367
                        {
2368
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
2369
                            double[] point = GetSegmentPoint(upStreamObj, downStreamObj, targetLMConnector);
2370
                            Array array = null;
2371
                            if (point != null)
2372
                                array = new double[] { 0, point[0], point[1] };
2373
                            else
2374
                                array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
2375
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
2376

    
2377
                            if (_LmLabelPersist != null)
2378
                            {
2379
                                _LmLabelPersist.Commit();
2380
                                specBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2381
                                if (_LmLabelPersist.ModelItemObject != null)
2382
                                    specBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2383
                                specBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
2384
                                ReleaseCOMObjects(_LmLabelPersist);
2385
                            }
2386
                        }
2387
                    }
2388
                    ReleaseCOMObjects(targetLMConnector);
2389
                }
2390
            }
2391
        }
2392

    
2393
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
2394
        {
2395
            LMConnector targetConnector = null;
2396
            Symbol targetSymbol = targetObj as Symbol;
2397
            Symbol connectedSymbol = connectedObj as Symbol;
2398
            Line targetLine = targetObj as Line;
2399
            Line connectedLine = connectedObj as Line;
2400
            if (targetSymbol != null && connectedSymbol != null)
2401
            {
2402
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2403
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2404

    
2405
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
2406
                {
2407
                    if (connector.get_ItemStatus() != "Active")
2408
                        continue;
2409

    
2410
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2411
                    {
2412
                        targetConnector = connector;
2413
                        break;
2414
                    }
2415
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2416
                    {
2417
                        targetConnector = connector;
2418
                        break;
2419
                    }
2420
                }
2421

    
2422
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
2423
                {
2424
                    if (connector.get_ItemStatus() != "Active")
2425
                        continue;
2426

    
2427
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2428
                    {
2429
                        targetConnector = connector;
2430
                        break;
2431
                    }
2432
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2433
                    {
2434
                        targetConnector = connector;
2435
                        break;
2436
                    }
2437
                }
2438

    
2439
                ReleaseCOMObjects(targetLMSymbol);
2440
                ReleaseCOMObjects(connectedLMSymbol);
2441
            }
2442
            else if (targetLine != null && connectedLine != null)
2443
            {
2444
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2445
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2446

    
2447
                if (targetModelItem != null && targetModelItem.get_ItemStatus() == "Active" && connectedModelItem != null && connectedModelItem.get_ItemStatus() == "Active")
2448
                {
2449
                    foreach (LMRepresentation rep in targetModelItem.Representations)
2450
                    {
2451
                        if (targetConnector != null)
2452
                            break;
2453

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

    
2458
                            if (IsConnected(_LMConnector, connectedModelItem))
2459
                                targetConnector = _LMConnector;
2460
                            else
2461
                                ReleaseCOMObjects(_LMConnector);
2462
                        }
2463
                    }
2464

    
2465
                    ReleaseCOMObjects(targetModelItem);
2466
                }
2467
            }
2468
            else
2469
            {
2470
                LMSymbol connectedLMSymbol = null;
2471
                if (connectedSymbol != null)
2472
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2473
                else if (targetSymbol != null)
2474
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2475
                else
2476
                {
2477

    
2478
                }
2479
                LMModelItem targetModelItem = null;
2480
                if (targetLine != null)
2481
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2482
                else if (connectedLine != null)
2483
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2484
                else
2485
                {
2486

    
2487
                }
2488
                if (connectedLMSymbol != null && targetModelItem != null)
2489
                {
2490
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
2491
                    {
2492
                        if (connector.get_ItemStatus() != "Active")
2493
                            continue;
2494

    
2495
                        if (IsConnected(connector, targetModelItem))
2496
                        {
2497
                            targetConnector = connector;
2498
                            break;
2499
                        }
2500
                    }
2501

    
2502
                    if (targetConnector == null)
2503
                    {
2504
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
2505
                        {
2506
                            if (connector.get_ItemStatus() != "Active")
2507
                                continue;
2508

    
2509
                            if (IsConnected(connector, targetModelItem))
2510
                            {
2511
                                targetConnector = connector;
2512
                                break;
2513
                            }
2514
                        }
2515
                    }
2516
                }
2517

    
2518
            }
2519

    
2520
            return targetConnector;
2521
        }
2522

    
2523
        private double[] GetSegmentPoint(object targetObj, object connObj, LMConnector targetConnector)
2524
        {
2525
            double[] result = null;
2526
            if (Convert.ToBoolean(targetConnector.get_IsZeroLength()))
2527
            {
2528
                return GetConnectorVertices(targetConnector)[0];
2529
            }
2530
            else
2531
            {
2532
                if (targetObj.GetType() == typeof(Line) && connObj.GetType() == typeof(Line))
2533
                {
2534
                    Line line = connObj as Line;
2535
                    LMConnector connectedConnector = null;
2536
                    int connIndex = 0;
2537
                    LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2538
                    FindConnectedConnector(targetConnector, modelItem, ref connectedConnector, ref connIndex);
2539

    
2540
                    List<double[]> vertices = GetConnectorVertices(targetConnector);
2541

    
2542
                    ReleaseCOMObjects(modelItem);
2543
                    ReleaseCOMObjects(connectedConnector);
2544

    
2545
                    if (vertices.Count > 0)
2546
                    {
2547
                        if (connIndex == 1)
2548
                            return vertices[0];
2549
                        else if (connIndex == 2)
2550
                            return vertices[vertices.Count - 1];
2551
                    }
2552
                }
2553
                else
2554
                {
2555
                    Log.Write("error in GetSegemtPoint");
2556
                }
2557
            }
2558

    
2559
            return result;
2560
        }
2561

    
2562
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
2563
        {
2564
            bool result = false;
2565

    
2566
            foreach (LMRepresentation rep in modelItem.Representations)
2567
            {
2568
                if (result)
2569
                    break;
2570

    
2571
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2572
                {
2573
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2574

    
2575
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
2576
                        connector.ConnectItem1SymbolObject != null &&
2577
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2578
                    {
2579
                        result = true;
2580
                        ReleaseCOMObjects(_LMConnector);
2581
                        break;
2582
                    }
2583
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
2584
                        connector.ConnectItem2SymbolObject != null &&
2585
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2586
                    {
2587
                        result = true;
2588
                        ReleaseCOMObjects(_LMConnector);
2589
                        break;
2590
                    }
2591
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2592
                        connector.ConnectItem1SymbolObject != null &&
2593
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2594
                    {
2595
                        result = true;
2596
                        ReleaseCOMObjects(_LMConnector);
2597
                        break;
2598
                    }
2599
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2600
                        connector.ConnectItem2SymbolObject != null &&
2601
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2602
                    {
2603
                        result = true;
2604
                        ReleaseCOMObjects(_LMConnector);
2605
                        break;
2606
                    }
2607

    
2608
                    ReleaseCOMObjects(_LMConnector);
2609
                }
2610
            }
2611

    
2612

    
2613
            return result;
2614
        }
2615

    
2616
        private void FindConnectedConnector(LMConnector connector, LMModelItem modelItem, ref LMConnector connectedConnector, ref int connectorIndex)
2617
        {
2618
            foreach (LMRepresentation rep in modelItem.Representations)
2619
            {
2620
                if (connectedConnector != null)
2621
                    break;
2622

    
2623
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2624
                {
2625
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2626

    
2627
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
2628
                        connector.ConnectItem1SymbolObject != null &&
2629
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2630
                    {
2631
                        connectedConnector = _LMConnector;
2632
                        connectorIndex = 1;
2633
                        break;
2634
                    }
2635
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
2636
                        connector.ConnectItem2SymbolObject != null &&
2637
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2638
                    {
2639
                        connectedConnector = _LMConnector;
2640
                        connectorIndex = 2;
2641
                        break;
2642
                    }
2643
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2644
                        connector.ConnectItem1SymbolObject != null &&
2645
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2646
                    {
2647
                        connectedConnector = _LMConnector;
2648
                        connectorIndex = 1;
2649
                        break;
2650
                    }
2651
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2652
                        connector.ConnectItem2SymbolObject != null &&
2653
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2654
                    {
2655
                        connectedConnector = _LMConnector;
2656
                        connectorIndex = 2;
2657
                        break;
2658
                    }
2659

    
2660
                    if (connectedConnector == null)
2661
                        ReleaseCOMObjects(_LMConnector);
2662
                }
2663
            }
2664
        }
2665

    
2666
        /// <summary>
2667
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
2668
        /// </summary>
2669
        /// <param name="modelItemID1"></param>
2670
        /// <param name="modelItemID2"></param>
2671
        private void JoinRun(string modelId1, string modelId2, ref string survivorId)
2672
        {
2673
            try
2674
            {
2675
                LMModelItem modelItem1 = dataSource.GetModelItem(modelId1);
2676
                _LMAItem item1 = modelItem1.AsLMAItem();
2677
                LMModelItem modelItem2 = dataSource.GetModelItem(modelId2);
2678
                _LMAItem item2 = modelItem2.AsLMAItem();
2679

    
2680
                // item2가 item1으로 조인
2681
                _placement.PIDJoinRuns(ref item1, ref item2);
2682
                item1.Commit();
2683
                item2.Commit();
2684

    
2685
                string beforeID = string.Empty;
2686
                string afterID = string.Empty;
2687

    
2688
                if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() != "Active")
2689
                {
2690
                    beforeID = modelItem2.Id;
2691
                    afterID = modelItem1.Id;
2692
                    survivorId = afterID;
2693
                    LMAAttribute attribute = modelItem1.Attributes["FlowDirection"];
2694
                    if (attribute != null)
2695
                        attribute.set_Value("End 1 is upstream (Inlet)");
2696
                }
2697
                else if (modelItem1.get_ItemStatus() != "Active" && modelItem2.get_ItemStatus() == "Active")
2698
                {
2699
                    beforeID = modelItem1.Id;
2700
                    afterID = modelItem2.Id;
2701
                    survivorId = afterID;
2702
                    LMAAttribute attribute = modelItem2.Attributes["FlowDirection"];
2703
                    if (attribute != null)
2704
                        attribute.set_Value("End 1 is upstream (Inlet)");
2705
                }
2706
                else if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() == "Active")
2707
                    survivorId = null;
2708
                else
2709
                {
2710
                    Log.Write("잘못된 경우");
2711
                    survivorId = null;
2712
                }
2713

    
2714
                if (!string.IsNullOrEmpty(beforeID) && !string.IsNullOrEmpty(afterID))
2715
                {
2716
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, beforeID);
2717
                    foreach (var line in lines)
2718
                        line.SPPID.ModelItemId = afterID;
2719
                }
2720

    
2721
                ReleaseCOMObjects(modelItem1);
2722
                ReleaseCOMObjects(item1);
2723
                ReleaseCOMObjects(modelItem2);
2724
                ReleaseCOMObjects(item2);
2725
            }
2726
            catch (Exception ex)
2727
            {
2728
                Log.Write("Join Error");
2729
                Log.Write(ex.Message);
2730
            }
2731
        }
2732

    
2733
        private List<string> FindOtherModelItemBySymbolWhereTypePipeRun(LMSymbol symbol, string modelId)
2734
        {
2735
            List<string> modelItemIDs = new List<string>();
2736
            foreach (LMConnector connector in symbol.Avoid1Connectors)
2737
            {
2738
                LMModelItem modelItem = connector.ModelItemObject;
2739
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId)
2740
                    modelItemIDs.Add(modelItem.Id);
2741
                ReleaseCOMObjects(modelItem);
2742
                ReleaseCOMObjects(connector);
2743
            }
2744

    
2745
            foreach (LMConnector connector in symbol.Avoid2Connectors)
2746
            {
2747
                LMModelItem modelItem = connector.ModelItemObject;
2748
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId)
2749
                    modelItemIDs.Add(modelItem.Id);
2750
                ReleaseCOMObjects(modelItem);
2751
                ReleaseCOMObjects(connector);
2752
            }
2753

    
2754

    
2755
            List<string> result = new List<string>();
2756
            modelItemIDs = modelItemIDs.Distinct().ToList();
2757
            string originalName = GetSPPIDFileName(modelId);
2758
            foreach (var item in modelItemIDs)
2759
            {
2760
                string fileName = GetSPPIDFileName(item);
2761
                if (originalName == fileName)
2762
                    result.Add(item);
2763
                else
2764
                {
2765
                    LMConnector connector = GetLMConnectorOnlyOne(item);
2766
                    if (connector != null && Convert.ToBoolean(connector.get_IsZeroLength()))
2767
                    {
2768
                        result.Add(item);
2769
                        ReleaseCOMObjects(connector);
2770
                    }
2771
                }
2772
            }
2773
            
2774
            return result;
2775
        }
2776

    
2777
        /// <summary>
2778
        /// PipeRun의 좌표를 가져오는 메서드
2779
        /// </summary>
2780
        /// <param name="modelId"></param>
2781
        /// <returns></returns>
2782
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
2783
        {
2784
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2785
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2786

    
2787
            if (modelItem != null)
2788
            {
2789
                foreach (LMRepresentation rep in modelItem.Representations)
2790
                {
2791
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2792
                    {
2793
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2794
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2795
                        dynamic OID = rep.get_GraphicOID().ToString();
2796
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2797
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2798
                        int verticesCount = lineStringGeometry.VertexCount;
2799
                        double[] vertices = null;
2800
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2801
                        for (int i = 0; i < verticesCount; i++)
2802
                        {
2803
                            double x = 0;
2804
                            double y = 0;
2805
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2806
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
2807
                        }
2808
                    }
2809
                }
2810

    
2811
                ReleaseCOMObjects(modelItem);
2812
            }
2813

    
2814
            return connectorVertices;
2815
        }
2816

    
2817
        private List<double[]> GetConnectorVertices(LMConnector connector)
2818
        {
2819
            List<double[]> vertices = new List<double[]>();
2820
            if (connector != null)
2821
            {
2822
                dynamic OID = connector.get_GraphicOID().ToString();
2823
                DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2824
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2825
                int verticesCount = lineStringGeometry.VertexCount;
2826
                double[] value = null;
2827
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
2828
                for (int i = 0; i < verticesCount; i++)
2829
                {
2830
                    double x = 0;
2831
                    double y = 0;
2832
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2833
                    vertices.Add(new double[] { x, y });
2834
                }
2835
            }
2836
            return vertices;
2837
        }
2838

    
2839
        /// <summary>
2840
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
2841
        /// </summary>
2842
        /// <param name="connectorVertices"></param>
2843
        /// <param name="connX"></param>
2844
        /// <param name="connY"></param>
2845
        /// <returns></returns>
2846
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
2847
        {
2848
            double length = double.MaxValue;
2849
            LMConnector targetConnector = null;
2850
            foreach (var item in connectorVertices)
2851
            {
2852
                List<double[]> points = item.Value;
2853
                for (int i = 0; i < points.Count - 1; i++)
2854
                {
2855
                    double[] point1 = points[i];
2856
                    double[] point2 = points[i + 1];
2857
                    double x1 = Math.Min(point1[0], point2[0]);
2858
                    double y1 = Math.Min(point1[1], point2[1]);
2859
                    double x2 = Math.Max(point1[0], point2[0]);
2860
                    double y2 = Math.Max(point1[1], point2[1]);
2861

    
2862
                    if ((x1 <= connX && x2 >= connX) ||
2863
                        (y1 <= connY && y2 >= connY))
2864
                    {
2865
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
2866
                        if (length >= distance)
2867
                        {
2868
                            targetConnector = item.Key;
2869
                            length = distance;
2870
                        }
2871

    
2872
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
2873
                        if (length >= distance)
2874
                        {
2875
                            targetConnector = item.Key;
2876
                            length = distance;
2877
                        }
2878
                    }
2879
                }
2880
            }
2881

    
2882
            // 못찾았을때.
2883
            length = double.MaxValue;
2884
            if (targetConnector == null)
2885
            {
2886
                foreach (var item in connectorVertices)
2887
                {
2888
                    List<double[]> points = item.Value;
2889

    
2890
                    foreach (double[] point in points)
2891
                    {
2892
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2893
                        if (length >= distance)
2894
                        {
2895
                            targetConnector = item.Key;
2896
                            length = distance;
2897
                        }
2898
                    }
2899
                }
2900
            }
2901

    
2902
            return targetConnector;
2903
        }
2904

    
2905
        private LMConnector FindTargetLMConnectorForBranch(Line line, Line targetLine, ref double x, ref double y)
2906
        {
2907
            Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
2908
            if (vertices.Count == 0)
2909
                return null;
2910

    
2911
            double length = double.MaxValue;
2912
            LMConnector targetConnector = null;
2913
            double[] resultPoint = null;
2914
            List<double[]> targetVertices = null;
2915

    
2916
            // Vertices 포인트에 제일 가까운곳
2917
            foreach (var item in vertices)
2918
            {
2919
                List<double[]> points = item.Value;
2920
                for (int i = 0; i < points.Count; i++)
2921
                {
2922
                    double[] point = points[i];
2923
                    double tempX = point[0];
2924
                    double tempY = point[1];
2925

    
2926
                    double distance = SPPIDUtil.CalcPointToPointdDistance(tempX, tempY, x, y);
2927
                    if (length >= distance)
2928
                    {
2929
                        targetConnector = item.Key;
2930
                        length = distance;
2931
                        resultPoint = point;
2932
                        targetVertices = item.Value;
2933
                    }
2934
                }
2935
            }
2936

    
2937
            // Vertices Cross에 제일 가까운곳
2938
            foreach (var item in vertices)
2939
            {
2940
                List<double[]> points = item.Value;
2941
                for (int i = 0; i < points.Count - 1; i++)
2942
                {
2943
                    double[] point1 = points[i];
2944
                    double[] point2 = points[i + 1];
2945

    
2946
                    double maxLineX = Math.Max(point1[0], point2[0]);
2947
                    double minLineX = Math.Min(point1[0], point2[0]);
2948
                    double maxLineY = Math.Max(point1[1], point2[1]);
2949
                    double minLineY = Math.Min(point1[1], point2[1]);
2950

    
2951
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
2952

    
2953
                    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]);
2954
                    if (crossingPoint != null)
2955
                    {
2956
                        double distance = SPPIDUtil.CalcPointToPointdDistance(crossingPoint[0], crossingPoint[1], x, y);
2957
                        if (length >= distance)
2958
                        {
2959
                            if (slope == SlopeType.Slope &&
2960
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
2961
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2962
                            {
2963
                                targetConnector = item.Key;
2964
                                length = distance;
2965
                                resultPoint = crossingPoint;
2966
                                targetVertices = item.Value;
2967
                            }
2968
                            else if (slope == SlopeType.HORIZONTAL &&
2969
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
2970
                            {
2971
                                targetConnector = item.Key;
2972
                                length = distance;
2973
                                resultPoint = crossingPoint;
2974
                                targetVertices = item.Value;
2975
                            }
2976
                            else if (slope == SlopeType.VERTICAL &&
2977
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2978
                            {
2979
                                targetConnector = item.Key;
2980
                                length = distance;
2981
                                resultPoint = crossingPoint;
2982
                                targetVertices = item.Value;
2983
                            }
2984
                        }
2985
                    }
2986
                }
2987
            }
2988

    
2989
            foreach (var item in vertices)
2990
                if (item.Key != null && item.Key != targetConnector)
2991
                    ReleaseCOMObjects(item.Key);
2992

    
2993
            if (SPPIDUtil.IsBranchLine(line, targetLine))
2994
            {
2995
                double tempResultX = resultPoint[0];
2996
                double tempResultY = resultPoint[1];
2997
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
2998

    
2999
                GridSetting gridSetting = GridSetting.GetInstance();
3000

    
3001
                for (int i = 0; i < targetVertices.Count; i++)
3002
                {
3003
                    double[] point = targetVertices[i];
3004
                    double tempX = targetVertices[i][0];
3005
                    double tempY = targetVertices[i][1];
3006
                    SPPIDUtil.ConvertGridPoint(ref tempX, ref tempY);
3007
                    if (tempX == tempResultX && tempY == tempResultY)
3008
                    {
3009
                        if (i == 0)
3010
                        {
3011
                            LMSymbol connSymbol = targetConnector.ConnectItem1SymbolObject;
3012
                            bool containZeroLength = false;
3013
                            if (connSymbol != null)
3014
                            {
3015
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
3016
                                {
3017
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
3018
                                        containZeroLength = true;
3019
                                }
3020
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
3021
                                {
3022
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
3023
                                        containZeroLength = true;
3024
                                }
3025
                            }
3026

    
3027
                            if (connSymbol == null ||
3028
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
3029
                                containZeroLength)
3030
                            {
3031
                                bool bCalcX = false;
3032
                                bool bCalcY = false;
3033
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3034
                                    bCalcX = true;
3035
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
3036
                                    bCalcY = true;
3037
                                else
3038
                                {
3039
                                    bCalcX = true;
3040
                                    bCalcY = true;
3041
                                }
3042

    
3043
                                if (bCalcX)
3044
                                {
3045
                                    double nextX = targetVertices[i + 1][0];
3046
                                    double newX = 0;
3047
                                    if (nextX > tempX)
3048
                                    {
3049
                                        newX = tempX + gridSetting.Length;
3050
                                        if (newX > nextX)
3051
                                            newX = (point[0] + nextX) / 2;
3052
                                    }
3053
                                    else
3054
                                    {
3055
                                        newX = tempX - gridSetting.Length;
3056
                                        if (newX < nextX)
3057
                                            newX = (point[0] + nextX) / 2;
3058
                                    }
3059
                                    resultPoint = new double[] { newX, resultPoint[1] };
3060
                                }
3061

    
3062
                                if (bCalcY)
3063
                                {
3064
                                    double nextY = targetVertices[i + 1][1];
3065
                                    double newY = 0;
3066
                                    if (nextY > tempY)
3067
                                    {
3068
                                        newY = tempY + gridSetting.Length;
3069
                                        if (newY > nextY)
3070
                                            newY = (point[1] + nextY) / 2;
3071
                                    }
3072
                                    else
3073
                                    {
3074
                                        newY = tempY - gridSetting.Length;
3075
                                        if (newY < nextY)
3076
                                            newY = (point[1] + nextY) / 2;
3077
                                    }
3078
                                    resultPoint = new double[] { resultPoint[0], newY };
3079
                                }
3080
                            }
3081
                        }
3082
                        else if (i == targetVertices.Count - 1)
3083
                        {
3084
                            LMSymbol connSymbol = targetConnector.ConnectItem2SymbolObject;
3085
                            bool containZeroLength = false;
3086
                            if (connSymbol != null)
3087
                            {
3088
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
3089
                                {
3090
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
3091
                                        containZeroLength = true;
3092
                                }
3093
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
3094
                                {
3095
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
3096
                                        containZeroLength = true;
3097
                                }
3098
                            }
3099

    
3100
                            if (connSymbol == null ||
3101
                                 (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
3102
                                containZeroLength)
3103
                            {
3104
                                bool bCalcX = false;
3105
                                bool bCalcY = false;
3106
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3107
                                    bCalcX = true;
3108
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
3109
                                    bCalcY = true;
3110
                                else
3111
                                {
3112
                                    bCalcX = true;
3113
                                    bCalcY = true;
3114
                                }
3115

    
3116
                                if (bCalcX)
3117
                                {
3118
                                    double nextX = targetVertices[i - 1][0];
3119
                                    double newX = 0;
3120
                                    if (nextX > tempX)
3121
                                    {
3122
                                        newX = tempX + gridSetting.Length;
3123
                                        if (newX > nextX)
3124
                                            newX = (point[0] + nextX) / 2;
3125
                                    }
3126
                                    else
3127
                                    {
3128
                                        newX = tempX - gridSetting.Length;
3129
                                        if (newX < nextX)
3130
                                            newX = (point[0] + nextX) / 2;
3131
                                    }
3132
                                    resultPoint = new double[] { newX, resultPoint[1] };
3133
                                }
3134

    
3135
                                if (bCalcY)
3136
                                {
3137
                                    double nextY = targetVertices[i - 1][1];
3138
                                    double newY = 0;
3139
                                    if (nextY > tempY)
3140
                                    {
3141
                                        newY = tempY + gridSetting.Length;
3142
                                        if (newY > nextY)
3143
                                            newY = (point[1] + nextY) / 2;
3144
                                    }
3145
                                    else
3146
                                    {
3147
                                        newY = tempY - gridSetting.Length;
3148
                                        if (newY < nextY)
3149
                                            newY = (point[1] + nextY) / 2;
3150
                                    }
3151
                                    resultPoint = new double[] { resultPoint[0], newY };
3152
                                }
3153
                            }
3154
                        }
3155
                        break;
3156
                    }
3157
                }
3158
            }
3159

    
3160
            x = resultPoint[0];
3161
            y = resultPoint[1];
3162

    
3163
            return targetConnector;
3164
        }
3165

    
3166
        private LMConnector GetLMConnectorOnlyOne(string modelItemID)
3167
        {
3168
            LMConnector result = null;
3169
            List<LMConnector> connectors = new List<LMConnector>();
3170
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
3171

    
3172
            if (modelItem != null)
3173
            {
3174
                foreach (LMRepresentation rep in modelItem.Representations)
3175
                {
3176
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3177
                        connectors.Add(dataSource.GetConnector(rep.Id));
3178
                }
3179

    
3180
                ReleaseCOMObjects(modelItem);
3181
            }
3182

    
3183
            if (connectors.Count == 1)
3184
                result = connectors[0];
3185
            else
3186
                foreach (var item in connectors)
3187
                    ReleaseCOMObjects(item);
3188

    
3189
            return result;
3190
        }
3191

    
3192
        /// <summary>
3193
        /// Line Number Symbol을 실제로 Modeling하는 메서드
3194
        /// </summary>
3195
        /// <param name="lineNumber"></param>
3196
        private void LineNumberModelingOnlyOne(Line line)
3197
        {
3198
            LineNumber lineNumber = document.LINENUMBERS.Find(x => x.CONNLINE == line.UID);
3199
            if (lineNumber != null)
3200
            {
3201
                LMConnector connectedLMConnector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
3202
                if (connectedLMConnector != null)
3203
                {
3204
                    double x = 0;
3205
                    double y = 0;
3206
                    CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
3207

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

    
3211
                    if (_LmLabelPresist != null)
3212
                    {
3213
                        _LmLabelPresist.Commit();
3214
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
3215
                        ReleaseCOMObjects(_LmLabelPresist);
3216
                    }
3217
                }
3218
            }
3219
        }
3220

    
3221
        private void LineNumberModeling(LineNumber lineNumber)
3222
        {
3223
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
3224
            if (line != null)
3225
            {
3226
                double x = 0;
3227
                double y = 0;
3228
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
3229

    
3230
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
3231
                LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, x, y);
3232
                if (connectedLMConnector != null)
3233
                {
3234
                    Array points = new double[] { 0, x, y };
3235
                    LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: false);
3236

    
3237
                    if (_LmLabelPresist != null)
3238
                    {
3239
                        _LmLabelPresist.Commit();
3240
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
3241
                        ReleaseCOMObjects(_LmLabelPresist);
3242
                    }
3243
                }
3244

    
3245
                foreach (var item in connectorVertices)
3246
                    ReleaseCOMObjects(item.Key);
3247
            }
3248
        }
3249
        /// <summary>
3250
        /// Flow Mark Modeling
3251
        /// </summary>
3252
        /// <param name="line"></param>
3253
        private void FlowMarkModeling(Line line)
3254
        {
3255
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
3256
            {
3257
                LMConnector connector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
3258
                if (connector != null)
3259
                {
3260
                    string mappingPath = _ETCSetting.FlowMarkSymbolPath;
3261
                    List<double[]> vertices = GetConnectorVertices(connector);
3262
                    vertices = vertices.FindAll(x => x[0] > 0 && x[1] > 0);
3263
                    double[] point = vertices[vertices.Count - 1];
3264
                    Array array = new double[] { 0, point[0], point[1] };
3265
                    LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mappingPath, ref array, LabeledItem: connector.AsLMRepresentation());
3266
                    if (_LMLabelPersist != null)
3267
                    {
3268
                        _LMLabelPersist.Commit();
3269
                        ReleaseCOMObjects(_LMLabelPersist);
3270
                    }
3271
                        
3272
                }
3273
            }
3274
        }
3275

    
3276
        /// <summary>
3277
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
3278
        /// </summary>
3279
        /// <param name="lineNumber"></param>
3280
        private void InputLineNumberAttribute(LineNumber lineNumber, List<string> endLine)
3281
        {
3282
            foreach (LineRun run in lineNumber.RUNS)
3283
            {
3284
                foreach (var item in run.RUNITEMS)
3285
                {
3286
                    if (item.GetType() == typeof(Symbol))
3287
                    {
3288
                        Symbol symbol = item as Symbol;
3289
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3290
                        if (_LMSymbol != null)
3291
                        {
3292
                            LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
3293

    
3294
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3295
                            {
3296
                                foreach (var attribute in lineNumber.ATTRIBUTES)
3297
                                {
3298
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
3299
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3300
                                    {
3301
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
3302
                                        if (_LMAAttribute != null)
3303
                                        {
3304
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3305
                                                _LMAAttribute.set_Value(attribute.VALUE);
3306
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
3307
                                                _LMAAttribute.set_Value(attribute.VALUE);
3308
                                        }
3309
                                    }
3310
                                }
3311
                                _LMModelItem.Commit();
3312
                            }
3313
                            if (_LMModelItem != null)
3314
                                ReleaseCOMObjects(_LMModelItem);
3315
                        }
3316
                        if (_LMSymbol != null)
3317
                            ReleaseCOMObjects(_LMSymbol);
3318
                    }
3319
                    else if (item.GetType() == typeof(Line))
3320
                    {
3321
                        Line line = item as Line;
3322
                        if (line != null && !endLine.Contains(line.SPPID.ModelItemId))
3323
                        {
3324
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3325
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3326
                            {
3327
                                foreach (var attribute in lineNumber.ATTRIBUTES)
3328
                                {
3329
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
3330
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3331
                                    {
3332
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
3333
                                        if (_LMAAttribute != null)
3334
                                        {
3335
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3336
                                                _LMAAttribute.set_Value(attribute.VALUE);
3337
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
3338
                                                _LMAAttribute.set_Value(attribute.VALUE);
3339

    
3340
                                        }
3341
                                    }
3342
                                }
3343
                                _LMModelItem.Commit();
3344
                            }
3345
                            if (_LMModelItem != null)
3346
                                ReleaseCOMObjects(_LMModelItem);
3347
                            endLine.Add(line.SPPID.ModelItemId);
3348
                        }
3349
                    }
3350
                }
3351
            }
3352
        }
3353

    
3354
        /// <summary>
3355
        /// Symbol Attribute 입력 메서드
3356
        /// </summary>
3357
        /// <param name="item"></param>
3358
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
3359
        {
3360
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
3361
            string sRep = null;
3362
            if (targetItem.GetType() == typeof(Symbol))
3363
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
3364
            else if (targetItem.GetType() == typeof(Equipment))
3365
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
3366

    
3367
            if (!string.IsNullOrEmpty(sRep))
3368
            {
3369
                LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
3370
                LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
3371
                LMAAttributes _Attributes = _LMModelItem.Attributes;
3372
                
3373
                foreach (var item in targetAttributes)
3374
                {
3375
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
3376
                    if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
3377
                    {
3378
                        LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
3379
                        if (_Attribute != null)
3380
                        {
3381
                            object associItem = SPPIDUtil.FindObjectByUID(document, item.ASSOCITEM);
3382
                            if (associItem != null)
3383
                            {
3384
                                if (associItem.GetType() == typeof(Text))
3385
                                {
3386
                                    Text text = associItem as Text;
3387
                                    text.SPPID.RepresentationId = "Attribute";
3388
                                }
3389
                                else if (associItem.GetType() == typeof(Note))
3390
                                {
3391
                                    Note note = associItem as Note;
3392
                                    note.SPPID.RepresentationId = "Attribute";
3393
                                }
3394
                            }
3395
                            _Attribute.set_Value(item.VALUE);
3396
                        }
3397
                            
3398
                    }
3399
                }
3400
                _LMModelItem.Commit();
3401

    
3402
                ReleaseCOMObjects(_Attributes);
3403
                ReleaseCOMObjects(_LMModelItem);
3404
                ReleaseCOMObjects(_LMSymbol);
3405
            }
3406
        }
3407

    
3408
        /// <summary>
3409
        /// Input SpecBreak Attribute
3410
        /// </summary>
3411
        /// <param name="specBreak"></param>
3412
        private void InputSpecBreakAttribute(SpecBreak specBreak)
3413
        {
3414
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
3415
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
3416

    
3417
            if (upStreamObj != null &&
3418
                downStreamObj != null)
3419
            {
3420
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
3421

    
3422
                if (targetLMConnector != null)
3423
                {
3424
                    foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
3425
                    {
3426
                        string symbolPath = _LMLabelPersist.get_FileName();
3427
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
3428
                        if (mapping != null)
3429
                        {
3430
                            BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
3431
                            if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3432
                            {
3433
                                string[] values = attribute.VALUE.Split(new char[] { ',' });
3434
                                if (values.Length == 2)
3435
                                {
3436
                                    string upStreamValue = values[0];
3437
                                    string downStreamValue = values[1];
3438

    
3439
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
3440
                                }
3441
                            }
3442
                        }
3443
                    }
3444

    
3445
                    ReleaseCOMObjects(targetLMConnector);
3446
                }
3447
            }
3448

    
3449

    
3450
            #region 내부에서만 쓰는 메서드
3451
            void InputAttributeForSpecBreak(object _upStreamObj, object _downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
3452
            {
3453
                Symbol upStreamSymbol = _upStreamObj as Symbol;
3454
                Line upStreamLine = _upStreamObj as Line;
3455
                Symbol downStreamSymbol = _downStreamObj as Symbol;
3456
                Line downStreamLine = _downStreamObj as Line;
3457
                // 둘다 Line일 경우
3458
                if (upStreamLine != null && downStreamLine != null)
3459
                {
3460
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3461
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3462
                }
3463
                // 둘다 Symbol일 경우
3464
                else if (upStreamSymbol != null && downStreamSymbol != null)
3465
                {
3466
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
3467
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3468
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3469

    
3470
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3471
                    {
3472
                        if (connector.get_ItemStatus() != "Active")
3473
                            continue;
3474

    
3475
                        if (connector.Id != zeroLenthConnector.Id)
3476
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3477
                    }
3478

    
3479
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3480
                    {
3481
                        if (connector.get_ItemStatus() != "Active")
3482
                            continue;
3483

    
3484
                        if (connector.Id != zeroLenthConnector.Id)
3485
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3486
                    }
3487

    
3488
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3489
                    {
3490
                        if (connector.get_ItemStatus() != "Active")
3491
                            continue;
3492

    
3493
                        if (connector.Id != zeroLenthConnector.Id)
3494
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3495
                    }
3496

    
3497
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3498
                    {
3499
                        if (connector.get_ItemStatus() != "Active")
3500
                            continue;
3501

    
3502
                        if (connector.Id != zeroLenthConnector.Id)
3503
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3504
                    }
3505

    
3506
                    ReleaseCOMObjects(zeroLenthConnector);
3507
                    ReleaseCOMObjects(upStreamLMSymbol);
3508
                    ReleaseCOMObjects(downStreamLMSymbol);
3509
                }
3510
                else if (upStreamSymbol != null && downStreamLine != null)
3511
                {
3512
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
3513
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3514
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3515

    
3516
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3517
                    {
3518
                        if (connector.get_ItemStatus() != "Active")
3519
                            continue;
3520

    
3521
                        if (connector.Id == zeroLenthConnector.Id)
3522
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3523
                    }
3524

    
3525
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3526
                    {
3527
                        if (connector.get_ItemStatus() != "Active")
3528
                            continue;
3529

    
3530
                        if (connector.Id == zeroLenthConnector.Id)
3531
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3532
                    }
3533

    
3534
                    ReleaseCOMObjects(zeroLenthConnector);
3535
                    ReleaseCOMObjects(upStreamLMSymbol);
3536
                }
3537
                else if (upStreamLine != null && downStreamSymbol != null)
3538
                {
3539
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
3540
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3541
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3542

    
3543
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3544
                    {
3545
                        if (connector.get_ItemStatus() != "Active")
3546
                            continue;
3547

    
3548
                        if (connector.Id == zeroLenthConnector.Id)
3549
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3550
                    }
3551

    
3552
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3553
                    {
3554
                        if (connector.get_ItemStatus() != "Active")
3555
                            continue;
3556

    
3557
                        if (connector.Id == zeroLenthConnector.Id)
3558
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3559
                    }
3560

    
3561
                    ReleaseCOMObjects(zeroLenthConnector);
3562
                    ReleaseCOMObjects(downStreamLMSymbol);
3563
                }
3564
            }
3565

    
3566
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
3567
            {
3568
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3569
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3570
                {
3571
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3572
                    if (_LMAAttribute != null)
3573
                    {
3574
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3575
                            _LMAAttribute.set_Value(value);
3576
                        else if (_LMAAttribute.get_Value() != value)
3577
                            _LMAAttribute.set_Value(value);
3578
                    }
3579

    
3580
                    _LMModelItem.Commit();
3581
                }
3582
                if (_LMModelItem != null)
3583
                    ReleaseCOMObjects(_LMModelItem);
3584
            }
3585

    
3586
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
3587
            {
3588
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
3589
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3590
                {
3591
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3592
                    if (_LMAAttribute != null)
3593
                    {
3594
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3595
                            _LMAAttribute.set_Value(value);
3596
                        else if (_LMAAttribute.get_Value() != value)
3597
                            _LMAAttribute.set_Value(value);
3598
                    }
3599

    
3600
                    _LMModelItem.Commit();
3601
                }
3602
                if (_LMModelItem != null)
3603
                    ReleaseCOMObjects(_LMModelItem);
3604
            }
3605
            #endregion
3606
        }
3607

    
3608
        /// <summary>
3609
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
3610
        /// </summary>
3611
        /// <param name="text"></param>
3612
        private void TextModeling(Text text)
3613
        {
3614
            LMSymbol _LMSymbol = null;
3615
            LMConnector connectedLMConnector = null;
3616
            //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
3617
            if (text.ASSOCIATION)
3618
            {
3619
                object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
3620
                if (owner != null && owner.GetType() == typeof(Symbol))
3621
                {
3622
                    Symbol symbol = owner as Symbol;
3623
                    _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3624
                    if (_LMSymbol != null)
3625
                    {
3626
                        BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3627
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3628
                        {
3629
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3630

    
3631
                            if (mapping != null)
3632
                            {
3633
                                double x = 0;
3634
                                double y = 0;
3635

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

    
3639
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3640
                                if (_LMLabelPersist != null)
3641
                                {
3642
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
3643
                                    _LMLabelPersist.Commit();
3644
                                    ReleaseCOMObjects(_LMLabelPersist);
3645
                                }
3646
                            }
3647
                        }
3648
                    }
3649
                }
3650
                else if (owner != null && owner.GetType() == typeof(Line))
3651
                {
3652
                    Line line = owner as Line;
3653
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
3654
                    connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
3655

    
3656
                    if (connectedLMConnector != null)
3657
                    {
3658
                        BaseModel.Attribute attribute = line.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3659
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3660
                        {
3661
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3662

    
3663
                            if (mapping != null)
3664
                            {
3665
                                double x = 0;
3666
                                double y = 0;
3667

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

    
3671
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3672
                                if (_LMLabelPersist != null)
3673
                                {
3674
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
3675
                                    _LMLabelPersist.Commit();
3676
                                    ReleaseCOMObjects(_LMLabelPersist);
3677
                                }
3678
                            }
3679
                        }
3680
                    }
3681
                }
3682
            }
3683
            else
3684
            {
3685
                LMItemNote _LMItemNote = null;
3686
                LMAAttribute _LMAAttribute = null;
3687

    
3688
                double x = 0;
3689
                double y = 0;
3690
                double angle = text.ANGLE;
3691
                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
3692

    
3693
                _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, Rotation: angle);
3694
                if (_LMSymbol != null)
3695
                {
3696
                    _LMSymbol.Commit();
3697
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3698
                    if (_LMItemNote != null)
3699
                    {
3700
                        _LMItemNote.Commit();
3701
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3702
                        if (_LMAAttribute != null)
3703
                        {
3704
                            _LMAAttribute.set_Value(text.VALUE);
3705
                            text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3706
                            _LMItemNote.Commit();
3707

    
3708
                            if (_LMAAttribute != null)
3709
                                ReleaseCOMObjects(_LMAAttribute);
3710
                            if (_LMItemNote != null)
3711
                                ReleaseCOMObjects(_LMItemNote);
3712
                        }
3713
                    }
3714
                }
3715
            }
3716
            if (_LMSymbol != null)
3717
                ReleaseCOMObjects(_LMSymbol);
3718
        }
3719

    
3720
        /// <summary>
3721
        /// Note Modeling
3722
        /// </summary>
3723
        /// <param name="note"></param>
3724
        private void NoteModeling(Note note)
3725
        {
3726
            LMSymbol _LMSymbol = null;
3727
            LMItemNote _LMItemNote = null;
3728
            LMAAttribute _LMAAttribute = null;
3729

    
3730
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
3731
            {
3732
                double x = 0;
3733
                double y = 0;
3734

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

    
3737
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
3738
                if (_LMSymbol != null)
3739
                {
3740
                    _LMSymbol.Commit();
3741
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3742
                    if (_LMItemNote != null)
3743
                    {
3744
                        _LMItemNote.Commit();
3745
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3746
                        if (_LMAAttribute != null)
3747
                        {
3748
                            _LMAAttribute.set_Value(note.VALUE);
3749
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3750
                            _LMItemNote.Commit();
3751
                        }
3752
                    }
3753
                }
3754
            }
3755

    
3756
            if (_LMAAttribute != null)
3757
                ReleaseCOMObjects(_LMAAttribute);
3758
            if (_LMItemNote != null)
3759
                ReleaseCOMObjects(_LMItemNote);
3760
            if (_LMSymbol != null)
3761
                ReleaseCOMObjects(_LMSymbol);
3762
        }
3763

    
3764
        private void JoinRunBySameType(string modelItemId, ref string survivorId)
3765
        {
3766
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
3767
            if (modelItem != null)
3768
            {
3769
                foreach (LMRepresentation rep in modelItem.Representations)
3770
                {
3771
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3772
                    {
3773
                        LMConnector connector = dataSource.GetConnector(rep.Id);
3774
                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
3775
                        {
3776
                            LMSymbol symbol = connector.ConnectItem1SymbolObject;
3777
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
3778
                            if (modelItemIds.Count == 1)
3779
                            {
3780
                                string joinModelItemId = modelItemIds[0];
3781
                                JoinRun(joinModelItemId, modelItemId, ref survivorId);
3782
                                if (survivorId != null)
3783
                                    break;
3784
                            }
3785
                        }
3786
                        if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
3787
                        {
3788
                            LMSymbol symbol = connector.ConnectItem2SymbolObject;
3789
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
3790
                            if (modelItemIds.Count == 1)
3791
                            {
3792
                                string joinModelItemId = modelItemIds[0];
3793
                                JoinRun(joinModelItemId, modelItemId, ref survivorId);
3794
                                if (survivorId != null)
3795
                                    break;
3796
                            }
3797
                        }
3798
                    }
3799
                }
3800
            }
3801
        }
3802

    
3803
        /// <summary>
3804
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
3805
        /// </summary>
3806
        /// <param name="x"></param>
3807
        /// <param name="y"></param>
3808
        /// <param name="originX"></param>
3809
        /// <param name="originY"></param>
3810
        /// <param name="SPPIDLabelLocation"></param>
3811
        /// <param name="location"></param>
3812
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
3813
        {
3814
            if (location == Location.None)
3815
            {
3816
                x = originX;
3817
                y = originY;
3818
            }
3819
            else
3820
            {
3821
                if (location.HasFlag(Location.Center))
3822
                {
3823
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
3824
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
3825
                }
3826

    
3827
                if (location.HasFlag(Location.Left))
3828
                    x = SPPIDLabelLocation.X1;
3829
                else if (location.HasFlag(Location.Right))
3830
                    x = SPPIDLabelLocation.X2;
3831

    
3832
                if (location.HasFlag(Location.Down))
3833
                    y = SPPIDLabelLocation.Y1;
3834
                else if (location.HasFlag(Location.Up))
3835
                    y = SPPIDLabelLocation.Y2;
3836
            }
3837
        }
3838

    
3839
        /// <summary>
3840
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
3841
        /// 1. Angle Valve
3842
        /// 2. 3개로 이루어진 Symbol Group
3843
        /// </summary>
3844
        /// <returns></returns>
3845
        private List<Symbol> GetPrioritySymbol()
3846
        {
3847
            DataTable symbolTable = document.SymbolTable;
3848
            // List에 순서대로 쌓는다.
3849
            List<Symbol> symbols = new List<Symbol>();
3850

    
3851
            // Angle Valve 부터
3852
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
3853
            {
3854
                if (!symbols.Contains(symbol))
3855
                {
3856
                    double originX = 0;
3857
                    double originY = 0;
3858

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

    
3863
                    SlopeType slopeType1 = SlopeType.None;
3864
                    SlopeType slopeType2 = SlopeType.None;
3865
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
3866
                    {
3867
                        double connectorX = 0;
3868
                        double connectorY = 0;
3869
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
3870
                        if (slopeType1 == SlopeType.None)
3871
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3872
                        else
3873
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3874
                    }
3875

    
3876
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
3877
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
3878
                        symbols.Add(symbol);
3879
                }
3880
            }
3881

    
3882
            List<Symbol> tempSymbols = new List<Symbol>();
3883
            // Conn 갯수 기준
3884
            foreach (var item in document.SYMBOLS)
3885
            {
3886
                if (!symbols.Contains(item))
3887
                    tempSymbols.Add(item);
3888
            }
3889
            tempSymbols.Sort(SortSymbolPriority);
3890
            symbols.AddRange(tempSymbols);
3891

    
3892
            return symbols;
3893
        }
3894

    
3895
        private void SetPriorityLine(List<Line> lines)
3896
        {
3897
            lines.Sort(SortLinePriority);
3898

    
3899
            int SortLinePriority(Line a, Line b)
3900
            {
3901
                // Branch 없는것부터
3902
                int branchRetval = CompareBranchLine(a, b);
3903
                if (branchRetval != 0)
3904
                {
3905
                    return branchRetval;
3906
                }
3907
                else
3908
                {
3909
                    // Symbol 연결 갯수
3910
                    int connSymbolRetval = CompareConnSymbol(a, b);
3911
                    if (connSymbolRetval != 0)
3912
                    {
3913
                        return connSymbolRetval;
3914
                    }
3915
                    else
3916
                    {
3917
                        // 아이템 연결 갯수(심볼, Line이면서 Not Branch)
3918
                        int connItemRetval = CompareConnItem(a, b);
3919
                        if (connItemRetval != 0)
3920
                        {
3921
                            return connItemRetval;
3922
                        }
3923
                        else
3924
                        {
3925
                            // ConnectedItem이 없는것
3926
                            int noneConnRetval = CompareNoneConn(a, b);
3927
                            if (noneConnRetval != 0)
3928
                            {
3929
                                return noneConnRetval;
3930
                            }
3931
                            else
3932
                            {
3933

    
3934
                            }
3935
                        }
3936
                    }
3937
                }
3938

    
3939
                return 0;
3940
            }
3941

    
3942
            int CompareNotSegmentLine(Line a, Line b)
3943
            {
3944
                List<Connector> connectorsA = a.CONNECTORS
3945
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3946
                    .ToList();
3947

    
3948
                List<Connector> connectorsB = b.CONNECTORS
3949
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3950
                    .ToList();
3951

    
3952
                // 오름차순
3953
                return connectorsB.Count.CompareTo(connectorsA.Count);
3954
            }
3955

    
3956
            int CompareConnSymbol(Line a, Line b)
3957
            {
3958
                List<Connector> connectorsA = a.CONNECTORS
3959
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3960
                    .ToList();
3961

    
3962
                List<Connector> connectorsB = b.CONNECTORS
3963
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3964
                    .ToList();
3965

    
3966
                // 오름차순
3967
                return connectorsB.Count.CompareTo(connectorsA.Count);
3968
            }
3969

    
3970
            int CompareConnItem(Line a, Line b)
3971
            {
3972
                List<Connector> connectorsA = a.CONNECTORS
3973
                    .Where(conn => conn.ConnectedObject != null && 
3974
                    (conn.ConnectedObject.GetType() == typeof(Symbol) || 
3975
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
3976
                    .ToList();
3977

    
3978
                List<Connector> connectorsB = b.CONNECTORS
3979
                    .Where(conn => conn.ConnectedObject != null &&
3980
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
3981
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
3982
                    .ToList();
3983

    
3984
                // 오름차순
3985
                return connectorsB.Count.CompareTo(connectorsA.Count);
3986
            }
3987

    
3988
            int CompareBranchLine(Line a, Line b)
3989
            {
3990
                List<Connector> connectorsA = a.CONNECTORS
3991
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
3992
                    .ToList();
3993
                List<Connector> connectorsB = b.CONNECTORS
3994
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
3995
                    .ToList();
3996

    
3997
                // 내림차순
3998
                return connectorsA.Count.CompareTo(connectorsB.Count);
3999
            }
4000

    
4001
            int CompareNoneConn(Line a, Line b)
4002
            {
4003
                List<Connector> connectorsA = a.CONNECTORS
4004
                    .Where(conn => conn.ConnectedObject == null)
4005
                    .ToList();
4006

    
4007
                List<Connector> connectorsB = b.CONNECTORS
4008
                    .Where(conn => conn.ConnectedObject == null)
4009
                    .ToList();
4010

    
4011
                // 오름차순
4012
                return connectorsB.Count.CompareTo(connectorsA.Count);
4013
            }
4014
        }
4015

    
4016
        private void SortBranchLines()
4017
        {
4018
            BranchLines.Sort(SortBranchLine);
4019
            int SortBranchLine(Line a, Line b)
4020
            {
4021
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
4022
                 x.ConnectedObject.GetType() == typeof(Line) &&
4023
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
4024
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
4025

    
4026
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
4027
                 x.ConnectedObject.GetType() == typeof(Line) &&
4028
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
4029
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
4030

    
4031
                // 내림차순
4032
                return countA.CompareTo(countB);
4033
            }
4034
        }
4035

    
4036
        private static int SortSymbolPriority(Symbol a, Symbol b)
4037
        {
4038
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
4039
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
4040
            int retval = countB.CompareTo(countA);
4041
            if (retval != 0)
4042
                return retval;
4043
            else
4044
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
4045
        }
4046

    
4047
        private string GetSPPIDFileName(LMModelItem modelItem)
4048
        {
4049
            string symbolPath = null;
4050
            foreach (LMRepresentation rep in modelItem.Representations)
4051
            {
4052
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
4053
                {
4054
                    symbolPath = rep.get_FileName();
4055
                    break;
4056
                }
4057
            }
4058
            return symbolPath;
4059
        }
4060

    
4061
        private string GetSPPIDFileName(string modelItemId)
4062
        {
4063
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
4064
            string symbolPath = null;
4065
            foreach (LMRepresentation rep in modelItem.Representations)
4066
            {
4067
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
4068
                {
4069
                    symbolPath = rep.get_FileName();
4070
                    break;
4071
                }
4072
            }
4073
            ReleaseCOMObjects(modelItem);
4074
            return symbolPath;
4075
        }
4076

    
4077
        /// <summary>
4078
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
4079
        /// </summary>
4080
        /// <param name="graphicOID"></param>
4081
        /// <param name="milliseconds"></param>
4082
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
4083
        {
4084
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
4085
            {
4086
                double minX = 0;
4087
                double minY = 0;
4088
                double maxX = 0;
4089
                double maxY = 0;
4090
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
4091
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
4092

    
4093
                Thread.Sleep(milliseconds);
4094
            }
4095
        }
4096

    
4097
        /// <summary>
4098
        /// ComObject를 Release
4099
        /// </summary>
4100
        /// <param name="objVars"></param>
4101
        public void ReleaseCOMObjects(params object[] objVars)
4102
        {
4103
            if (objVars != null)
4104
            {
4105
                int intNewRefCount = 0;
4106
                foreach (object obj in objVars)
4107
                {
4108
                    if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
4109
                        intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
4110
                }
4111
            }
4112
        }
4113
    }
4114
}
클립보드 이미지 추가 (최대 크기: 500 MB)