프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 442bd51e

이력 | 보기 | 이력해설 | 다운로드 (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
        bool closeDocument;
35
        Ingr.RAD2D.Application radApp;
36
        SPPID_Document document;
37
        ETCSetting _ETCSetting;
38

    
39
        public string DocumentLabelText { get; set; }
40

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

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

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

    
68
                }
69
            }
70
        }
71

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

    
84
                CreateDocument(ref drawingNumber, ref drawingName);
85

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

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

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

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

    
145
                if (radApp.ActiveDocument != null)
146
                {
147
                    newDrawing.Save();
148
                    if (closeDocument)
149
                        newDrawing.CloseDrawing(true);
150
                    ReleaseCOMObjects(newDrawing);
151
                }
152

    
153
                ReleaseCOMObjects(dataSource);
154
                ReleaseCOMObjects(_placement);
155

    
156
                Project_DB.InsertDrawingInfo(document.PATH, drawingNumber, drawingName, document);
157
                if (SplashScreenManager.Default.IsSplashFormVisible)
158
                {
159
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
160
                    SplashScreenManager.CloseForm(false);
161
                    Log.Write("\r\n");
162
                }
163

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

    
214
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, step1_Line.Count);
215
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 1");
216

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

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

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

    
289
                LMRelationships relationships = new LMRelationships();
290
                relationships.Collect(dataSource, Filter: filter);
291

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

    
491
                        SetFlowDirectionByLine(line.SPPID.ModelItemId);
492

    
493
                        ReleaseCOMObjects(modelItem);
494
                    }
495
                }
496

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

    
509
                SetFlowDirectionByLine(modelId);
510

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

    
524
                SetFlowDirectionByLine(modelId);
525

    
526
                ReleaseCOMObjects(zeroLengthModelItem);
527
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
528
            }
529
            foreach (var modelId in ZeroLengthSymbolToSymbolModelItemID)
530
            {
531
                SetFlowDirectionByLine(modelId);
532
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
533
            }
534

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

    
550
                                foreach (LMRelationship relationship in connector.Relation1Relationships)
551
                                    SetSymbolFlowDirectionByRelationShip(relationship, connector, sFlowDirection);
552
                                foreach (LMRelationship relationship in connector.Relation2Relationships)
553
                                    SetSymbolFlowDirectionByRelationShip(relationship, connector, sFlowDirection);
554

    
555
                                ReleaseCOMObjects(connector);
556
                            }
557
                        }
558
                    }
559
                    ReleaseCOMObjects(modelItem);
560
                }
561

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

    
572
                        SetSymbolFlowDirection(lineIndex, symbolIndex, sFlowDirection, symbolModelItem);
573

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

    
585
                        SetSymbolFlowDirection(lineIndex, symbolIndex, sFlowDirection, symbolModelItem);
586

    
587
                        symbolModelItem.Commit();
588
                        ReleaseCOMObjects(symbolModelItem);
589
                    }
590
                }
591

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

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

    
747
            LMDrawings drawings = new LMDrawings();
748
            drawings.Collect(dataSource, Filter: filter);
749

    
750
            currentDrawing = ((dynamic)drawings).Nth(1);
751
        }
752

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

    
763
            List<string> drawingNameList = new List<string>();
764
            List<string> drawingNumberList = new List<string>();
765

    
766
            foreach (LMDrawing item in drawings)
767
            {
768
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
769
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
770
            }
771

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

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

    
798
            ReleaseCOMObjects(drawings);
799
        }
800

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

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

    
838
            LMSymbol _LMSymbol = null;
839

    
840
            string mappingPath = symbol.SPPID.MAPPINGNAME;
841
            double x = symbol.SPPID.ORIGINAL_X;
842
            double y = symbol.SPPID.ORIGINAL_Y;
843
            int mirror = 0;
844
            double angle = symbol.ANGLE;
845

    
846
            // OPC 일경우 180도 일때 Mirror
847
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
848
                mirror = 1;
849

    
850
            // Mirror 계산
851
            if (symbol.FLIP == 1)
852
            {
853
                mirror = 1;
854
                angle += Math.PI;
855
            }
856

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

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

    
866
                if (_LMSymbol != null && _TargetItem != null)
867
                {
868
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
869

    
870
                    if (SPPIDUtil.IsSegmentLine(document, symbol, targetSymbol))
871
                    {
872
                        LMConnector reModelingConnector = FindBreakLineTarget(symbol, targetSymbol);
873
                        if (reModelingConnector != null)
874
                            ReModelingLMConnector(reModelingConnector);
875
                    }
876
                }
877

    
878
                ReleaseCOMObjects(_TargetItem);
879
            }
880
            else
881
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
882

    
883
            if (_LMSymbol != null)
884
            {
885
                _LMSymbol.Commit();
886

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

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

    
908

    
909
                }
910

    
911
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
912
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
913
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
914

    
915
                foreach (var item in symbol.ChildSymbols)
916
                    CreateChildSymbol(item, _LMSymbol, symbol);
917

    
918
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
919
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
920

    
921
                double[] range = null;
922
                GetSPPIDSymbolRange(symbol, ref range);
923
                symbol.SPPID.SPPID_Min_X = range[0];
924
                symbol.SPPID.SPPID_Min_Y = range[1];
925
                symbol.SPPID.SPPID_Max_X = range[2];
926
                symbol.SPPID.SPPID_Max_Y = range[3];
927

    
928
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
929
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
930
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
931
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
932

    
933
                ReleaseCOMObjects(_LMSymbol);
934
            }
935
        }
936

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

    
949
            symbol.SPPID.RepresentationId = string.Empty;
950
            symbol.SPPID.ModelItemID = string.Empty;
951
            symbol.SPPID.SPPID_X = double.NaN;
952
            symbol.SPPID.SPPID_Y = double.NaN;
953
            symbol.SPPID.SPPID_Min_X = double.NaN;
954
            symbol.SPPID.SPPID_Min_Y = double.NaN;
955
            symbol.SPPID.SPPID_Max_X = double.NaN;
956
            symbol.SPPID.SPPID_Max_Y = double.NaN;
957
        }
958

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

    
973
                symbol.SPPID.RepresentationId = string.Empty;
974
                symbol.SPPID.ModelItemID = string.Empty;
975
                symbol.SPPID.SPPID_X = double.NaN;
976
                symbol.SPPID.SPPID_Y = double.NaN;
977
                symbol.SPPID.SPPID_Min_X = double.NaN;
978
                symbol.SPPID.SPPID_Min_Y = double.NaN;
979
                symbol.SPPID.SPPID_Max_X = double.NaN;
980
                symbol.SPPID.SPPID_Max_Y = double.NaN;
981
            }
982
        }
983

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

    
995
            double[] range = null;
996
            List<double[]> points = new List<double[]>();
997
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
998
            double x1 = range[0];
999
            double y1 = range[1];
1000
            double x2 = range[2];
1001
            double y2 = range[3];
1002

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

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

    
1020
            double percentX = (x2 - x1) / sizeWidth;
1021
            double percentY = (y2 - y1) / sizeHeight;
1022

    
1023
            double SPPIDgapX = gapX * percentX;
1024
            double SPPIDgapY = gapY * percentY;
1025

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

    
1041
            ReleaseCOMObjects(_TargetItem);
1042
        }
1043

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

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

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

    
1085
            foreach (var childSymbol in symbol.ChildSymbols)
1086
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
1087

    
1088
            ReleaseCOMObjects(_TargetItem);
1089
        }
1090

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

    
1104
                foreach (var childSymbol in symbol.ChildSymbols)
1105
                    GetSPPIDChildSymbolRange(childSymbol, ref range);
1106

    
1107
                ReleaseCOMObjects(_TargetItem);
1108
            }
1109
        }
1110

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

    
1124
                tempRange[0] = Math.Min(tempRange[0], x1);
1125
                tempRange[1] = Math.Min(tempRange[1], y1);
1126
                tempRange[2] = Math.Max(tempRange[2], x2);
1127
                tempRange[3] = Math.Max(tempRange[3], y2);
1128

    
1129
                foreach (var childSymbol in symbol.ChildSymbols)
1130
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
1131

    
1132
                ReleaseCOMObjects(_TargetItem);
1133
            }
1134

    
1135
            range = tempRange;
1136
        }
1137

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

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

    
1169
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1170
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
1171

    
1172
                ReleaseCOMObjects(_ChildSymbol);
1173
            }
1174
        }
1175

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

    
1192
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1193
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
1194
                ReleaseCOMObjects(_ChildSymbol);
1195
            }
1196
        }
1197

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

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

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

    
1235
                        //Leader 선 센터로
1236
                        if (_LMLabelPresist != null)
1237
                        {
1238
                            // Target Item에 Label의 Attribute Input
1239
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1240

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

    
1269
                                        if (result)
1270
                                            break;
1271
                                    }
1272

    
1273
                                    if (result)
1274
                                        break;
1275
                                }
1276
                            }
1277

    
1278
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1279
                            _LMLabelPresist.Commit();
1280
                            ReleaseCOMObjects(_LMLabelPresist);
1281
                        }
1282

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

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

    
1309
                    foreach (var item in connectorVertices)
1310
                        if (item.Key != null)
1311
                            ReleaseCOMObjects(item.Key);
1312
                }
1313
            }
1314
        }
1315

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

    
1325
            LMSymbol _LMSymbol = null;
1326
            LMSymbol targetItem = null;
1327
            string mappingPath = equipment.SPPID.MAPPINGNAME;
1328
            double x = equipment.SPPID.ORIGINAL_X;
1329
            double y = equipment.SPPID.ORIGINAL_Y;
1330
            int mirror = 0;
1331
            double angle = equipment.ANGLE;
1332

    
1333
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
1334

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

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

    
1371
            if (_LMSymbol != null)
1372
            {
1373
                _LMSymbol.Commit();
1374
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1375
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
1376
                ReleaseCOMObjects(_LMSymbol);
1377
            }
1378

    
1379
            if (targetItem != null)
1380
            {
1381
                ReleaseCOMObjects(targetItem);
1382
            }
1383

    
1384
            ReleaseCOMObjects(_LMSymbol);
1385
        }
1386

    
1387
        /// <summary>
1388
        /// 첫 진입점
1389
        /// </summary>
1390
        /// <param name="symbol"></param>
1391
        private void SymbolModelingBySymbol(Symbol symbol)
1392
        {
1393
            SymbolModeling(symbol, null);
1394
            List<object> endObjects = new List<object>();
1395
            endObjects.Add(symbol);
1396

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

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

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

    
1475
                                    // Range 겹치는지 확인해야함
1476
                                    double[] prevRange = null;
1477
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1478
                                    double[] groupRange = null;
1479
                                    GetSPPIDSymbolRange(group, ref groupRange);
1480

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

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

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

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

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

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

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

    
1585

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

    
1594
            List<Line> group = new List<Line>();
1595
            GetConnectedLineGroup(line, group);
1596
            LineCoordinateCorrection(group);
1597

    
1598
            foreach (var groupLine in group)
1599
            {
1600
                if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
1601
                {
1602
                    BranchLines.Add(groupLine);
1603
                    continue;
1604
                }
1605

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

    
1679
                            placeRunInputs.AddPoint(x, y);
1680

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

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

    
1713
                    if (bRemodelingStart || bRemodelingEnd)
1714
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
1715

    
1716
                    FlowMarkModeling(groupLine);
1717
                    LineNumberModelingOnlyOne(groupLine);
1718

    
1719
                    ReleaseCOMObjects(_lMConnector);
1720
                }
1721
                else if (!isBranchModeling)
1722
                {
1723
                    Log.Write("Main Line Modeling : " + groupLine.UID);
1724
                }
1725

    
1726
                List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
1727
                x.ConnectedObject != null &&
1728
                x.ConnectedObject.GetType() == typeof(Line) &&
1729
                !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
1730
                .Select(x => x.ConnectedObject)
1731
                .ToList();
1732

    
1733
                foreach (var item in removeLines)
1734
                    RemoveLineForModeling(item as Line);
1735

    
1736
                if (_LMAItem != null)
1737
                    ReleaseCOMObjects(_LMAItem);
1738
                if (placeRunInputs != null)
1739
                    ReleaseCOMObjects(placeRunInputs);
1740
                if (_LMSymbolStart != null)
1741
                    ReleaseCOMObjects(_LMSymbolStart);
1742
                if (_LMSymbolEnd != null)
1743
                    ReleaseCOMObjects(_LMSymbolEnd);
1744

    
1745
                if (isBranchModeling && BranchLines.Contains(groupLine))
1746
                    BranchLines.Remove(groupLine);
1747
            }
1748
        }
1749

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

    
1778
                ReleaseCOMObjects(modelItem);
1779
            }
1780
        }
1781

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

    
1806
        private void LineCoordinateCorrection(List<Line> group)
1807
        {
1808
            // 순서대로 전 Item 기준 정렬
1809
            LineCoordinateCorrectionByStart(group);
1810

    
1811
            // 역으로 심볼이 있을 경우 좌표 보정
1812
            LineCoordinateCorrectionForLastLine(group);
1813
        }
1814

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

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

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

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

    
1869
                            double x = 0;
1870
                            double y = 0;
1871
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1872
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
1873
                        }
1874
                        break;
1875
                    }
1876
                }
1877
            }
1878
        }
1879

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

    
1899
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
1900
        }
1901

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

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

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

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

    
1970
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
1971
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
1972
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
1973

    
1974
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
1975
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
1976
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
1977

    
1978
                if (startSpecBreak != null || startEndBreak != null)
1979
                    result = true;
1980
            }
1981
        }
1982

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

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

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

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

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

    
2071
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
2072
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2073

    
2074
            ReleaseCOMObjects(placeRunInputs);
2075
            ReleaseCOMObjects(_LMAItem);
2076
            ReleaseCOMObjects(modelItem);
2077

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

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

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

    
2123
                line.SPPID.ModelItemId = newConnector.ModelItemID;
2124
                ReleaseCOMObjects(newConnector);
2125
            }
2126

    
2127
            ReleaseCOMObjects(modelItem);
2128
        }
2129

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

    
2152
                    if (result)
2153
                        break;
2154
                }
2155
            }
2156
            foreach (var item in dicVertices)
2157
                ReleaseCOMObjects(item.Key);
2158
        }
2159

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

    
2185
                            if (child != null)
2186
                                break;
2187
                        }
2188

    
2189
                        if (child != null)
2190
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
2191
                    }
2192

    
2193
                    break;
2194
                }
2195
            }
2196

    
2197
            return _LMSymbol;
2198
        }
2199

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

    
2216
            return null;
2217
        }
2218

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

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

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

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

    
2274
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
2275
            {
2276
                double[] vertices = null;
2277
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2278
                double x = 0;
2279
                double y = 0;
2280
                lineStringGeometry.GetVertex(1, ref x, ref y);
2281

    
2282
                string flowDirection = string.Empty;
2283
                LMAAttribute flowAttribute = connector.ModelItemObject.Attributes["FlowDirection"];
2284
                if (flowAttribute != null && !DBNull.Value.Equals(flowAttribute.get_Value()))
2285
                    flowDirection = flowAttribute.get_Value().ToString();
2286

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

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

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

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

    
2340
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2341
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2342
                newConnector.Commit();
2343
                foreach (var line in lines)
2344
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
2345
            }
2346

    
2347

    
2348
            return newConnector;
2349
        }
2350

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

    
2360
            if (upStreamObj != null &&
2361
                downStreamObj != null)
2362
            {
2363
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2364

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

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

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

    
2409
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
2410
                {
2411
                    if (connector.get_ItemStatus() != "Active")
2412
                        continue;
2413

    
2414
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2415
                    {
2416
                        targetConnector = connector;
2417
                        break;
2418
                    }
2419
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2420
                    {
2421
                        targetConnector = connector;
2422
                        break;
2423
                    }
2424
                }
2425

    
2426
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
2427
                {
2428
                    if (connector.get_ItemStatus() != "Active")
2429
                        continue;
2430

    
2431
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2432
                    {
2433
                        targetConnector = connector;
2434
                        break;
2435
                    }
2436
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2437
                    {
2438
                        targetConnector = connector;
2439
                        break;
2440
                    }
2441
                }
2442

    
2443
                ReleaseCOMObjects(targetLMSymbol);
2444
                ReleaseCOMObjects(connectedLMSymbol);
2445
            }
2446
            else if (targetLine != null && connectedLine != null)
2447
            {
2448
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2449
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2450

    
2451
                if (targetModelItem != null && targetModelItem.get_ItemStatus() == "Active" && connectedModelItem != null && connectedModelItem.get_ItemStatus() == "Active")
2452
                {
2453
                    foreach (LMRepresentation rep in targetModelItem.Representations)
2454
                    {
2455
                        if (targetConnector != null)
2456
                            break;
2457

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

    
2462
                            if (IsConnected(_LMConnector, connectedModelItem))
2463
                                targetConnector = _LMConnector;
2464
                            else
2465
                                ReleaseCOMObjects(_LMConnector);
2466
                        }
2467
                    }
2468

    
2469
                    ReleaseCOMObjects(targetModelItem);
2470
                }
2471
            }
2472
            else
2473
            {
2474
                LMSymbol connectedLMSymbol = null;
2475
                if (connectedSymbol != null)
2476
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2477
                else if (targetSymbol != null)
2478
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2479
                else
2480
                {
2481

    
2482
                }
2483
                LMModelItem targetModelItem = null;
2484
                if (targetLine != null)
2485
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2486
                else if (connectedLine != null)
2487
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2488
                else
2489
                {
2490

    
2491
                }
2492
                if (connectedLMSymbol != null && targetModelItem != null)
2493
                {
2494
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
2495
                    {
2496
                        if (connector.get_ItemStatus() != "Active")
2497
                            continue;
2498

    
2499
                        if (IsConnected(connector, targetModelItem))
2500
                        {
2501
                            targetConnector = connector;
2502
                            break;
2503
                        }
2504
                    }
2505

    
2506
                    if (targetConnector == null)
2507
                    {
2508
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
2509
                        {
2510
                            if (connector.get_ItemStatus() != "Active")
2511
                                continue;
2512

    
2513
                            if (IsConnected(connector, targetModelItem))
2514
                            {
2515
                                targetConnector = connector;
2516
                                break;
2517
                            }
2518
                        }
2519
                    }
2520
                }
2521

    
2522
            }
2523

    
2524
            return targetConnector;
2525
        }
2526

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

    
2544
                    List<double[]> vertices = GetConnectorVertices(targetConnector);
2545

    
2546
                    ReleaseCOMObjects(modelItem);
2547
                    ReleaseCOMObjects(connectedConnector);
2548

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

    
2563
            return result;
2564
        }
2565

    
2566
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
2567
        {
2568
            bool result = false;
2569

    
2570
            foreach (LMRepresentation rep in modelItem.Representations)
2571
            {
2572
                if (result)
2573
                    break;
2574

    
2575
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2576
                {
2577
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2578

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

    
2612
                    ReleaseCOMObjects(_LMConnector);
2613
                }
2614
            }
2615

    
2616

    
2617
            return result;
2618
        }
2619

    
2620
        private void FindConnectedConnector(LMConnector connector, LMModelItem modelItem, ref LMConnector connectedConnector, ref int connectorIndex)
2621
        {
2622
            foreach (LMRepresentation rep in modelItem.Representations)
2623
            {
2624
                if (connectedConnector != null)
2625
                    break;
2626

    
2627
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2628
                {
2629
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2630

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

    
2664
                    if (connectedConnector == null)
2665
                        ReleaseCOMObjects(_LMConnector);
2666
                }
2667
            }
2668
        }
2669

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

    
2684
                // item2가 item1으로 조인
2685
                _placement.PIDJoinRuns(ref item1, ref item2);
2686
                item1.Commit();
2687
                item2.Commit();
2688

    
2689
                string beforeID = string.Empty;
2690
                string afterID = string.Empty;
2691

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

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

    
2725
                ReleaseCOMObjects(modelItem1);
2726
                ReleaseCOMObjects(item1);
2727
                ReleaseCOMObjects(modelItem2);
2728
                ReleaseCOMObjects(item2);
2729
            }
2730
            catch (Exception ex)
2731
            {
2732
                Log.Write("Join Error");
2733
                Log.Write(ex.Message);
2734
            }
2735
        }
2736

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

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

    
2758

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

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

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

    
2815
                ReleaseCOMObjects(modelItem);
2816
            }
2817

    
2818
            return connectorVertices;
2819
        }
2820

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

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

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

    
2876
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
2877
                        if (length >= distance)
2878
                        {
2879
                            targetConnector = item.Key;
2880
                            length = distance;
2881
                        }
2882
                    }
2883
                }
2884
            }
2885

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

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

    
2906
            return targetConnector;
2907
        }
2908

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

    
2915
            double length = double.MaxValue;
2916
            LMConnector targetConnector = null;
2917
            double[] resultPoint = null;
2918
            List<double[]> targetVertices = null;
2919

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

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

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

    
2950
                    double maxLineX = Math.Max(point1[0], point2[0]);
2951
                    double minLineX = Math.Min(point1[0], point2[0]);
2952
                    double maxLineY = Math.Max(point1[1], point2[1]);
2953
                    double minLineY = Math.Min(point1[1], point2[1]);
2954

    
2955
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
2956

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

    
2993
            foreach (var item in vertices)
2994
                if (item.Key != null && item.Key != targetConnector)
2995
                    ReleaseCOMObjects(item.Key);
2996

    
2997
            if (SPPIDUtil.IsBranchLine(line, targetLine))
2998
            {
2999
                double tempResultX = resultPoint[0];
3000
                double tempResultY = resultPoint[1];
3001
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
3002

    
3003
                GridSetting gridSetting = GridSetting.GetInstance();
3004

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

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

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

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

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

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

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

    
3164
            x = resultPoint[0];
3165
            y = resultPoint[1];
3166

    
3167
            return targetConnector;
3168
        }
3169

    
3170
        private LMConnector GetLMConnectorOnlyOne(string modelItemID)
3171
        {
3172
            LMConnector result = null;
3173
            List<LMConnector> connectors = new List<LMConnector>();
3174
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
3175

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

    
3184
                ReleaseCOMObjects(modelItem);
3185
            }
3186

    
3187
            if (connectors.Count == 1)
3188
                result = connectors[0];
3189
            else
3190
                foreach (var item in connectors)
3191
                    ReleaseCOMObjects(item);
3192

    
3193
            return result;
3194
        }
3195

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

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

    
3215
                    if (_LmLabelPresist != null)
3216
                    {
3217
                        _LmLabelPresist.Commit();
3218
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
3219
                        ReleaseCOMObjects(_LmLabelPresist);
3220
                    }
3221
                }
3222
            }
3223
        }
3224

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

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

    
3241
                    if (_LmLabelPresist != null)
3242
                    {
3243
                        _LmLabelPresist.Commit();
3244
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
3245
                        ReleaseCOMObjects(_LmLabelPresist);
3246
                    }
3247
                }
3248

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

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

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

    
3344
                                        }
3345
                                    }
3346
                                }
3347
                                _LMModelItem.Commit();
3348
                            }
3349
                            if (_LMModelItem != null)
3350
                                ReleaseCOMObjects(_LMModelItem);
3351
                            endLine.Add(line.SPPID.ModelItemId);
3352
                        }
3353
                    }
3354
                }
3355
            }
3356
        }
3357

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

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

    
3406
                ReleaseCOMObjects(_Attributes);
3407
                ReleaseCOMObjects(_LMModelItem);
3408
                ReleaseCOMObjects(_LMSymbol);
3409
            }
3410
        }
3411

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

    
3421
            if (upStreamObj != null &&
3422
                downStreamObj != null)
3423
            {
3424
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
3425

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

    
3443
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
3444
                                }
3445
                            }
3446
                        }
3447
                    }
3448

    
3449
                    ReleaseCOMObjects(targetLMConnector);
3450
                }
3451
            }
3452

    
3453

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

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

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

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

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

    
3492
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3493
                    {
3494
                        if (connector.get_ItemStatus() != "Active")
3495
                            continue;
3496

    
3497
                        if (connector.Id != zeroLenthConnector.Id)
3498
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3499
                    }
3500

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

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

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

    
3520
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3521
                    {
3522
                        if (connector.get_ItemStatus() != "Active")
3523
                            continue;
3524

    
3525
                        if (connector.Id == zeroLenthConnector.Id)
3526
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3527
                    }
3528

    
3529
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3530
                    {
3531
                        if (connector.get_ItemStatus() != "Active")
3532
                            continue;
3533

    
3534
                        if (connector.Id == zeroLenthConnector.Id)
3535
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3536
                    }
3537

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

    
3547
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3548
                    {
3549
                        if (connector.get_ItemStatus() != "Active")
3550
                            continue;
3551

    
3552
                        if (connector.Id == zeroLenthConnector.Id)
3553
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3554
                    }
3555

    
3556
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3557
                    {
3558
                        if (connector.get_ItemStatus() != "Active")
3559
                            continue;
3560

    
3561
                        if (connector.Id == zeroLenthConnector.Id)
3562
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3563
                    }
3564

    
3565
                    ReleaseCOMObjects(zeroLenthConnector);
3566
                    ReleaseCOMObjects(downStreamLMSymbol);
3567
                }
3568
            }
3569

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

    
3584
                    _LMModelItem.Commit();
3585
                }
3586
                if (_LMModelItem != null)
3587
                    ReleaseCOMObjects(_LMModelItem);
3588
            }
3589

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

    
3604
                    _LMModelItem.Commit();
3605
                }
3606
                if (_LMModelItem != null)
3607
                    ReleaseCOMObjects(_LMModelItem);
3608
            }
3609
            #endregion
3610
        }
3611

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

    
3635
                            if (mapping != null)
3636
                            {
3637
                                double x = 0;
3638
                                double y = 0;
3639

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

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

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

    
3667
                            if (mapping != null)
3668
                            {
3669
                                double x = 0;
3670
                                double y = 0;
3671

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

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

    
3692
                double x = 0;
3693
                double y = 0;
3694
                double angle = text.ANGLE;
3695
                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
3696

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

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

    
3724
        /// <summary>
3725
        /// Note Modeling
3726
        /// </summary>
3727
        /// <param name="note"></param>
3728
        private void NoteModeling(Note note)
3729
        {
3730
            LMSymbol _LMSymbol = null;
3731
            LMItemNote _LMItemNote = null;
3732
            LMAAttribute _LMAAttribute = null;
3733

    
3734
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
3735
            {
3736
                double x = 0;
3737
                double y = 0;
3738

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

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

    
3760
            if (_LMAAttribute != null)
3761
                ReleaseCOMObjects(_LMAAttribute);
3762
            if (_LMItemNote != null)
3763
                ReleaseCOMObjects(_LMItemNote);
3764
            if (_LMSymbol != null)
3765
                ReleaseCOMObjects(_LMSymbol);
3766
        }
3767

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

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

    
3831
                if (location.HasFlag(Location.Left))
3832
                    x = SPPIDLabelLocation.X1;
3833
                else if (location.HasFlag(Location.Right))
3834
                    x = SPPIDLabelLocation.X2;
3835

    
3836
                if (location.HasFlag(Location.Down))
3837
                    y = SPPIDLabelLocation.Y1;
3838
                else if (location.HasFlag(Location.Up))
3839
                    y = SPPIDLabelLocation.Y2;
3840
            }
3841
        }
3842

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

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

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

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

    
3880
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
3881
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
3882
                        symbols.Add(symbol);
3883
                }
3884
            }
3885

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

    
3896
            return symbols;
3897
        }
3898

    
3899
        private void SetPriorityLine(List<Line> lines)
3900
        {
3901
            lines.Sort(SortLinePriority);
3902

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

    
3938
                            }
3939
                        }
3940
                    }
3941
                }
3942

    
3943
                return 0;
3944
            }
3945

    
3946
            int CompareNotSegmentLine(Line a, Line b)
3947
            {
3948
                List<Connector> connectorsA = a.CONNECTORS
3949
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3950
                    .ToList();
3951

    
3952
                List<Connector> connectorsB = b.CONNECTORS
3953
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3954
                    .ToList();
3955

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

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

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

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

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

    
3982
                List<Connector> connectorsB = b.CONNECTORS
3983
                    .Where(conn => conn.ConnectedObject != null &&
3984
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
3985
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
3986
                    .ToList();
3987

    
3988
                // 오름차순
3989
                return connectorsB.Count.CompareTo(connectorsA.Count);
3990
            }
3991

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

    
4001
                // 내림차순
4002
                return connectorsA.Count.CompareTo(connectorsB.Count);
4003
            }
4004

    
4005
            int CompareNoneConn(Line a, Line b)
4006
            {
4007
                List<Connector> connectorsA = a.CONNECTORS
4008
                    .Where(conn => conn.ConnectedObject == null)
4009
                    .ToList();
4010

    
4011
                List<Connector> connectorsB = b.CONNECTORS
4012
                    .Where(conn => conn.ConnectedObject == null)
4013
                    .ToList();
4014

    
4015
                // 오름차순
4016
                return connectorsB.Count.CompareTo(connectorsA.Count);
4017
            }
4018
        }
4019

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

    
4030
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
4031
                 x.ConnectedObject.GetType() == typeof(Line) &&
4032
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
4033
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
4034

    
4035
                // 내림차순
4036
                return countA.CompareTo(countB);
4037
            }
4038
        }
4039

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

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

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

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

    
4097
                Thread.Sleep(milliseconds);
4098
            }
4099
        }
4100

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