프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 5a25b6b0

이력 | 보기 | 이력해설 | 다운로드 (296 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
    [Flags]
28
    public enum SegmentLocation
29
    {
30
        None = 0,
31
        Right = 1,
32
        Left = 2,
33
        Down = 4,
34
        Up = 8
35
    }
36
    public class AutoModeling : IDisposable
37
    {
38
        Placement _placement;
39
        LMADataSource dataSource;
40
        string drawingID;
41
        dynamic newDrawing;
42
        dynamic application;
43
        bool closeDocument;
44
        Ingr.RAD2D.Application radApp;
45
        SPPID_Document document;
46
        ETCSetting _ETCSetting;
47
        DataTable nominalDiameterTable = null;
48
        public string DocumentLabelText { get; set; }
49

    
50
        List<Line> BranchLines = new List<Line>();
51
        List<string> ZeroLengthSymbolToSymbolModelItemID = new List<string>();
52
        List<string> ZeroLengthModelItemID = new List<string>();
53
        List<string> ZeroLengthModelItemIDReverse = new List<string>();
54
        List<Symbol> prioritySymbols;
55
        List<string> FlowMarkRepIds = new List<string>();
56

    
57
        public AutoModeling(SPPID_Document document, bool closeDocument)
58
        {
59
            application = Interaction.GetObject("", "PIDAutomation.Application");
60
            WrapperApplication wApp = new WrapperApplication(application.Application);
61
            radApp = wApp.RADApplication;
62

    
63
            this.closeDocument = closeDocument;
64
            this.document = document;
65
            this._ETCSetting = ETCSetting.GetInstance();
66
        }
67

    
68
        private void SetSystemEditingCommand(bool value)
69
        {
70
            foreach (var item in radApp.Commands)
71
            {
72
                if (item.Argument == "SystemEditingCmd.SystemEditing")
73
                {
74
                    if (item.Checked != value)
75
                    {
76
                        radApp.RunMacro("systemeditingcmd.dll");
77
                        break;
78
                    }
79

    
80
                }
81
            }
82
        }
83

    
84
        /// <summary>
85
        /// 도면 단위당 실행되는 메서드
86
        /// </summary>
87
        public void Run()
88
        {
89
            string drawingNumber = document.DrawingNumber;
90
            string drawingName = document.DrawingName;
91
            try
92
            {
93
                nominalDiameterTable = Project_DB.SelectProjectNominalDiameter();
94
                _placement = new Placement();
95
                dataSource = _placement.PIDDataSource;
96

    
97
                if (CreateDocument(ref drawingNumber, ref drawingName) && DocumentCoordinateCorrection())
98
                {
99
                    Log.Write("Start Modeling");
100
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
101
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetParent, (IntPtr)radApp.HWnd);
102
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllStepCount, 25);
103
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetDocumentName, DocumentLabelText);
104

    
105
                    // VendorPackage Modeling
106
                    RunVendorPackageModeling();
107
                    // Equipment Modeling
108
                    RunEquipmentModeling();
109
                    // Symbol Modeling
110
                    RunSymbolModeling();
111
                    // LineRun Line Modeling
112
                    RunLineModeling();
113
                    // Vent Drain Modeling
114
                    RunVentDrainModeling();
115
                    // Clear Attribute
116
                    RunClearNominalDiameter();
117
                    // Join SameConnector
118
                    RunJoinRunForSameConnector();
119
                    // Join Run
120
                    RunJoinRun();
121
                    // EndBreak Modeling
122
                    RunEndBreakModeling();
123
                    // SpecBreak Modeling
124
                    RunSpecBreakModeling();
125
                    //Line Number Modeling
126
                    RunLineNumberModeling();
127
                    // Note Modeling
128
                    RunNoteModeling();
129
                    // Text Modeling
130
                    RunTextModeling();
131
                    // Input LineNumber Attribute
132
                    RunInputLineNumberAttribute();
133
                    // Input Symbol Attribute
134
                    RunInputSymbolAttribute();
135
                    // Input SpecBreak Attribute
136
                    RunInputSpecBreakAttribute();
137
                    // Input EndBreak Attribute
138
                    RunInputEndBreakAttribute();
139
                    // Label Symbol Modeling
140
                    RunLabelSymbolModeling();
141
                    // Correct Text
142
                    RunCorrectAssociationText();
143
                    // ETC
144
                    RunETC();
145
                    // BulkAttribute
146
                    RunBulkAttribute();
147
                    // Result Logging
148
                    document.CheckModelingResult();
149
                }
150
            }
151
            catch (Exception ex)
152
            {
153
                if (SplashScreenManager.Default != null && SplashScreenManager.Default.IsSplashFormVisible)
154
                {
155
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
156
                    SplashScreenManager.CloseForm(false);
157
                    Log.Write("\r\n");
158
                }
159
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
160
            }
161
            finally
162
            {
163
                Project_DB.InsertDrawingInfoAndOPCInfo(document.PATH, drawingNumber, drawingName, document);
164
                //Project_DB.InsertLineNumberInfo(document.PATH, drawingNumber, drawingName, document);
165

    
166
                if (SplashScreenManager.Default != null && SplashScreenManager.Default.IsSplashFormVisible)
167
                {
168
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
169
                    SplashScreenManager.CloseForm(false);
170
                    Log.Write("\r\n");
171
                }
172
                Thread.Sleep(1000);
173

    
174
                Log.Write("End Modeling");
175
                radApp.ActiveWindow.Fit();
176

    
177
                ReleaseCOMObjects(application);
178
                application = null;
179
                if (radApp.ActiveDocument != null)
180
                {
181
                    if (closeDocument && newDrawing != null)
182
                    {
183
                        newDrawing.Save();
184
                        newDrawing.CloseDrawing(true);
185
                        ReleaseCOMObjects(newDrawing);
186
                        newDrawing = null;
187
                    }
188
                    else if (newDrawing == null)
189
                    {
190
                        Log.Write("error document");
191
                    }
192
                }
193

    
194
                ReleaseCOMObjects(dataSource);
195
                dataSource = null;
196
                ReleaseCOMObjects(_placement);
197
                _placement = null;
198

    
199
                Thread.Sleep(1000);
200
            }
201
        }
202

    
203
        private void RunVendorPackageModeling()
204
        {
205
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.VendorPackages.Count);
206
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "VendorPackages Modeling");
207
            foreach (VendorPackage item in document.VendorPackages)
208
            {
209
                try
210
                {
211
                    VendorPackageModeling(item);
212
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
213
                }
214
                catch (Exception ex)
215
                {
216
                    Log.Write("Error in RunVendorPackageModeling");
217
                    Log.Write("UID : " + item.UID);
218
                    Log.Write(ex.Message);
219
                    Log.Write(ex.StackTrace);
220
                }
221
            }
222
        }
223
        private void RunEquipmentModeling()
224
        {
225
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Equipments.Count);
226
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
227
            for (int i = 0; i < document.Equipments.Count; i++)
228
            {
229
                Equipment item = document.Equipments[i];
230
                try
231
                {
232
                    if (!string.IsNullOrEmpty(item.SPPID.RepresentationId))
233
                        continue;
234
                    EquipmentModeling(item);
235
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.Equipments.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
236
                    if (!string.IsNullOrEmpty(item.SPPID.RepresentationId))
237
                        i = -1;
238
                }
239
                catch (Exception ex)
240
                {
241
                    Log.Write("Error in EquipmentModeling");
242
                    Log.Write("UID : " + item.UID);
243
                    Log.Write(ex.Message);
244
                    Log.Write(ex.StackTrace);
245
                }
246
            }
247
        }
248
        private void RunSymbolModeling()
249
        {
250
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
251
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
252
            prioritySymbols = GetPrioritySymbol();
253
            foreach (var item in prioritySymbols)
254
            {
255
                try
256
                {
257
                    if (document.VentDrainSymbol.Contains(item))
258
                        continue;
259
                    SymbolModelingBySymbol(item);
260
                }
261
                catch (Exception ex)
262
                {
263
                    Log.Write("Error in SymbolModelingByPriority");
264
                    Log.Write("UID : " + item.UID);
265
                    Log.Write(ex.Message);
266
                    Log.Write(ex.StackTrace);
267
                }
268
            }
269
        }
270
        private void RunLineModeling()
271
        {
272
            List<Line> AllLine = document.LINES.ToList();
273
            List<Line> stepLast_Line = document.LINES.FindAll(x => x.CONNECTORS.FindAll(y => y.ConnectedObject != null && y.ConnectedObject.GetType() == typeof(Symbol)).Count == 2 &&
274
            !SPPIDUtil.IsBranchedLine(document, x));
275
            List<Line> step1_Line = AllLine.FindAll(x => !stepLast_Line.Contains(x));
276

    
277
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, step1_Line.Count);
278
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 1");
279

    
280
            SetPriorityLine(step1_Line);
281
            foreach (var item in step1_Line)
282
            {
283
                try
284
                {
285
                    if (document.VentDrainLine.Contains(item))
286
                        continue;
287
                    NewLineModeling(item);
288
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
289
                }
290
                catch (Exception ex)
291
                {
292
                    Log.Write("Error in NewLineModeling");
293
                    Log.Write("UID : " + item.UID);
294
                    Log.Write(ex.Message);
295
                    Log.Write(ex.StackTrace);
296
                }
297
            }
298

    
299
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, BranchLines.Count);
300
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 2");
301
            int branchCount = BranchLines.Count;
302
            while (BranchLines.Count > 0)
303
            {
304
                try
305
                {
306
                    SortBranchLines();
307
                    Line item = BranchLines[0];
308
                    NewLineModeling(item, true);
309
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
310
                }
311
                catch (Exception ex)
312
                {
313
                    Log.Write("Error in NewLineModeling");
314
                    Log.Write("UID : " + BranchLines[0].UID);
315
                    Log.Write(ex.Message);
316
                    Log.Write(ex.StackTrace);
317
                    BranchLines.Remove(BranchLines[0]);
318
                }
319
            }
320

    
321
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, stepLast_Line.Count);
322
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 3");
323
            foreach (var item in stepLast_Line)
324
            {
325
                try
326
                {
327
                    if (document.VentDrainLine.Contains(item))
328
                        continue;
329
                    NewLineModeling(item);
330
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
331
                }
332
                catch (Exception ex)
333
                {
334
                    Log.Write("Error in NewLineModeling");
335
                    Log.Write("UID : " + item.UID);
336
                    Log.Write(ex.Message);
337
                    Log.Write(ex.StackTrace);
338
                }
339
            }
340
        }
341
        private void RunVentDrainModeling()
342
        {
343
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.VentDrainLine.Count);
344
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Vent Drain Modeling");
345
            foreach (var item in document.VentDrainLine)
346
            {
347
                try
348
                {
349
                    Connector connector = item.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
350
                    if (connector != null)
351
                    {
352
                        SetCoordinate();
353
                        Symbol connSymbol = connector.ConnectedObject as Symbol;
354
                        SymbolModeling(connSymbol, null);
355
                        NewLineModeling(item, true);
356

    
357
                        GridSetting grid = GridSetting.GetInstance();
358
                        int count = grid.DrainValveCellCount;
359
                        double length = grid.Length;
360

    
361
                        // 길이 확인
362
                        if (!string.IsNullOrEmpty(item.SPPID.ModelItemId))
363
                        {
364
                            LMConnector _LMConnector = GetLMConnectorOnlyOne(item.SPPID.ModelItemId);
365
                            if (_LMConnector != null)
366
                            {
367
                                double[] connectorRange = GetConnectorRange(_LMConnector);
368
                                double connectorLength = double.NaN;
369
                                if (item.SlopeType == SlopeType.HORIZONTAL)
370
                                    connectorLength = connectorRange[2] - connectorRange[0];
371
                                else if (item.SlopeType == SlopeType.VERTICAL)
372
                                    connectorLength = connectorRange[3] - connectorRange[1];
373

    
374
                                if (!double.IsNaN(connectorLength) && connectorLength != count * length)
375
                                {
376
                                    double move = count * length - connectorLength;
377
                                    List<Symbol> group = new List<Symbol>();
378
                                    SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
379
                                    foreach (var symbol in group)
380
                                    {
381
                                        int connSymbolIndex = item.CONNECTORS.IndexOf(item.CONNECTORS.Find(x => x.ConnectedObject == connSymbol));
382
                                        if (item.SlopeType == SlopeType.HORIZONTAL)
383
                                        {
384
                                            if (connSymbolIndex == 0)
385
                                            {
386
                                                if (item.SPPID.START_X > item.SPPID.END_X)
387
                                                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X + move;
388
                                                else
389
                                                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X - move;
390
                                            }
391
                                            else
392
                                            {
393
                                                if (item.SPPID.START_X < item.SPPID.END_X)
394
                                                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X + move;
395
                                                else
396
                                                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X - move;
397
                                            }
398
                                        }
399
                                        else if (item.SlopeType == SlopeType.VERTICAL)
400
                                        {
401
                                            if (connSymbolIndex == 0)
402
                                            {
403
                                                if (item.SPPID.START_Y > item.SPPID.END_Y)
404
                                                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y + move;
405
                                                else
406
                                                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y - move;
407
                                            }
408
                                            else
409
                                            {
410
                                                if (item.SPPID.START_Y < item.SPPID.END_Y)
411
                                                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y + move;
412
                                                else
413
                                                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y - move;
414
                                            }
415
                                        }
416
                                    }
417

    
418
                                    // 제거
419
                                    RemoveSymbol(connSymbol);
420
                                    RemoveLine(item);
421

    
422
                                    // 재생성
423
                                    SymbolModelingBySymbol(connSymbol);
424
                                    NewLineModeling(item, true);
425
                                }
426
                            }
427

    
428
                            ReleaseCOMObjects(_LMConnector);
429
                            _LMConnector = null;
430
                        }
431
                    }
432
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
433
                }
434
                catch (Exception ex)
435
                {
436
                    Log.Write("Error in NewLineModeling");
437
                    Log.Write("UID : " + item.UID);
438
                    Log.Write(ex.Message);
439
                    Log.Write(ex.StackTrace);
440
                }
441

    
442
                void SetCoordinate()
443
                {
444
                    Connector branchConnector = item.CONNECTORS.Find(loop => loop.ConnectedObject != null && loop.ConnectedObject.GetType() == typeof(Line));
445
                    if (branchConnector != null)
446
                    {
447
                        Line connLine = branchConnector.ConnectedObject as Line;
448
                        double x = 0;
449
                        double y = 0;
450
                        GetTargetLineConnectorPoint(branchConnector, item, ref x, ref y);
451
                        LMConnector targetConnector = FindTargetLMConnectorForBranch(item, connLine, ref x, ref y);
452
                        if (targetConnector != null)
453
                        {
454
                            List<Symbol> group = new List<Symbol>();
455
                            SPPIDUtil.FindConnectedSymbolGroup(document, item.CONNECTORS.Find(loop => loop != branchConnector).ConnectedObject as Symbol, group);
456
                            if (item.SlopeType == SlopeType.HORIZONTAL)
457
                            {
458
                                item.SPPID.START_Y = y;
459
                                item.SPPID.END_Y = y;
460
                                foreach (var symbol in group)
461
                                {
462
                                    symbol.SPPID.ORIGINAL_Y = y;
463
                                    symbol.SPPID.SPPID_Y = y;
464
                                }
465
                            }
466
                            else if (item.SlopeType == SlopeType.VERTICAL)
467
                            {
468
                                item.SPPID.START_X = x;
469
                                item.SPPID.END_X = x;
470
                                foreach (var symbol in group)
471
                                {
472
                                    symbol.SPPID.ORIGINAL_X = x;
473
                                    symbol.SPPID.SPPID_X = x;
474
                                }
475
                            }
476
                        }
477
                        ReleaseCOMObjects(targetConnector);
478
                        targetConnector = null;
479
                    }
480
                }
481
            }
482
        }
483
        private void RunClearNominalDiameter()
484
        {
485
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count + document.LINES.Count);
486
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Clear Attribute");
487
            return;
488

    
489
            List<string> endClearModelItemID = new List<string>();
490
            for (int i = 0; i < document.LINES.Count; i++)
491
            {
492
                Line item = document.LINES[i];
493
                string modelItemID = item.SPPID.ModelItemId;
494
                if (!string.IsNullOrEmpty(modelItemID))
495
                {
496
                    LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
497
                    if (modelItem != null)
498
                    {
499
                        LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
500
                        if (attribute != null)
501
                            attribute.set_Value(DBNull.Value);
502

    
503
                        modelItem.Commit();
504
                        ReleaseCOMObjects(modelItem);
505
                        modelItem = null;
506
                    }
507
                }
508
                if (!endClearModelItemID.Contains(modelItemID))
509
                    endClearModelItemID.Add(modelItemID);
510
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
511
            }
512
            for (int i = 0; i < document.SYMBOLS.Count; i++)
513
            {
514
                Symbol item = document.SYMBOLS[i];
515
                string repID = item.SPPID.RepresentationId;
516
                string modelItemID = item.SPPID.ModelItemID;
517
                if (!string.IsNullOrEmpty(modelItemID))
518
                {
519
                    LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
520
                    if (modelItem != null)
521
                    {
522
                        LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
523
                        if (attribute != null)
524
                            attribute.set_Value(DBNull.Value);
525
                        int index = 1;
526
                        while (true)
527
                        {
528
                            attribute = modelItem.Attributes[string.Format("PipingPoint{0}.NominalDiameter", index)];
529
                            if (attribute != null)
530
                                attribute.set_Value(DBNull.Value);
531
                            else
532
                                break;
533
                            index++;
534
                        }
535
                        modelItem.Commit();
536
                        ReleaseCOMObjects(modelItem);
537
                        modelItem = null;
538
                    }
539
                }
540
                if (!string.IsNullOrEmpty(repID))
541
                {
542
                    LMSymbol symbol = dataSource.GetSymbol(repID);
543
                    if (symbol != null)
544
                    {
545
                        foreach (LMConnector connector in symbol.Connect1Connectors)
546
                        {
547
                            if (connector.get_ItemStatus() == "Active" && !endClearModelItemID.Contains(connector.ModelItemID))
548
                            {
549
                                endClearModelItemID.Add(connector.ModelItemID);
550
                                LMModelItem modelItem = connector.ModelItemObject;
551
                                if (modelItem != null)
552
                                {
553
                                    LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
554
                                    if (attribute != null)
555
                                        attribute.set_Value(DBNull.Value);
556

    
557
                                    modelItem.Commit();
558
                                    ReleaseCOMObjects(modelItem);
559
                                    modelItem = null;
560
                                }
561
                            }
562
                        }
563
                        foreach (LMConnector connector in symbol.Connect2Connectors)
564
                        {
565
                            if (connector.get_ItemStatus() == "Active" && !endClearModelItemID.Contains(connector.ModelItemID))
566
                            {
567
                                endClearModelItemID.Add(connector.ModelItemID);
568
                                LMModelItem modelItem = connector.ModelItemObject;
569
                                if (modelItem != null)
570
                                {
571
                                    LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
572
                                    if (attribute != null)
573
                                        attribute.set_Value(DBNull.Value);
574

    
575
                                    modelItem.Commit();
576
                                    ReleaseCOMObjects(modelItem);
577
                                    modelItem = null;
578
                                }
579
                            }
580
                        }
581
                    }
582
                    ReleaseCOMObjects(symbol);
583
                    symbol = null;
584
                }
585
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
586
            }
587
        }
588
        private void RunClearValueInconsistancy()
589
        {
590
            int count = 1;
591
            bool loop = true;
592
            while (loop)
593
            {
594
                loop = false;
595
                LMAFilter filter = new LMAFilter();
596
                LMACriterion criterion = new LMACriterion();
597
                filter.ItemType = "Relationship";
598
                criterion.SourceAttributeName = "SP_DRAWINGID";
599
                criterion.Operator = "=";
600
                criterion.set_ValueAttribute(drawingID);
601
                filter.get_Criteria().Add(criterion);
602

    
603
                LMRelationships relationships = new LMRelationships();
604
                relationships.Collect(dataSource, Filter: filter);
605

    
606
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, relationships.Count);
607
                if (count > 1)
608
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStepMinus, null);
609
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Clear Inconsistent Property Value - " + count);
610
                foreach (LMRelationship relationship in relationships)
611
                {
612
                    foreach (LMInconsistency inconsistency in relationship.Inconsistencies)
613
                    {
614
                        if (inconsistency.get_InconsistencyTypeIndex() == 1)
615
                        {
616
                            LMModelItem modelItem1 = relationship.Item1RepresentationObject == null ? null : relationship.Item1RepresentationObject.ModelItemObject;
617
                            LMModelItem modelItem2 = relationship.Item2RepresentationObject == null ? null : relationship.Item2RepresentationObject.ModelItemObject;
618
                            string[] array = inconsistency.get_Name().ToString().Split(new char[] { '=' });
619
                            if (modelItem1 != null)
620
                            {
621
                                string attrName = array[0];
622
                                if (attrName.Contains("PipingPoint"))
623
                                {
624
                                    string originalAttr = attrName.Split(new char[] { '.' })[1];
625
                                    int index = Convert.ToInt32(relationship.get_Item1Location());
626
                                    LMAAttribute attribute1 = modelItem1.Attributes["PipingPoint" + index + "." + originalAttr];
627
                                    if (attribute1 != null && !DBNull.Value.Equals(attribute1.get_Value()))
628
                                    {
629
                                        loop = true;
630
                                        attribute1.set_Value(DBNull.Value);
631
                                    }
632
                                    attribute1 = null;
633
                                }
634
                                else
635
                                {
636
                                    LMAAttribute attribute1 = modelItem1.Attributes[attrName];
637
                                    if (attribute1 != null && !DBNull.Value.Equals(attribute1.get_Value()))
638
                                    {
639
                                        loop = true;
640
                                        attribute1.set_Value(DBNull.Value);
641
                                    }
642
                                    attribute1 = null;
643
                                }
644
                                modelItem1.Commit();
645
                            }
646
                            if (modelItem2 != null)
647
                            {
648
                                string attrName = array[1];
649
                                if (attrName.Contains("PipingPoint"))
650
                                {
651
                                    string originalAttr = attrName.Split(new char[] { '.' })[1];
652
                                    int index = Convert.ToInt32(relationship.get_Item2Location());
653
                                    LMAAttribute attribute2 = modelItem2.Attributes["PipingPoint" + index + "." + originalAttr];
654
                                    if (attribute2 != null && !DBNull.Value.Equals(attribute2.get_Value()))
655
                                    {
656
                                        attribute2.set_Value(DBNull.Value);
657
                                        loop = true;
658
                                    }
659
                                    attribute2 = null;
660
                                }
661
                                else
662
                                {
663
                                    LMAAttribute attribute2 = modelItem2.Attributes[attrName];
664
                                    if (attribute2 != null && !DBNull.Value.Equals(attribute2.get_Value()))
665
                                    {
666
                                        attribute2.set_Value(DBNull.Value);
667
                                        loop = true;
668
                                    }
669
                                    attribute2 = null;
670
                                }
671
                                modelItem2.Commit();
672
                            }
673
                            ReleaseCOMObjects(modelItem1);
674
                            modelItem1 = null;
675
                            ReleaseCOMObjects(modelItem2);
676
                            modelItem2 = null;
677
                            inconsistency.Commit();
678
                        }
679
                        ReleaseCOMObjects(inconsistency);
680
                    }
681
                    relationship.Commit();
682
                    ReleaseCOMObjects(relationship);
683
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
684
                }
685
                ReleaseCOMObjects(filter);
686
                filter = null;
687
                ReleaseCOMObjects(criterion);
688
                criterion = null;
689
                ReleaseCOMObjects(relationships);
690
                relationships = null;
691
                count++;
692
            }
693
        }
694
        private void RunEndBreakModeling()
695
        {
696
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.EndBreaks.Count);
697
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
698
            foreach (var item in document.EndBreaks)
699
                try
700
                {
701
                    EndBreakModeling(item);
702
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
703
                }
704
                catch (Exception ex)
705
                {
706
                    Log.Write("Error in EndBreakModeling");
707
                    Log.Write("UID : " + item.UID);
708
                    Log.Write(ex.Message);
709
                    Log.Write(ex.StackTrace);
710
                }
711
        }
712
        private void RunSpecBreakModeling()
713
        {
714
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SpecBreaks.Count);
715
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "SpecBreaks Modeling");
716
            foreach (var item in document.SpecBreaks)
717
                try
718
                {
719
                    SpecBreakModeling(item);
720
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
721
                }
722
                catch (Exception ex)
723
                {
724
                    Log.Write("Error in SpecBreakModeling");
725
                    Log.Write("UID : " + item.UID);
726
                    Log.Write(ex.Message);
727
                    Log.Write(ex.StackTrace);
728
                }
729
        }
730
        private void RunJoinRunForSameConnector()
731
        {
732
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
733
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "PipeRun Join - 1");
734
            foreach (var line in document.LINES)
735
            {
736
                Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(line.SPPID.ModelItemId, false);
737
                List<List<double[]>> result = new List<List<double[]>>();
738
                foreach (var item in vertices)
739
                {
740
                    ReleaseCOMObjects(item.Key);
741
                    result.Add(item.Value);
742
                }
743
                line.SPPID.Vertices = result;
744
                vertices = null;
745
            }
746

    
747
            foreach (var line in document.LINES)
748
            {
749
                foreach (var connector in line.CONNECTORS)
750
                {
751
                    if (connector.ConnectedObject != null &&
752
                        connector.ConnectedObject.GetType() == typeof(Line) &&
753
                        !SPPIDUtil.IsBranchLine(line, connector.ConnectedObject as Line))
754
                    {
755
                        Line connLine = connector.ConnectedObject as Line;
756
                        if (line.SPPID.ModelItemId != connLine.SPPID.ModelItemId &&
757
                            !string.IsNullOrEmpty(line.SPPID.ModelItemId) &&
758
                            !string.IsNullOrEmpty(connLine.SPPID.ModelItemId) &&
759
                            !SPPIDUtil.IsSegment(document, line, connLine))
760
                        {
761
                            string survivorId = string.Empty;
762
                            JoinRun(connLine.SPPID.ModelItemId, line.SPPID.ModelItemId, ref survivorId);
763
                        }
764

    
765
                    }
766
                }
767
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
768
            }
769

    
770
            foreach (var line in document.LINES)
771
                line.SPPID.Representations = GetRepresentations(line.SPPID.ModelItemId);
772
        }
773
        private void RunJoinRun()
774
        {
775
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
776
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "PipeRun Join - 2");
777
            List<string> endModelID = new List<string>();
778
            foreach (var line in document.LINES)
779
            {
780
                if (!endModelID.Contains(line.SPPID.ModelItemId))
781
                {
782
                    while (!endModelID.Contains(line.SPPID.ModelItemId))
783
                    {
784
                        string survivorId = string.Empty;
785
                        JoinRunBySameType(line.SPPID.ModelItemId, ref survivorId);
786
                        if (string.IsNullOrEmpty(survivorId))
787
                        {
788
                            endModelID.Add(line.SPPID.ModelItemId);
789
                        }
790
                    }
791
                }
792
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
793
            }
794
        }
795
        private void RunLineNumberModeling()
796
        {
797
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINENUMBERS.Count);
798
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Number Modeling");
799
            foreach (var item in document.LINENUMBERS)
800
            {
801
                LMLabelPersist label = dataSource.GetLabelPersist(item.SPPID.RepresentationId);
802
                if (label == null || (label != null && label.get_ItemStatus() != "Active"))
803
                {
804
                    ReleaseCOMObjects(label);
805
                    item.SPPID.RepresentationId = null;
806
                    LineNumberModeling(item);
807
                }
808
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
809
            }
810
        }
811
        private void RunNoteModeling()
812
        {
813
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
814
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
815
            List<Note> correctList = new List<Note>();
816
            foreach (var item in document.NOTES)
817
                try
818
                {
819
                    NoteModeling(item, correctList);
820
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
821
                }
822
                catch (Exception ex)
823
                {
824
                    Log.Write("Error in NoteModeling");
825
                    Log.Write("UID : " + item.UID);
826
                    Log.Write(ex.Message);
827
                    Log.Write(ex.StackTrace);
828
                }
829

    
830
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, correctList.Count);
831
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Correct Note");
832
            SortNote(correctList);
833
            List<Note> endList = new List<Note>();
834
            if (correctList.Count > 0)
835
                endList.Add(correctList[0]);
836
            foreach (var item in correctList)
837
                try
838
                {
839
                    if (!endList.Contains(item))
840
                        NoteCorrectModeling(item, endList);
841
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
842
                }
843
                catch (Exception ex)
844
                {
845
                    Log.Write("Error in NoteModeling");
846
                    Log.Write("UID : " + item.UID);
847
                    Log.Write(ex.Message);
848
                    Log.Write(ex.StackTrace);
849
                }
850
        }
851
        private void RunTextModeling()
852
        {
853
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.TEXTINFOS.Count);
854
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
855
            SortText(document.TEXTINFOS);
856
            foreach (var item in document.TEXTINFOS)
857
                try
858
                {
859
                    if (item.ASSOCIATION)
860
                        AssociationTextModeling(item);
861
                    else
862
                        NormalTextModeling(item);
863
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
864
                }
865
                catch (Exception ex)
866
                {
867
                    Log.Write("Error in TextModeling");
868
                    Log.Write("UID : " + item.UID);
869
                    Log.Write(ex.Message);
870
                    Log.Write(ex.StackTrace);
871
                }
872
        }
873
        private void RunInputLineNumberAttribute()
874
        {
875
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINENUMBERS.Count);
876
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set LineNumbers Attribute");
877
            List<string> endLine = new List<string>();
878
            foreach (var item in document.LINENUMBERS)
879
                try
880
                {
881
                    InputLineNumberAttribute(item, endLine);
882
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
883
                }
884
                catch (Exception ex)
885
                {
886
                    Log.Write("Error in InputLineNumberAttribute");
887
                    Log.Write("UID : " + item.UID);
888
                    Log.Write(ex.Message);
889
                    Log.Write(ex.StackTrace);
890
                }
891
        }
892
        private void RunInputSymbolAttribute()
893
        {
894
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count + document.Equipments.Count + document.LINES.Count);
895
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
896
            foreach (var item in document.SYMBOLS)
897
                try
898
                {
899
                    InputSymbolAttribute(item, item.ATTRIBUTES);
900
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
901
                }
902
                catch (Exception ex)
903
                {
904
                    Log.Write("Error in InputSymbolAttribute");
905
                    Log.Write("UID : " + item.UID);
906
                    Log.Write(ex.Message);
907
                    Log.Write(ex.StackTrace);
908
                }
909

    
910
            foreach (var item in document.Equipments)
911
                try
912
                {
913
                    InputSymbolAttribute(item, item.ATTRIBUTES);
914
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
915
                }
916
                catch (Exception ex)
917
                {
918
                    Log.Write("Error in InputSymbolAttribute");
919
                    Log.Write("UID : " + item.UID);
920
                    Log.Write(ex.Message);
921
                    Log.Write(ex.StackTrace);
922
                }
923
            foreach (var item in document.LINES)
924
                try
925
                {
926
                    InputSymbolAttribute(item, item.ATTRIBUTES);
927
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
928
                }
929
                catch (Exception ex)
930
                {
931
                    Log.Write("Error in InputSymbolAttribute");
932
                    Log.Write("UID : " + item.UID);
933
                    Log.Write(ex.Message);
934
                    Log.Write(ex.StackTrace);
935
                }
936
        }
937
        private void RunInputSpecBreakAttribute()
938
        {
939
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SpecBreaks.Count);
940
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set SpecBreak Attribute");
941
            foreach (var item in document.SpecBreaks)
942
                try
943
                {
944
                    InputSpecBreakAttribute(item);
945
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
946
                }
947
                catch (Exception ex)
948
                {
949
                    Log.Write("Error in InputSpecBreakAttribute");
950
                    Log.Write("UID : " + item.UID);
951
                    Log.Write(ex.Message);
952
                    Log.Write(ex.StackTrace);
953
                }
954
        }
955
        private void RunInputEndBreakAttribute()
956
        {
957
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.EndBreaks.Count);
958
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set EndBreak Attribute");
959
            foreach (var item in document.EndBreaks)
960
                try
961
                {
962
                    InputEndBreakAttribute(item);
963
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
964
                }
965
                catch (Exception ex)
966
                {
967
                    Log.Write("Error in RunInputEndBreakAttribute");
968
                    Log.Write("UID : " + item.UID);
969
                    Log.Write(ex.Message);
970
                    Log.Write(ex.StackTrace);
971
                }
972
        }
973
        private void RunLabelSymbolModeling()
974
        {
975
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
976
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
977
            foreach (var item in document.SYMBOLS)
978
                try
979
                {
980
                    LabelSymbolModeling(item);
981
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
982
                }
983
                catch (Exception ex)
984
                {
985
                    Log.Write("Error in LabelSymbolModeling");
986
                    Log.Write("UID : " + item.UID);
987
                    Log.Write(ex.Message);
988
                    Log.Write(ex.StackTrace);
989
                }
990

    
991
        }
992
        private void RunCorrectAssociationText()
993
        {
994
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.TEXTINFOS.Count + document.LINENUMBERS.Count);
995
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Correct Labels");
996
            List<Text> endTexts = new List<Text>();
997
            foreach (var item in document.TEXTINFOS)
998
            {
999
                try
1000
                {
1001
                    if (item.ASSOCIATION && !endTexts.Contains(item))
1002
                        AssociationTextCorrectModeling(item, endTexts);
1003
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1004
                }
1005
                catch (Exception ex)
1006
                {
1007
                    Log.Write("Error in RunCorrectAssociationText");
1008
                    Log.Write("UID : " + item.UID);
1009
                    Log.Write(ex.Message);
1010
                    Log.Write(ex.StackTrace);
1011
                }
1012

    
1013
            }
1014

    
1015
            foreach (var item in document.LINENUMBERS)
1016
            {
1017
                try
1018
                {
1019
                    LineNumberCorrectModeling(item);
1020
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1021
                }
1022
                catch (Exception ex)
1023
                {
1024
                    Log.Write("Error in RunCorrectAssociationText");
1025
                    Log.Write("UID : " + item.UID);
1026
                    Log.Write(ex.Message);
1027
                    Log.Write(ex.StackTrace);
1028
                }
1029
            }
1030
        }
1031
        private void RunETC()
1032
        {
1033
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, FlowMarkRepIds.Count);
1034
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "ETC");
1035
            foreach (var item in FlowMarkRepIds)
1036
            {
1037
                LMLabelPersist label = dataSource.GetLabelPersist(item);
1038
                if (label != null)
1039
                {
1040
                    label.get_GraphicOID();
1041
                    DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[label.get_GraphicOID().ToString()] as DependencyObject;
1042
                    if (dependency != null)
1043
                        dependency.BringToFront();
1044
                }
1045
                ReleaseCOMObjects(label);
1046
                label = null;
1047
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1048
            }
1049
        }
1050
        private void RunBulkAttribute()
1051
        {
1052
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, 2);
1053
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Bulk Attribute");
1054

    
1055
            List<SPPIDModel.BulkAttribute> select = _ETCSetting.BulkAttributes.FindAll(x => x.RuleName.Equals(document.BulkAttributeName));
1056
            if (select.Count > 0)
1057
                SPPIDUtil.BulkAttribute(dataSource, select, BulkAttributeItemType.PipeRun);
1058
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1059
            if (select.Count > 0)
1060
                SPPIDUtil.BulkAttribute(dataSource, select, BulkAttributeItemType.Symbol);
1061
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1062
        }
1063
        /// <summary>
1064
        /// 도면 생성 메서드
1065
        /// </summary>
1066
        private bool CreateDocument(ref string drawingNumber, ref string drawingName)
1067
        {
1068
            Log.Write("------------------ Start create document ------------------");
1069
            GetDrawingNameAndNumber(ref drawingName, ref drawingNumber);
1070
            Log.Write("Drawing name : " + drawingName);
1071
            Log.Write("Drawing number : " + drawingNumber);
1072
            Thread.Sleep(1000);
1073
            newDrawing = application.Drawings.Add(document.Unit, document.Template, drawingNumber, drawingName);
1074
            if (newDrawing != null)
1075
            {
1076
                document.SPPID_DrawingNumber = drawingNumber;
1077
                document.SPPID_DrawingName = drawingName;
1078
                Thread.Sleep(1000);
1079
                radApp.ActiveWindow.Fit();
1080
                Thread.Sleep(1000);
1081
                radApp.ActiveWindow.Zoom = 2000;
1082
                Thread.Sleep(2000);
1083

    
1084
                //current LMDrawing 가져오기
1085
                LMAFilter filter = new LMAFilter();
1086
                LMACriterion criterion = new LMACriterion();
1087
                filter.ItemType = "Drawing";
1088
                criterion.SourceAttributeName = "Name";
1089
                criterion.Operator = "=";
1090
                criterion.set_ValueAttribute(drawingName);
1091
                filter.get_Criteria().Add(criterion);
1092

    
1093
                LMDrawings drawings = new LMDrawings();
1094
                drawings.Collect(dataSource, Filter: filter);
1095

    
1096
                // Input Drawing Attribute
1097
                LMDrawing drawing = ((dynamic)drawings).Nth(1);
1098
                if (drawing != null)
1099
                {
1100
                    using (DataTable drawingAttributeDT = Project_DB.SelectDrawingProjectAttribute())
1101
                    {
1102
                        foreach (DataRow row in drawingAttributeDT.Rows)
1103
                        {
1104
                            string mappingName = DBNull.Value.Equals(row["SPPID_ATTRIBUTE"]) ? string.Empty : row["SPPID_ATTRIBUTE"].ToString();
1105
                            if (!string.IsNullOrEmpty(mappingName))
1106
                            {
1107
                                string uid = row["UID"].ToString();
1108
                                string name = row["NAME"].ToString();
1109
                                Text text = document.TEXTINFOS.Find(x => x.AREA == uid);
1110
                                if (text != null)
1111
                                {
1112
                                    string value = text.VALUE;
1113
                                    LMAAttribute attribute = drawing.Attributes[mappingName];
1114
                                    if (attribute != null)
1115
                                        attribute.set_Value(value);
1116
                                    ReleaseCOMObjects(attribute);
1117
                                    document.TEXTINFOS.Remove(text);
1118
                                }
1119
                            }
1120
                        }
1121

    
1122
                        drawingAttributeDT.Dispose();
1123
                    }
1124

    
1125
                    ReleaseCOMObjects(drawing);
1126
                }
1127

    
1128
                drawingID = ((dynamic)drawings).Nth(1).Id;
1129
                ReleaseCOMObjects(filter);
1130
                ReleaseCOMObjects(criterion);
1131
                ReleaseCOMObjects(drawings);
1132
                filter = null;
1133
                criterion = null;
1134
                drawings = null;
1135
            }
1136
            else
1137
                Log.Write("Fail Create Drawing");
1138

    
1139
            if (newDrawing != null)
1140
            {
1141
                SetBorderFile();
1142
                return true;
1143
            }
1144
            else
1145
                return false;
1146
        }
1147

    
1148
        private void SetBorderFile()
1149
        {
1150
            ETCSetting setting = ETCSetting.GetInstance();
1151

    
1152
            if (!string.IsNullOrEmpty(setting.BorderFilePath) && System.IO.File.Exists(setting.BorderFilePath))
1153
            {
1154
                foreach (Ingr.RAD2D.SmartFrame2d smartFrame in radApp.ActiveDocument.ActiveSheet.SmartFrames2d)
1155
                {
1156
                    if (!string.IsNullOrEmpty(smartFrame.LinkMoniker) && smartFrame.LinkMoniker != setting.BorderFilePath)
1157
                    {
1158
                        smartFrame.ChangeSource(Ingr.RAD2D.OLEInsertionTypeConstant.igOLELinked, setting.BorderFilePath, true);
1159
                        smartFrame.Update();
1160
                    }
1161

    
1162
                }
1163
            }
1164
        }
1165

    
1166
        /// <summary>
1167
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
1168
        /// </summary>
1169
        /// <param name="drawingName"></param>
1170
        /// <param name="drawingNumber"></param>
1171
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
1172
        {
1173
            LMDrawings drawings = new LMDrawings();
1174
            drawings.Collect(dataSource);
1175

    
1176
            List<string> drawingNameList = new List<string>();
1177
            List<string> drawingNumberList = new List<string>();
1178

    
1179
            foreach (LMDrawing item in drawings)
1180
            {
1181
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
1182
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
1183
            }
1184

    
1185
            int nameLength = drawingName.Length;
1186
            while (drawingNameList.Contains(drawingName))
1187
            {
1188
                if (nameLength == drawingName.Length)
1189
                    drawingName += "-1";
1190
                else
1191
                {
1192
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
1193
                    drawingName = drawingName.Substring(0, nameLength + 1);
1194
                    drawingName += ++index;
1195
                }
1196
            }
1197

    
1198
            int numberLength = drawingNumber.Length;
1199
            while (drawingNameList.Contains(drawingNumber))
1200
            {
1201
                if (numberLength == drawingNumber.Length)
1202
                    drawingNumber += "-1";
1203
                else
1204
                {
1205
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
1206
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
1207
                    drawingNumber += ++index;
1208
                }
1209
            }
1210
            ReleaseCOMObjects(drawings);
1211
            drawings = null;
1212
        }
1213

    
1214
        /// <summary>
1215
        /// 도면 크기 구하는 메서드
1216
        /// </summary>
1217
        /// <returns></returns>
1218
        private bool DocumentCoordinateCorrection()
1219
        {
1220
            //if (radApp.ActiveDocument.ActiveSheet.SmartFrames2d.Count > 0)
1221
            //{
1222
            //    double x = 0;
1223
            //    double y = 0;
1224
            //    foreach (Ingr.RAD2D.SmartFrame2d smartFrame in radApp.ActiveDocument.ActiveSheet.SmartFrames2d)
1225
            //    {
1226
            //        x = Math.Max(smartFrame.CropRight, x);
1227
            //        y = Math.Max(smartFrame.CropTop, y);
1228
            //    }
1229
            //    document.SetSPPIDLocation(x, y);
1230
            //    document.CoordinateCorrection();
1231
            //    return true;
1232
            //}
1233
            //else
1234
            //{
1235
            //    Log.Write("Need Border!");
1236
            //    return false;
1237
            //}
1238

    
1239
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
1240
            {
1241
                Log.Write("Setting Drawing X, Drawing Y");
1242
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
1243
                Log.Write("Start coordinate correction");
1244
                document.CoordinateCorrection();
1245
                return true;
1246
            }
1247
            else
1248
            {
1249
                Log.Write("Need Drawing X, Y");
1250
                return false;
1251
            }
1252
        }
1253

    
1254
        /// <summary>
1255
        /// 심볼을 실제로 Modeling 메서드
1256
        /// </summary>
1257
        /// <param name="symbol">생성할 심볼</param>
1258
        /// <param name="targetSymbol">연결되어 있는 심볼</param>
1259
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
1260
        {
1261
            // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
1262
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
1263
            if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
1264
                return;
1265
            // 이미 모델링 됐을 경우
1266
            else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1267
                return;
1268

    
1269
            LMSymbol _LMSymbol = null;
1270

    
1271
            string mappingPath = symbol.SPPID.MAPPINGNAME;
1272
            double x = symbol.SPPID.ORIGINAL_X;
1273
            double y = symbol.SPPID.ORIGINAL_Y;
1274
            int mirror = 0;
1275
            double angle = symbol.ANGLE;
1276

    
1277
            // OPC 일경우 180도 일때 Mirror
1278
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
1279
                mirror = 1;
1280

    
1281
            // Mirror 계산
1282
            if (symbol.FLIP == 1)
1283
            {
1284
                mirror = 1;
1285
                if (angle == Math.PI || angle == 0)
1286
                    angle += Math.PI;
1287
            }
1288

    
1289
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
1290
            {
1291
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);   /// RepresentationId로 SPPID 심볼을 찾음
1292
                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
1293
                if (connector != null)
1294
                    GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
1295

    
1296
                LMConnector temp = LineModelingForSymbolZeroLength(symbol, _TargetItem, x, y);
1297
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
1298
                if (temp != null)
1299
                    _placement.PIDRemovePlacement(temp.AsLMRepresentation());
1300
                ReleaseCOMObjects(temp);
1301
                temp = null;
1302

    
1303
                if (_LMSymbol != null && _TargetItem != null)
1304
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1305

    
1306
                ReleaseCOMObjects(_TargetItem);
1307
            }
1308
            else
1309
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1310

    
1311
            if (_LMSymbol != null)
1312
            {
1313
                _LMSymbol.Commit();
1314

    
1315
                // ConnCheck
1316
                List<string> ids = new List<string>();
1317
                foreach (LMConnector item in _LMSymbol.Connect1Connectors)
1318
                {
1319
                    if (item.get_ItemStatus() == "Active" && !ids.Contains(item.Id))
1320
                        ids.Add(item.Id);
1321
                    ReleaseCOMObjects(item);
1322
                }
1323
                foreach (LMConnector item in _LMSymbol.Connect2Connectors)
1324
                {
1325
                    if (item.get_ItemStatus() == "Active" && !ids.Contains(item.Id))
1326
                        ids.Add(item.Id);
1327
                    ReleaseCOMObjects(item);
1328
                }
1329

    
1330
                int createdSymbolCount = document.SYMBOLS.FindAll(i => i.CONNECTORS.Find(j => j.CONNECTEDITEM == symbol.UID) != null && !string.IsNullOrEmpty(i.SPPID.RepresentationId)).Count;
1331
                if (targetSymbol == null && ids.Count != createdSymbolCount)
1332
                {
1333
                    double currentX = _LMSymbol.get_XCoordinate();
1334
                    double currentY = _LMSymbol.get_YCoordinate();
1335

    
1336

    
1337
                }
1338

    
1339
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1340
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
1341
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
1342

    
1343
                foreach (var item in symbol.ChildSymbols)
1344
                    CreateChildSymbol(item, _LMSymbol, symbol);
1345

    
1346
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
1347
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
1348

    
1349
                double[] range = null;
1350
                GetSPPIDSymbolRange(symbol, ref range);
1351
                symbol.SPPID.SPPID_Min_X = range[0];
1352
                symbol.SPPID.SPPID_Min_Y = range[1];
1353
                symbol.SPPID.SPPID_Max_X = range[2];
1354
                symbol.SPPID.SPPID_Max_Y = range[3];
1355

    
1356
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
1357
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
1358
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
1359
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
1360

    
1361
                ReleaseCOMObjects(_LMSymbol);
1362
            }
1363
        }
1364
        /// <summary>
1365
        /// targetX와 targetY 기준 제일 먼 PipingPoint에 TempLine Modeling
1366
        /// Signal Point는 고려하지 않음
1367
        /// </summary>
1368
        /// <param name="symbol"></param>
1369
        /// <param name="_TargetItem"></param>
1370
        /// <param name="targetX"></param>
1371
        /// <param name="targetY"></param>
1372
        /// <returns></returns>
1373
        private LMConnector LineModelingForSymbolZeroLength(Symbol symbol, LMSymbol _TargetItem, double targetX, double targetY)
1374
        {
1375
            LMConnector tempConnector = null;
1376

    
1377
            List<Symbol> group = new List<Symbol>();
1378
            SPPIDUtil.FindConnectedSymbolGroup(document, symbol, group);
1379
            if (group.FindAll(loopX => !string.IsNullOrEmpty(loopX.SPPID.RepresentationId)).Count == 1)
1380
            {
1381
                List<Connector> connectors = new List<Connector>();
1382
                foreach (var item in group)
1383
                    connectors.AddRange(item.CONNECTORS.FindAll(loopX => loopX.ConnectedObject != null && loopX.ConnectedObject.GetType() == typeof(Line)));
1384
                /// Primary or Secondary Type Line만 고려
1385
                Connector _connector = connectors.Find(loopX => loopX.ConnectedObject != null && loopX.ConnectedObject.GetType() == typeof(Line) &&
1386
                (((Line)loopX.ConnectedObject).TYPE == "Primary" || ((Line)loopX.ConnectedObject).TYPE == "Secondary"));
1387
                if (_connector != null)
1388
                {
1389
                    string sppidLine = ((Line)_connector.ConnectedObject).SPPID.MAPPINGNAME;
1390
                    List<double[]> pointInfos = getPipingPoints(_TargetItem);
1391
                    /// PipingPoint가 2개 이상만
1392
                    if (pointInfos.Count >= 2)
1393
                    {
1394
                        double lineX = 0;
1395
                        double lineY = 0;
1396
                        double length = 0;
1397
                        foreach (var item in pointInfos)
1398
                        {
1399
                            double tempX = item[1];
1400
                            double tempY = item[2];
1401

    
1402
                            double calcDistance = SPPIDUtil.CalcPointToPointdDistance(targetX, targetY, tempX, tempY);
1403
                            if (calcDistance > length)
1404
                            {
1405
                                lineX = tempX;
1406
                                lineY = tempY;
1407
                            }
1408
                        }
1409

    
1410
                        _LMAItem _LMAItem = _placement.PIDCreateItem(sppidLine);
1411
                        PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1412
                        placeRunInputs.AddPoint(-1, -1);
1413
                        placeRunInputs.AddSymbolTarget(_TargetItem, lineX, lineY);
1414
                        tempConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1415
                        if (tempConnector != null)
1416
                            tempConnector.Commit();
1417
                        ReleaseCOMObjects(_LMAItem);
1418
                        _LMAItem = null;
1419
                        ReleaseCOMObjects(placeRunInputs);
1420
                        placeRunInputs = null;
1421
                    }
1422
                }
1423
            }
1424

    
1425
            return tempConnector;
1426
        }
1427
        /// <summary>
1428
        /// Symbol의 PipingPoints를 구함
1429
        /// SignalPoint는 고려하지 않음
1430
        /// </summary>
1431
        /// <param name="symbol"></param>
1432
        /// <returns></returns>
1433
        private List<double[]> getPipingPoints(LMSymbol symbol)
1434
        {
1435
            LMModelItem modelItem = symbol.ModelItemObject;
1436
            LMPipingPoints pipingPoints = null;
1437
            if (modelItem.get_ItemTypeName() == "PipingComp")
1438
            {
1439
                LMPipingComp pipingComp = dataSource.GetPipingComp(modelItem.Id);
1440
                pipingPoints = pipingComp.PipingPoints;
1441
                ReleaseCOMObjects(pipingComp);
1442
                pipingComp = null;
1443
            }
1444
            else if (modelItem.get_ItemTypeName() == "Instrument")
1445
            {
1446
                LMInstrument instrument = dataSource.GetInstrument(modelItem.Id);
1447
                pipingPoints = instrument.PipingPoints;
1448
                ReleaseCOMObjects(instrument);
1449
                instrument = null;
1450
            }
1451
            else
1452
                Log.Write("다른 Type");
1453

    
1454
            List<double[]> info = new List<double[]>();
1455
            if (pipingPoints != null)
1456
            {
1457
                foreach (LMPipingPoint pipingPoint in pipingPoints)
1458
                {
1459
                    foreach (LMAAttribute attribute in pipingPoint.Attributes)
1460
                    {
1461
                        if (attribute.Name == "PipingPointNumber")
1462
                        {
1463
                            int index = Convert.ToInt32(attribute.get_Value());
1464
                            if (info.Find(loopX => loopX[0] == index) == null)
1465
                            {
1466
                                double x = 0;
1467
                                double y = 0;
1468
                                if (_placement.PIDConnectPointLocation(symbol, index, ref x, ref y))
1469
                                    info.Add(new double[] { index, x, y });
1470
                            }
1471
                        }
1472
                    }
1473
                }
1474
            }
1475
            ReleaseCOMObjects(modelItem);
1476
            modelItem = null;
1477
            ReleaseCOMObjects(pipingPoints);
1478
            pipingPoints = null;
1479

    
1480
            return info;
1481
        }
1482

    
1483
        private void RemoveSymbol(Symbol symbol)
1484
        {
1485
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1486
            {
1487
                LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1488
                if (_LMSymbol != null)
1489
                {
1490
                    _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
1491
                    ReleaseCOMObjects(_LMSymbol);
1492
                }
1493
            }
1494

    
1495
            symbol.SPPID.RepresentationId = string.Empty;
1496
            symbol.SPPID.ModelItemID = string.Empty;
1497
            symbol.SPPID.SPPID_X = double.NaN;
1498
            symbol.SPPID.SPPID_Y = double.NaN;
1499
            symbol.SPPID.SPPID_Min_X = double.NaN;
1500
            symbol.SPPID.SPPID_Min_Y = double.NaN;
1501
            symbol.SPPID.SPPID_Max_X = double.NaN;
1502
            symbol.SPPID.SPPID_Max_Y = double.NaN;
1503
        }
1504

    
1505
        private void RemoveSymbol(List<Symbol> symbols)
1506
        {
1507
            foreach (var symbol in symbols)
1508
            {
1509
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1510
                {
1511
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1512
                    if (_LMSymbol != null)
1513
                    {
1514
                        _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
1515
                        ReleaseCOMObjects(_LMSymbol);
1516
                    }
1517
                }
1518

    
1519
                symbol.SPPID.RepresentationId = string.Empty;
1520
                symbol.SPPID.ModelItemID = string.Empty;
1521
                symbol.SPPID.SPPID_X = double.NaN;
1522
                symbol.SPPID.SPPID_Y = double.NaN;
1523
                symbol.SPPID.SPPID_Min_X = double.NaN;
1524
                symbol.SPPID.SPPID_Min_Y = double.NaN;
1525
                symbol.SPPID.SPPID_Max_X = double.NaN;
1526
                symbol.SPPID.SPPID_Max_Y = double.NaN;
1527
            }
1528
        }
1529

    
1530
        /// <summary>
1531
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
1532
        /// </summary>
1533
        /// <param name="targetConnector"></param>
1534
        /// <param name="targetSymbol"></param>
1535
        /// <param name="x"></param>
1536
        /// <param name="y"></param>
1537
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
1538
        {
1539
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
1540

    
1541
            double[] range = null;
1542
            List<double[]> points = new List<double[]>();
1543
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
1544
            double x1 = range[0];
1545
            double y1 = range[1];
1546
            double x2 = range[2];
1547
            double y2 = range[3];
1548

    
1549
            // Origin 기준 Connector의 위치차이
1550
            double sceneX = 0;
1551
            double sceneY = 0;
1552
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
1553
            double originX = 0;
1554
            double originY = 0;
1555
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
1556
            double gapX = originX - sceneX;
1557
            double gapY = originY - sceneY;
1558

    
1559
            // SPPID Symbol과 ID2 심볼의 크기 차이
1560
            double sizeWidth = 0;
1561
            double sizeHeight = 0;
1562
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
1563
            if (sizeWidth == 0 || sizeHeight == 0)
1564
                throw new Exception("Check symbol size! \r\nUID : " + targetSymbol.UID);
1565

    
1566
            double percentX = (x2 - x1) / sizeWidth;
1567
            double percentY = (y2 - y1) / sizeHeight;
1568

    
1569
            double SPPIDgapX = gapX * percentX;
1570
            double SPPIDgapY = gapY * percentY;
1571

    
1572
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
1573
            double distance = double.MaxValue;
1574
            double[] resultPoint;
1575
            foreach (var point in points)
1576
            {
1577
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
1578
                if (distance > result)
1579
                {
1580
                    distance = result;
1581
                    resultPoint = point;
1582
                    x = point[0];
1583
                    y = point[1];
1584
                }
1585
            }
1586

    
1587
            ReleaseCOMObjects(_TargetItem);
1588
        }
1589

    
1590
        private void GetTargetLineConnectorPoint(Connector targetConnector, Line targetLine, ref double x, ref double y)
1591
        {
1592
            int index = targetLine.CONNECTORS.IndexOf(targetConnector);
1593
            if (index == 0)
1594
            {
1595
                x = targetLine.SPPID.START_X;
1596
                y = targetLine.SPPID.START_Y;
1597
            }
1598
            else
1599
            {
1600
                x = targetLine.SPPID.END_X;
1601
                y = targetLine.SPPID.END_Y;
1602
            }
1603
        }
1604

    
1605
        /// <summary>
1606
        /// SPPID Symbol의 Range를 구한다.
1607
        /// </summary>
1608
        /// <param name="symbol"></param>
1609
        /// <param name="range"></param>
1610
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
1611
        {
1612
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1613
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1614
            double x1 = 0;
1615
            double y1 = 0;
1616
            double x2 = 0;
1617
            double y2 = 0;
1618
            symbol2d.Range(out x1, out y1, out x2, out y2);
1619
            range = new double[] { x1, y1, x2, y2 };
1620

    
1621
            for (int i = 1; i < 30; i++)
1622
            {
1623
                double connX = 0;
1624
                double connY = 0;
1625
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
1626
                    points.Add(new double[] { connX, connY });
1627
            }
1628

    
1629
            foreach (var childSymbol in symbol.ChildSymbols)
1630
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
1631

    
1632
            ReleaseCOMObjects(_TargetItem);
1633
        }
1634

    
1635
        private void GetSPPIDSymbolRange(Symbol symbol, ref double[] range, bool bOnlySymbol = false, bool bForGraphic = false)
1636
        {
1637
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1638
            if (_TargetItem != null)
1639
            {
1640
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1641
                double x1 = 0;
1642
                double y1 = 0;
1643
                double x2 = 0;
1644
                double y2 = 0;
1645
                if (!bForGraphic)
1646
                {
1647
                    symbol2d.Range(out x1, out y1, out x2, out y2);
1648
                    range = new double[] { x1, y1, x2, y2 };
1649
                }
1650
                else
1651
                {
1652
                    x1 = double.MaxValue;
1653
                    y1 = double.MaxValue;
1654
                    x2 = double.MinValue;
1655
                    y2 = double.MinValue;
1656
                    range = new double[] { x1, y1, x2, y2 };
1657

    
1658
                    foreach (var item in symbol2d.DrawingObjects)
1659
                    {
1660
                        if (item.GetType() == typeof(Ingr.RAD2D.Line2d))
1661
                        {
1662
                            Ingr.RAD2D.Line2d rangeObject = item as Ingr.RAD2D.Line2d;
1663
                            if (rangeObject.Layer == "Default")
1664
                            {
1665
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1666
                                range = new double[] {
1667
                                Math.Min(x1, range[0]),
1668
                                Math.Min(y1, range[1]),
1669
                                Math.Max(x2, range[2]),
1670
                                Math.Max(y2, range[3])
1671
                            };
1672
                            }
1673
                        }
1674
                        else if (item.GetType() == typeof(Ingr.RAD2D.Circle2d))
1675
                        {
1676
                            Ingr.RAD2D.Circle2d rangeObject = item as Ingr.RAD2D.Circle2d;
1677
                            if (rangeObject.Layer == "Default")
1678
                            {
1679
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1680
                                range = new double[] {
1681
                                Math.Min(x1, range[0]),
1682
                                Math.Min(y1, range[1]),
1683
                                Math.Max(x2, range[2]),
1684
                                Math.Max(y2, range[3])
1685
                            };
1686
                            }
1687
                        }
1688
                        else if (item.GetType() == typeof(Ingr.RAD2D.Rectangle2d))
1689
                        {
1690
                            Ingr.RAD2D.Rectangle2d rangeObject = item as Ingr.RAD2D.Rectangle2d;
1691
                            if (rangeObject.Layer == "Default")
1692
                            {
1693
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1694
                                range = new double[] {
1695
                                Math.Min(x1, range[0]),
1696
                                Math.Min(y1, range[1]),
1697
                                Math.Max(x2, range[2]),
1698
                                Math.Max(y2, range[3])
1699
                            };
1700
                            }
1701
                        }
1702
                        else if (item.GetType() == typeof(Ingr.RAD2D.Arc2d))
1703
                        {
1704
                            Ingr.RAD2D.Arc2d rangeObject = item as Ingr.RAD2D.Arc2d;
1705
                            if (rangeObject.Layer == "Default")
1706
                            {
1707
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1708
                                range = new double[] {
1709
                                Math.Min(x1, range[0]),
1710
                                Math.Min(y1, range[1]),
1711
                                Math.Max(x2, range[2]),
1712
                                Math.Max(y2, range[3])
1713
                            };
1714
                            }
1715
                        }
1716
                    }
1717
                }
1718

    
1719
                if (!bOnlySymbol)
1720
                {
1721
                    foreach (var childSymbol in symbol.ChildSymbols)
1722
                        GetSPPIDChildSymbolRange(childSymbol, ref range);
1723
                }
1724
                ReleaseCOMObjects(_TargetItem);
1725
            }
1726
        }
1727

    
1728
        private void GetSPPIDSymbolRange(LMLabelPersist labelPersist, ref double[] range)
1729
        {
1730
            if (labelPersist != null)
1731
            {
1732
                double x1 = double.MaxValue;
1733
                double y1 = double.MaxValue;
1734
                double x2 = double.MinValue;
1735
                double y2 = double.MinValue;
1736
                range = new double[] { x1, y1, x2, y2 };
1737

    
1738
                Ingr.RAD2D.DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[labelPersist.get_GraphicOID().ToString()] as DependencyObject;
1739
                foreach (var item in dependency.DrawingObjects)
1740
                {
1741
                    Ingr.RAD2D.TextBox textBox = item as Ingr.RAD2D.TextBox;
1742
                    if (textBox != null)
1743
                    {
1744
                        if (dependency != null)
1745
                        {
1746
                            double tempX1;
1747
                            double tempY1;
1748
                            double tempX2;
1749
                            double tempY2;
1750
                            textBox.Range(out tempX1, out tempY1, out tempX2, out tempY2);
1751
                            x1 = Math.Min(x1, tempX1);
1752
                            y1 = Math.Min(y1, tempY1);
1753
                            x2 = Math.Max(x2, tempX2);
1754
                            y2 = Math.Max(y2, tempY2);
1755

    
1756
                            range = new double[] { x1, y1, x2, y2 };
1757
                        }
1758
                    }
1759
                }
1760

    
1761
            }
1762
        }
1763

    
1764
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range, bool bOnlySymbol = false, bool bForGraphic = false)
1765
        {
1766
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
1767
            foreach (var symbol in symbols)
1768
            {
1769
                double[] symbolRange = null;
1770
                GetSPPIDSymbolRange(symbol, ref symbolRange, bOnlySymbol, bForGraphic);
1771

    
1772
                tempRange[0] = Math.Min(tempRange[0], symbolRange[0]);
1773
                tempRange[1] = Math.Min(tempRange[1], symbolRange[1]);
1774
                tempRange[2] = Math.Max(tempRange[2], symbolRange[2]);
1775
                tempRange[3] = Math.Max(tempRange[3], symbolRange[3]);
1776

    
1777
                foreach (var childSymbol in symbol.ChildSymbols)
1778
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
1779
            }
1780

    
1781
            range = tempRange;
1782
        }
1783

    
1784
        /// <summary>
1785
        /// Child Modeling 된 Symbol의 Range를 구한다.
1786
        /// </summary>
1787
        /// <param name="childSymbol"></param>
1788
        /// <param name="range"></param>
1789
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
1790
        {
1791
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1792
            if (_ChildSymbol != null)
1793
            {
1794
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1795
                double x1 = 0;
1796
                double y1 = 0;
1797
                double x2 = 0;
1798
                double y2 = 0;
1799
                symbol2d.Range(out x1, out y1, out x2, out y2);
1800
                range[0] = Math.Min(range[0], x1);
1801
                range[1] = Math.Min(range[1], y1);
1802
                range[2] = Math.Max(range[2], x2);
1803
                range[3] = Math.Max(range[3], y2);
1804

    
1805
                for (int i = 1; i < int.MaxValue; i++)
1806
                {
1807
                    double connX = 0;
1808
                    double connY = 0;
1809
                    if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
1810
                        points.Add(new double[] { connX, connY });
1811
                    else
1812
                        break;
1813
                }
1814

    
1815
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1816
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
1817

    
1818
                ReleaseCOMObjects(_ChildSymbol);
1819
            }
1820
        }
1821

    
1822
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range)
1823
        {
1824
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1825
            if (_ChildSymbol != null)
1826
            {
1827
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1828
                double x1 = 0;
1829
                double y1 = 0;
1830
                double x2 = 0;
1831
                double y2 = 0;
1832
                symbol2d.Range(out x1, out y1, out x2, out y2);
1833
                range[0] = Math.Min(range[0], x1);
1834
                range[1] = Math.Min(range[1], y1);
1835
                range[2] = Math.Max(range[2], x2);
1836
                range[3] = Math.Max(range[3], y2);
1837

    
1838
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1839
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
1840
                ReleaseCOMObjects(_ChildSymbol);
1841
            }
1842
        }
1843

    
1844
        /// <summary>
1845
        /// Label Symbol Modeling
1846
        /// </summary>
1847
        /// <param name="symbol"></param>
1848
        private void LabelSymbolModeling(Symbol symbol)
1849
        {
1850
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1851
            {
1852
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
1853
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
1854
                    return;
1855
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
1856

    
1857
                string symbolUID = itemAttribute.VALUE;
1858
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
1859
                if (targetItem != null &&
1860
                    (targetItem.GetType() == typeof(Symbol) ||
1861
                    targetItem.GetType() == typeof(Equipment)))
1862
                {
1863
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
1864
                    string sRep = null;
1865
                    if (targetItem.GetType() == typeof(Symbol))
1866
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
1867
                    else if (targetItem.GetType() == typeof(Equipment))
1868
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
1869
                    if (!string.IsNullOrEmpty(sRep))
1870
                    {
1871
                        // LEADER Line 검사
1872
                        bool leaderLine = false;
1873
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1874
                        if (symbolMapping != null)
1875
                            leaderLine = symbolMapping.LEADERLINE;
1876

    
1877
                        // Target Symbol Item 가져오고 Label Modeling
1878
                        LMSymbol _TargetItem = dataSource.GetSymbol(sRep);
1879
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: symbol.ANGLE, LabeledItem: _TargetItem.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1880

    
1881
                        //Leader 선 센터로
1882
                        if (_LMLabelPresist != null)
1883
                        {
1884
                            // Target Item에 Label의 Attribute Input
1885
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1886

    
1887
                            string OID = _LMLabelPresist.get_GraphicOID().ToString();
1888
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
1889
                            if (dependency != null)
1890
                            {
1891
                                bool result = false;
1892
                                foreach (var attributes in dependency.AttributeSets)
1893
                                {
1894
                                    foreach (var attribute in attributes)
1895
                                    {
1896
                                        string name = attribute.Name;
1897
                                        string value = attribute.GetValue().ToString();
1898
                                        if (name == "DrawingItemType" && value == "LabelPersist")
1899
                                        {
1900
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
1901
                                            {
1902
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
1903
                                                {
1904
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
1905
                                                    double prevX = _TargetItem.get_XCoordinate();
1906
                                                    double prevY = _TargetItem.get_YCoordinate();
1907
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
1908
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
1909
                                                    result = true;
1910
                                                    break;
1911
                                                }
1912
                                            }
1913
                                        }
1914

    
1915
                                        if (result)
1916
                                            break;
1917
                                    }
1918

    
1919
                                    if (result)
1920
                                        break;
1921
                                }
1922
                            }
1923

    
1924
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1925
                            _LMLabelPresist.Commit();
1926
                            ReleaseCOMObjects(_LMLabelPresist);
1927
                        }
1928

    
1929
                        ReleaseCOMObjects(_TargetItem);
1930
                    }
1931
                }
1932
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
1933
                {
1934
                    Line targetLine = targetItem as Line;
1935
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
1936
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
1937
                    if (connectedLMConnector != null)
1938
                    {
1939
                        // Target Item에 Label의 Attribute Input
1940
                        InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1941

    
1942
                        // LEADER Line 검사
1943
                        bool leaderLine = false;
1944
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1945
                        if (symbolMapping != null)
1946
                            leaderLine = symbolMapping.LEADERLINE;
1947

    
1948
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: symbol.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1949
                        if (_LMLabelPresist != null)
1950
                        {
1951
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1952
                            _LMLabelPresist.Commit();
1953
                            ReleaseCOMObjects(_LMLabelPresist);
1954
                        }
1955
                        ReleaseCOMObjects(connectedLMConnector);
1956
                    }
1957

    
1958
                    foreach (var item in connectorVertices)
1959
                        if (item.Key != null)
1960
                            ReleaseCOMObjects(item.Key);
1961
                }
1962
            }
1963
        }
1964

    
1965
        /// <summary>
1966
        /// Equipment를 실제로 Modeling 메서드
1967
        /// </summary>
1968
        /// <param name="equipment"></param>
1969
        private void EquipmentModeling(Equipment equipment)
1970
        {
1971
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
1972
                return;
1973

    
1974
            LMSymbol _LMSymbol = null;
1975
            LMSymbol targetItem = null;
1976
            string mappingPath = equipment.SPPID.MAPPINGNAME;
1977
            double x = equipment.SPPID.ORIGINAL_X;
1978
            double y = equipment.SPPID.ORIGINAL_Y;
1979
            int mirror = 0;
1980
            double angle = equipment.ANGLE;
1981

    
1982
            // Mirror 계산
1983
            if (equipment.FLIP == 1)
1984
            {
1985
                mirror = 1;
1986
                if (angle == Math.PI || angle == 0)
1987
                    angle += Math.PI;
1988
            }
1989

    
1990
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
1991

    
1992
            Connector connector = equipment.CONNECTORS.Find(conn =>
1993
            !string.IsNullOrEmpty(conn.CONNECTEDITEM) &&
1994
            conn.CONNECTEDITEM != "None" &&
1995
            conn.ConnectedObject.GetType() == typeof(Equipment));
1996

    
1997
            if (connector != null)
1998
            {
1999
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
2000
                VendorPackage connVendorPackage = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as VendorPackage;
2001
                if (connEquipment != null)
2002
                {
2003
                    //if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
2004
                    //    EquipmentModeling(connEquipment);
2005

    
2006
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
2007
                    {
2008
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
2009
                        if (targetItem != null)
2010
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
2011
                        else
2012
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2013
                    }
2014
                    else
2015
                    {
2016
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2017
                    }
2018
                }
2019
                else if (connVendorPackage != null)
2020
                {
2021
                    if (!string.IsNullOrEmpty(connVendorPackage.SPPID.RepresentationId))
2022
                    {
2023
                        targetItem = dataSource.GetSymbol(connVendorPackage.SPPID.RepresentationId);
2024
                        if (targetItem != null)
2025
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
2026
                        else
2027
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2028
                    }
2029
                }
2030
                else
2031
                {
2032
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2033
                }
2034
            }
2035
            else
2036
            {
2037
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2038
            }
2039

    
2040
            if (_LMSymbol != null)
2041
            {
2042
                _LMSymbol.Commit();
2043
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
2044
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
2045
                ReleaseCOMObjects(_LMSymbol);
2046
            }
2047

    
2048
            if (targetItem != null)
2049
            {
2050
                ReleaseCOMObjects(targetItem);
2051
            }
2052

    
2053
            ReleaseCOMObjects(_LMSymbol);
2054
        }
2055

    
2056
        private void VendorPackageModeling(VendorPackage vendorPackage)
2057
        {
2058
            ETCSetting setting = ETCSetting.GetInstance();
2059
            if (!string.IsNullOrEmpty(setting.VendorPackageSymbolPath))
2060
            {
2061
                string symbolPath = setting.VendorPackageSymbolPath;
2062
                double x = vendorPackage.SPPID.ORIGINAL_X;
2063
                double y = vendorPackage.SPPID.ORIGINAL_Y;
2064

    
2065
                LMSymbol symbol = _placement.PIDPlaceSymbol(symbolPath, x, y);
2066
                if (symbol != null)
2067
                {
2068
                    symbol.Commit();
2069
                    vendorPackage.SPPID.RepresentationId = symbol.AsLMRepresentation().Id;
2070
                }
2071

    
2072
                ReleaseCOMObjects(symbol);
2073
                symbol = null;
2074
            }
2075
        }
2076

    
2077
        /// <summary>
2078
        /// 첫 진입점
2079
        /// </summary>
2080
        /// <param name="symbol"></param>
2081
        private void SymbolModelingBySymbol(Symbol symbol)
2082
        {
2083
            SymbolModeling(symbol, null);   /// 심볼을 생성한다
2084
            List<object> endObjects = new List<object>();
2085
            endObjects.Add(symbol);
2086

    
2087
            /// 심볼에 연결되어 있는 항목들을 모델링한다
2088
            foreach (var connector in symbol.CONNECTORS)
2089
            {
2090
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
2091
                if (connItem != null && connItem.GetType() != typeof(Equipment))
2092
                {
2093
                    endObjects.Add(connItem);
2094
                    if (connItem.GetType() == typeof(Symbol))
2095
                    {
2096
                        Symbol connSymbol = connItem as Symbol;
2097
                        if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
2098
                        {
2099
                            SymbolModeling(connSymbol, symbol);
2100
                        }
2101
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
2102
                        SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
2103
                    }
2104
                    else if (connItem.GetType() == typeof(Line))
2105
                    {
2106
                        Line connLine = connItem as Line;
2107
                        SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
2108
                    }
2109
                }
2110
            }
2111
        }
2112

    
2113
        private void SymbolModelingByNeerSymbolLoop(Symbol symbol, List<object> endObjects)
2114
        {
2115
            foreach (var connector in symbol.CONNECTORS)
2116
            {
2117
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
2118
                if (connItem != null && connItem.GetType() != typeof(Equipment))
2119
                {
2120
                    if (!endObjects.Contains(connItem))
2121
                    {
2122
                        endObjects.Add(connItem);
2123
                        if (connItem.GetType() == typeof(Symbol))
2124
                        {
2125
                            Symbol connSymbol = connItem as Symbol;
2126
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
2127
                            {
2128
                                SymbolModeling(connSymbol, symbol);
2129
                            }
2130
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
2131
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
2132
                        }
2133
                        else if (connItem.GetType() == typeof(Line))
2134
                        {
2135
                            Line connLine = connItem as Line;
2136
                            SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
2137
                        }
2138
                    }
2139
                }
2140
            }
2141
        }
2142

    
2143
        private void SymbolModelingByNeerLineLoop(Line line, List<object> endObjects, Symbol prevSymbol)
2144
        {
2145
            foreach (var connector in line.CONNECTORS)
2146
            {
2147
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
2148
                if (connItem != null && connItem.GetType() != typeof(Equipment))
2149
                {
2150
                    if (!endObjects.Contains(connItem))
2151
                    {
2152
                        endObjects.Add(connItem);
2153
                        if (connItem.GetType() == typeof(Symbol))
2154
                        {
2155
                            Symbol connSymbol = connItem as Symbol;
2156
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
2157
                            {
2158
                                Line connLine = SPPIDUtil.GetConnectedLine(prevSymbol, connSymbol);
2159
                                int branchCount = 0;
2160
                                if (connLine != null)
2161
                                    branchCount = SPPIDUtil.GetBranchLineCount(document, connLine);
2162

    
2163
                                List<Symbol> group = new List<Symbol>();
2164
                                SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
2165
                                Symbol priority = prioritySymbols.Find(x => group.Contains(x));
2166
                                List<Symbol> endModelingGroup = new List<Symbol>();
2167
                                if (priority != null)
2168
                                {
2169
                                    SymbolGroupModeling(priority, group);
2170

    
2171
                                    // Range 겹치는지 확인해야함
2172
                                    double[] prevRange = null;
2173
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange, bForGraphic: true);
2174
                                    double[] groupRange = null;
2175
                                    GetSPPIDSymbolRange(group, ref groupRange, bForGraphic: true);
2176

    
2177
                                    double distanceX = 0;
2178
                                    double distanceY = 0;
2179
                                    bool overlapX = false;
2180
                                    bool overlapY = false;
2181
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
2182
                                    SPPIDUtil.CalcOverlap(prevRange, groupRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
2183
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
2184
                                        (slopeType == SlopeType.VERTICAL && overlapY))
2185
                                    {
2186
                                        RemoveSymbol(group);
2187
                                        foreach (var _temp in group)
2188
                                            SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, distanceX, distanceY);
2189

    
2190
                                        SymbolGroupModeling(priority, group);
2191
                                    }
2192
                                    else if (branchCount > 0)
2193
                                    {
2194
                                        LMConnector _connector = JustLineModeling(connLine);
2195
                                        if (_connector != null)
2196
                                        {
2197
                                            double distance = GetConnectorDistance(_connector);
2198
                                            int cellCount = (int)Math.Truncate(distance / GridSetting.GetInstance().Length);
2199
                                            _placement.PIDRemovePlacement(_connector.AsLMRepresentation());
2200
                                            _connector.Commit();
2201
                                            ReleaseCOMObjects(_connector);
2202
                                            _connector = null;
2203
                                            if (cellCount < branchCount + 1)
2204
                                            {
2205
                                                int moveCount = branchCount - cellCount;
2206
                                                RemoveSymbol(group);
2207
                                                foreach (var _temp in group)
2208
                                                    SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, moveCount * GridSetting.GetInstance().Length, moveCount * GridSetting.GetInstance().Length);
2209

    
2210
                                                SymbolGroupModeling(priority, group);
2211
                                            }
2212
                                        }
2213
                                    }
2214
                                }
2215
                                else
2216
                                {
2217
                                    SymbolModeling(connSymbol, null);
2218
                                    // Range 겹치는지 확인해야함
2219
                                    double[] prevRange = null;
2220
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange, bForGraphic: true);
2221
                                    double[] connRange = null;
2222
                                    GetSPPIDSymbolRange(connSymbol, ref connRange, bForGraphic: true);
2223

    
2224
                                    double distanceX = 0;
2225
                                    double distanceY = 0;
2226
                                    bool overlapX = false;
2227
                                    bool overlapY = false;
2228
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
2229
                                    SPPIDUtil.CalcOverlap(prevRange, connRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
2230
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
2231
                                        (slopeType == SlopeType.VERTICAL && overlapY))
2232
                                    {
2233
                                        RemoveSymbol(connSymbol);
2234
                                        SPPIDUtil.CalcNewCoordinateForSymbol(connSymbol, prevSymbol, distanceX, distanceY);
2235

    
2236
                                        SymbolModeling(connSymbol, null);
2237
                                    }
2238
                                    else if (branchCount > 0)
2239
                                    {
2240
                                        LMConnector _connector = JustLineModeling(connLine);
2241
                                        if (_connector != null)
2242
                                        {
2243
                                            double distance = GetConnectorDistance(_connector);
2244
                                            int cellCount = (int)Math.Truncate(distance / GridSetting.GetInstance().Length);
2245
                                            _placement.PIDRemovePlacement(_connector.AsLMRepresentation());
2246
                                            _connector.Commit();
2247
                                            ReleaseCOMObjects(_connector);
2248
                                            _connector = null;
2249
                                            if (cellCount < branchCount + 1)
2250
                                            {
2251
                                                int moveCount = branchCount - cellCount;
2252
                                                RemoveSymbol(group);
2253
                                                foreach (var _temp in group)
2254
                                                    SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, moveCount * GridSetting.GetInstance().Length, moveCount * GridSetting.GetInstance().Length);
2255

    
2256
                                                SymbolGroupModeling(priority, group);
2257
                                            }
2258
                                        }
2259
                                    }
2260
                                }
2261
                            }
2262
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
2263
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
2264
                        }
2265
                        else if (connItem.GetType() == typeof(Line))
2266
                        {
2267
                            Line connLine = connItem as Line;
2268
                            if (!SPPIDUtil.IsBranchLine(connLine, line))
2269
                                SymbolModelingByNeerLineLoop(connLine, endObjects, prevSymbol);
2270
                        }
2271
                    }
2272
                }
2273
            }
2274
        }
2275

    
2276
        private void SymbolGroupModeling(Symbol firstSymbol, List<Symbol> group)
2277
        {
2278
            List<Symbol> endModelingGroup = new List<Symbol>();
2279
            SymbolModeling(firstSymbol, null);
2280
            endModelingGroup.Add(firstSymbol);
2281
            while (endModelingGroup.Count != group.Count)
2282
            {
2283
                foreach (var _symbol in group)
2284
                {
2285
                    if (!endModelingGroup.Contains(_symbol))
2286
                    {
2287
                        foreach (var _connector in _symbol.CONNECTORS)
2288
                        {
2289
                            Symbol _connSymbol = SPPIDUtil.FindObjectByUID(document, _connector.CONNECTEDITEM) as Symbol;
2290
                            if (_connSymbol != null && endModelingGroup.Contains(_connSymbol))
2291
                            {
2292
                                SymbolModeling(_symbol, _connSymbol);
2293
                                endModelingGroup.Add(_symbol);
2294
                                break;
2295
                            }
2296
                        }
2297
                    }
2298
                }
2299
            }
2300
        }
2301

    
2302
        /// <summary>
2303
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
2304
        /// </summary>
2305
        /// <param name="childSymbol"></param>
2306
        /// <param name="parentSymbol"></param>
2307
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol, Symbol parent)
2308
        {
2309
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
2310
            double x1 = 0;
2311
            double x2 = 0;
2312
            double y1 = 0;
2313
            double y2 = 0;
2314
            symbol2d.Range(out x1, out y1, out x2, out y2);
2315

    
2316
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
2317
            if (_LMSymbol != null)
2318
            {
2319
                _LMSymbol.Commit();
2320
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
2321
                foreach (var item in childSymbol.ChildSymbols)
2322
                    CreateChildSymbol(item, _LMSymbol, parent);
2323
            }
2324

    
2325

    
2326
            ReleaseCOMObjects(_LMSymbol);
2327
        }
2328
        double index = 0;
2329
        private void NewLineModeling(Line line, bool isBranchModeling = false)
2330
        {
2331
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) || (BranchLines.Contains(line) && !isBranchModeling))
2332
                return;
2333

    
2334
            List<Line> group = new List<Line>();
2335
            GetConnectedLineGroup(line, group);
2336
            LineCoordinateCorrection(group);
2337

    
2338
            foreach (var groupLine in group)
2339
            {
2340
                if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
2341
                {
2342
                    BranchLines.Add(groupLine);
2343
                    continue;
2344
                }
2345

    
2346
                bool diagonal = false;
2347
                if (groupLine.SlopeType != SlopeType.HORIZONTAL && groupLine.SlopeType != SlopeType.VERTICAL)
2348
                    diagonal = true;
2349
                _LMAItem _LMAItem = _placement.PIDCreateItem(groupLine.SPPID.MAPPINGNAME);
2350
                LMSymbol _LMSymbolStart = null;
2351
                LMSymbol _LMSymbolEnd = null;
2352
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2353
                foreach (var connector in groupLine.CONNECTORS)
2354
                {
2355
                    double x = 0;
2356
                    double y = 0;
2357
                    GetTargetLineConnectorPoint(connector, groupLine, ref x, ref y);
2358
                    if (connector.ConnectedObject == null)
2359
                        placeRunInputs.AddPoint(x, y);
2360
                    else if (connector.ConnectedObject.GetType() == typeof(Symbol))
2361
                    {
2362
                        Symbol targetSymbol = connector.ConnectedObject as Symbol;
2363
                        GetTargetSymbolConnectorPoint(targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == groupLine), targetSymbol, ref x, ref y);
2364
                        if (groupLine.CONNECTORS.IndexOf(connector) == 0)
2365
                        {
2366
                            _LMSymbolStart = GetTargetSymbol(targetSymbol, groupLine);
2367
                            if (_LMSymbolStart != null)
2368
                                placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y, diagonal);
2369
                            else
2370
                                placeRunInputs.AddPoint(x, y);
2371
                        }
2372
                        else
2373
                        {
2374
                            _LMSymbolEnd = GetTargetSymbol(targetSymbol, groupLine);
2375
                            if (_LMSymbolEnd != null)
2376
                                placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y, diagonal);
2377
                            else
2378
                                placeRunInputs.AddPoint(x, y);
2379
                        }
2380
                    }
2381
                    else if (connector.ConnectedObject.GetType() == typeof(Line))
2382
                    {
2383
                        Line targetLine = connector.ConnectedObject as Line;
2384
                        if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
2385
                        {
2386
                            LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
2387
                            if (targetConnector != null)
2388
                            {
2389
                                placeRunInputs.AddConnectorTarget(targetConnector, x, y, diagonal);
2390
                                ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
2391
                            }
2392
                            else
2393
                            {
2394
                                placeRunInputs.AddPoint(x, y);
2395
                                ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
2396
                            }
2397
                        }
2398
                        else
2399
                        {
2400
                            if (groupLine.CONNECTORS.IndexOf(connector) == 0)
2401
                            {
2402
                                index += 0.01;
2403
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
2404
                                    placeRunInputs.AddPoint(x, -0.1 - index);
2405
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
2406
                                    placeRunInputs.AddPoint(-0.1 - index, y);
2407
                                else
2408
                                {
2409
                                    Line nextLine = groupLine.CONNECTORS[0].ConnectedObject as Line;
2410
                                    if (SPPIDUtil.CalcAngle(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y) < 45)
2411
                                        placeRunInputs.AddPoint(-0.1 - index, y);
2412
                                    else
2413
                                        placeRunInputs.AddPoint(x, -0.1 - index);
2414
                                }
2415
                            }
2416

    
2417
                            placeRunInputs.AddPoint(x, y);
2418

    
2419
                            if (groupLine.CONNECTORS.IndexOf(connector) == 1)
2420
                            {
2421
                                index += 0.01;
2422
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
2423
                                    placeRunInputs.AddPoint(x, -0.1 - index);
2424
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
2425
                                    placeRunInputs.AddPoint(-0.1 - index, y);
2426
                                else
2427
                                {
2428
                                    Line nextLine = groupLine.CONNECTORS[1].ConnectedObject as Line;
2429
                                    if (SPPIDUtil.CalcAngle(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y) < 45)
2430
                                        placeRunInputs.AddPoint(-0.1 - index, y);
2431
                                    else
2432
                                        placeRunInputs.AddPoint(x, -0.1 - index);
2433
                                }
2434
                            }
2435
                        }
2436
                    }
2437
                    else
2438
                        placeRunInputs.AddPoint(x, y);
2439
                }
2440

    
2441
                LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2442
                if (_lMConnector != null)
2443
                {
2444
                    _lMConnector.Commit();
2445
                    groupLine.SPPID.ModelItemId = _lMConnector.ModelItemID;
2446

    
2447
                    bool bRemodelingStart = false;
2448
                    if (_LMSymbolStart != null)
2449
                        NeedReModeling(groupLine, _LMSymbolStart, ref bRemodelingStart);
2450
                    bool bRemodelingEnd = false;
2451
                    if (_LMSymbolEnd != null)
2452
                        NeedReModeling(groupLine, _LMSymbolEnd, ref bRemodelingEnd);
2453

    
2454
                    if (bRemodelingStart || bRemodelingEnd)
2455
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
2456

    
2457
                    FlowMarkModeling(groupLine);
2458

    
2459
                    ReleaseCOMObjects(_lMConnector);
2460

    
2461
                    LMModelItem modelItem = dataSource.GetModelItem(groupLine.SPPID.ModelItemId);
2462
                    if (modelItem != null)
2463
                    {
2464
                        LMAAttribute attribute = modelItem.Attributes["FlowDirection"];
2465
                        if (attribute != null)
2466
                            attribute.set_Value("End 1 is upstream (Inlet)");
2467
                        modelItem.Commit();
2468
                    }
2469
                    ReleaseCOMObjects(modelItem);
2470
                    modelItem = null;
2471
                }
2472
                else if (!isBranchModeling)
2473
                {
2474
                    Log.Write("Main Line Modeling : " + groupLine.UID);
2475
                }
2476

    
2477
                List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
2478
                x.ConnectedObject != null &&
2479
                x.ConnectedObject.GetType() == typeof(Line) &&
2480
                !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
2481
                .Select(x => x.ConnectedObject)
2482
                .ToList();
2483

    
2484
                foreach (var item in removeLines)
2485
                    RemoveLineForModeling(item as Line);
2486

    
2487
                ReleaseCOMObjects(_LMAItem);
2488
                _LMAItem = null;
2489
                ReleaseCOMObjects(placeRunInputs);
2490
                placeRunInputs = null;
2491
                ReleaseCOMObjects(_LMSymbolStart);
2492
                _LMSymbolStart = null;
2493
                ReleaseCOMObjects(_LMSymbolEnd);
2494
                _LMSymbolEnd = null;
2495

    
2496
                if (isBranchModeling && BranchLines.Contains(groupLine))
2497
                    BranchLines.Remove(groupLine);
2498
            }
2499
        }
2500

    
2501
        private LMConnector JustLineModeling(Line line)
2502
        {
2503
            bool diagonal = false;
2504
            if (line.SlopeType != SlopeType.HORIZONTAL && line.SlopeType != SlopeType.VERTICAL)
2505
                diagonal = true;
2506
            _LMAItem _LMAItem = _placement.PIDCreateItem(line.SPPID.MAPPINGNAME);
2507
            LMSymbol _LMSymbolStart = null;
2508
            LMSymbol _LMSymbolEnd = null;
2509
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2510
            foreach (var connector in line.CONNECTORS)
2511
            {
2512
                double x = 0;
2513
                double y = 0;
2514
                GetTargetLineConnectorPoint(connector, line, ref x, ref y);
2515
                if (connector.ConnectedObject == null)
2516
                {
2517
                    placeRunInputs.AddPoint(x, y);
2518
                }
2519
                else if (connector.ConnectedObject.GetType() == typeof(Symbol))
2520
                {
2521
                    Symbol targetSymbol = connector.ConnectedObject as Symbol;
2522
                    GetTargetSymbolConnectorPoint(targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line), targetSymbol, ref x, ref y);
2523
                    if (line.CONNECTORS.IndexOf(connector) == 0)
2524
                    {
2525
                        _LMSymbolStart = GetTargetSymbol(targetSymbol, line);
2526
                        if (_LMSymbolStart != null)
2527
                            placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y, diagonal);
2528
                        else
2529
                            placeRunInputs.AddPoint(x, y);
2530
                    }
2531
                    else
2532
                    {
2533
                        _LMSymbolEnd = GetTargetSymbol(targetSymbol, line);
2534
                        if (_LMSymbolEnd != null)
2535
                            placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y, diagonal);
2536
                        else
2537
                            placeRunInputs.AddPoint(x, y);
2538
                    }
2539
                }
2540
                else if (connector.ConnectedObject.GetType() == typeof(Line))
2541
                {
2542
                    Line targetLine = connector.ConnectedObject as Line;
2543
                    if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
2544
                    {
2545
                        LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
2546
                        if (targetConnector != null)
2547
                        {
2548
                            placeRunInputs.AddConnectorTarget(targetConnector, x, y, diagonal);
2549
                            ChangeLineSPPIDCoordinateByConnector(line, targetLine, x, y, false);
2550
                        }
2551
                        else
2552
                        {
2553
                            placeRunInputs.AddPoint(x, y);
2554
                            ChangeLineSPPIDCoordinateByConnector(line, targetLine, x, y, false);
2555
                        }
2556
                    }
2557
                    else
2558
                        placeRunInputs.AddPoint(x, y);
2559
                }
2560
            }
2561

    
2562
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2563
            if (_lMConnector != null)
2564
                _lMConnector.Commit();
2565

    
2566
            ReleaseCOMObjects(_LMAItem);
2567
            _LMAItem = null;
2568
            ReleaseCOMObjects(placeRunInputs);
2569
            placeRunInputs = null;
2570
            ReleaseCOMObjects(_LMSymbolStart);
2571
            _LMSymbolStart = null;
2572
            ReleaseCOMObjects(_LMSymbolEnd);
2573
            _LMSymbolEnd = null;
2574

    
2575
            return _lMConnector;
2576
        }
2577

    
2578
        private void RemoveLineForModeling(Line line)
2579
        {
2580
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2581
            if (modelItem != null)
2582
            {
2583
                foreach (LMRepresentation rep in modelItem.Representations)
2584
                {
2585
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2586
                    {
2587
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2588
                        dynamic OID = rep.get_GraphicOID().ToString();
2589
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2590
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2591
                        int verticesCount = lineStringGeometry.VertexCount;
2592
                        double[] vertices = null;
2593
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2594
                        for (int i = 0; i < verticesCount; i++)
2595
                        {
2596
                            double x = 0;
2597
                            double y = 0;
2598
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2599
                            if (verticesCount == 2 && (x < 0 || y < 0))
2600
                                _placement.PIDRemovePlacement(rep);
2601
                        }
2602
                        ReleaseCOMObjects(_LMConnector);
2603
                    }
2604
                }
2605

    
2606
                ReleaseCOMObjects(modelItem);
2607
            }
2608
        }
2609

    
2610
        private void RemoveLine(Line line)
2611
        {
2612
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2613
            if (modelItem != null)
2614
            {
2615
                foreach (LMRepresentation rep in modelItem.Representations)
2616
                {
2617
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2618
                        _placement.PIDRemovePlacement(rep);
2619
                }
2620
                ReleaseCOMObjects(modelItem);
2621
            }
2622
            line.SPPID.ModelItemId = null;
2623
        }
2624

    
2625
        private void GetConnectedLineGroup(Line line, List<Line> group)
2626
        {
2627
            if (!group.Contains(line))
2628
                group.Add(line);
2629
            foreach (var connector in line.CONNECTORS)
2630
            {
2631
                if (connector.ConnectedObject != null &&
2632
                    connector.ConnectedObject.GetType() == typeof(Line) &&
2633
                    !group.Contains(connector.ConnectedObject) &&
2634
                    string.IsNullOrEmpty(((Line)connector.ConnectedObject).SPPID.ModelItemId))
2635
                {
2636
                    Line connLine = connector.ConnectedObject as Line;
2637
                    if (!SPPIDUtil.IsBranchLine(connLine, line))
2638
                    {
2639
                        if (line.CONNECTORS.IndexOf(connector) == 0)
2640
                            group.Insert(0, connLine);
2641
                        else
2642
                            group.Add(connLine);
2643
                        GetConnectedLineGroup(connLine, group);
2644
                    }
2645
                }
2646
            }
2647
        }
2648

    
2649
        private void LineCoordinateCorrection(List<Line> group)
2650
        {
2651
            // 순서대로 전 Item 기준 정렬
2652
            LineCoordinateCorrectionByStart(group);
2653

    
2654
            // 역으로 심볼이 있을 경우 좌표 보정
2655
            LineCoordinateCorrectionForLastLine(group);
2656
        }
2657

    
2658
        private void LineCoordinateCorrectionByStart(List<Line> group)
2659
        {
2660
            for (int i = 0; i < group.Count; i++)
2661
            {
2662
                Line line = group[i];
2663
                if (i == 0)
2664
                {
2665
                    Connector symbolConnector = line.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
2666
                    if (symbolConnector != null)
2667
                        LineCoordinateCorrectionByConnItem(line, symbolConnector.ConnectedObject);
2668
                }
2669
                else if (i != 0)
2670
                {
2671
                    LineCoordinateCorrectionByConnItem(line, group[i - 1]);
2672
                }
2673
            }
2674
        }
2675

    
2676
        private void LineCoordinateCorrectionForLastLine(List<Line> group)
2677
        {
2678
            Line checkLine = group[group.Count - 1];
2679
            Connector lastSymbolConnector = checkLine.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
2680
            if (lastSymbolConnector != null)
2681
            {
2682
                LineCoordinateCorrectionByConnItem(checkLine, lastSymbolConnector.ConnectedObject);
2683
                for (int i = group.Count - 2; i >= 0; i--)
2684
                {
2685
                    Line line = group[i + 1];
2686
                    Line prevLine = group[i];
2687

    
2688
                    // 같으면 보정
2689
                    if (line.SlopeType == prevLine.SlopeType)
2690
                        LineCoordinateCorrectionByConnItem(prevLine, line);
2691
                    else
2692
                    {
2693
                        if (line.SlopeType == SlopeType.HORIZONTAL)
2694
                        {
2695
                            double prevX = 0;
2696
                            double prevY = 0;
2697
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
2698
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(line, prevLine, prevX);
2699

    
2700
                            double x = 0;
2701
                            double y = 0;
2702
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
2703
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(prevLine, line, y);
2704
                        }
2705
                        else if (line.SlopeType == SlopeType.VERTICAL)
2706
                        {
2707
                            double prevX = 0;
2708
                            double prevY = 0;
2709
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
2710
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(line, prevLine, prevY);
2711

    
2712
                            double x = 0;
2713
                            double y = 0;
2714
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
2715
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
2716
                        }
2717
                        break;
2718
                    }
2719
                }
2720
            }
2721
        }
2722

    
2723
        private void LineCoordinateCorrectionByConnItem(Line line, object connItem)
2724
        {
2725
            double x = 0;
2726
            double y = 0;
2727
            if (connItem.GetType() == typeof(Symbol))
2728
            {
2729
                Symbol targetSymbol = connItem as Symbol;
2730
                Connector targetConnector = targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line);
2731
                if (targetConnector != null)
2732
                    GetTargetSymbolConnectorPoint(targetConnector, targetSymbol, ref x, ref y);
2733
                else
2734
                    throw new Exception("Target symbol UID : " + targetSymbol.UID + "\r\nLine UID : " + line.UID);
2735
            }
2736
            else if (connItem.GetType() == typeof(Line))
2737
            {
2738
                Line targetLine = connItem as Line;
2739
                GetTargetLineConnectorPoint(targetLine.CONNECTORS.Find(z => z.ConnectedObject == line), targetLine, ref x, ref y);
2740
            }
2741

    
2742
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
2743
        }
2744

    
2745
        private void ChangeLineSPPIDCoordinateByConnector(Line line, object connItem, double x, double y, bool changeOtherCoordinate = true)
2746
        {
2747
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2748
            int index = line.CONNECTORS.IndexOf(connector);
2749
            if (index == 0)
2750
            {
2751
                line.SPPID.START_X = x;
2752
                line.SPPID.START_Y = y;
2753
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
2754
                    line.SPPID.END_Y = y;
2755
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
2756
                    line.SPPID.END_X = x;
2757
            }
2758
            else
2759
            {
2760
                line.SPPID.END_X = x;
2761
                line.SPPID.END_Y = y;
2762
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
2763
                    line.SPPID.START_Y = y;
2764
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
2765
                    line.SPPID.START_X = x;
2766
            }
2767
        }
2768

    
2769
        private void ChangeLineSPPIDCoordinateByConnectorOnlyX(Line line, object connItem, double x)
2770
        {
2771
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2772
            int index = line.CONNECTORS.IndexOf(connector);
2773
            if (index == 0)
2774
            {
2775
                line.SPPID.START_X = x;
2776
                if (line.SlopeType == SlopeType.VERTICAL)
2777
                    line.SPPID.END_X = x;
2778
            }
2779
            else
2780
            {
2781
                line.SPPID.END_X = x;
2782
                if (line.SlopeType == SlopeType.VERTICAL)
2783
                    line.SPPID.START_X = x;
2784
            }
2785
        }
2786

    
2787
        private void ChangeLineSPPIDCoordinateByConnectorOnlyY(Line line, object connItem, double y)
2788
        {
2789
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2790
            int index = line.CONNECTORS.IndexOf(connector);
2791
            if (index == 0)
2792
            {
2793
                line.SPPID.START_Y = y;
2794
                if (line.SlopeType == SlopeType.HORIZONTAL)
2795
                    line.SPPID.END_Y = y;
2796
            }
2797
            else
2798
            {
2799
                line.SPPID.END_Y = y;
2800
                if (line.SlopeType == SlopeType.HORIZONTAL)
2801
                    line.SPPID.START_Y = y;
2802
            }
2803
        }
2804

    
2805
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
2806
        {
2807
            if (symbol != null)
2808
            {
2809
                string repID = symbol.AsLMRepresentation().Id;
2810
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
2811
                string lineUID = line.UID;
2812

    
2813
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
2814
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
2815
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
2816

    
2817
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
2818
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
2819
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
2820

    
2821
                if (startSpecBreak != null || startEndBreak != null)
2822
                    result = true;
2823
            }
2824
        }
2825

    
2826
        /// <summary>
2827
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
2828
        /// </summary>
2829
        /// <param name="lines"></param>
2830
        /// <param name="prevLMConnector"></param>
2831
        /// <param name="startSymbol"></param>
2832
        /// <param name="endSymbol"></param>
2833
        private void ReModelingLine(Line line, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
2834
        {
2835
            string symbolPath = string.Empty;
2836
            #region get symbol path
2837
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
2838
            symbolPath = GetSPPIDFileName(modelItem);
2839
            ReleaseCOMObjects(modelItem);
2840
            #endregion
2841
            bool diagonal = false;
2842
            if (line.SlopeType != SlopeType.HORIZONTAL && line.SlopeType != SlopeType.VERTICAL)
2843
                diagonal = true;
2844
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2845
            LMConnector newConnector = null;
2846
            dynamic OID = prevLMConnector.get_GraphicOID().ToString();
2847
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2848
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2849
            int verticesCount = lineStringGeometry.VertexCount;
2850
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2851

    
2852
            List<double[]> vertices = new List<double[]>();
2853
            for (int i = 1; i <= verticesCount; i++)
2854
            {
2855
                double x = 0;
2856
                double y = 0;
2857
                lineStringGeometry.GetVertex(i, ref x, ref y);
2858
                vertices.Add(new double[] { x, y });
2859
            }
2860

    
2861
            for (int i = 0; i < vertices.Count; i++)
2862
            {
2863
                double[] points = vertices[i];
2864
                // 시작 심볼이 있고 첫번째 좌표일 때
2865
                if (startSymbol != null && i == 0)
2866
                {
2867
                    if (bStart)
2868
                    {
2869
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
2870
                        if (slopeType == SlopeType.HORIZONTAL)
2871
                            placeRunInputs.AddPoint(points[0], -0.1);
2872
                        else if (slopeType == SlopeType.VERTICAL)
2873
                            placeRunInputs.AddPoint(-0.1, points[1]);
2874
                        else
2875
                            placeRunInputs.AddPoint(points[0], -0.1);
2876

    
2877
                        placeRunInputs.AddPoint(points[0], points[1]);
2878
                    }
2879
                    else
2880
                    {
2881
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1], diagonal);
2882
                    }
2883
                }
2884
                // 마지막 심볼이 있고 마지막 좌표일 때
2885
                else if (endSymbol != null && i == vertices.Count - 1)
2886
                {
2887
                    if (bEnd)
2888
                    {
2889
                        placeRunInputs.AddPoint(points[0], points[1]);
2890

    
2891
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
2892
                        if (slopeType == SlopeType.HORIZONTAL)
2893
                            placeRunInputs.AddPoint(points[0], -0.1);
2894
                        else if (slopeType == SlopeType.VERTICAL)
2895
                            placeRunInputs.AddPoint(-0.1, points[1]);
2896
                        else
2897
                            placeRunInputs.AddPoint(points[0], -0.1);
2898
                    }
2899
                    else
2900
                    {
2901
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1], diagonal);
2902
                    }
2903
                }
2904
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
2905
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
2906
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1], diagonal);
2907
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
2908
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
2909
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1], diagonal);
2910
                else
2911
                    placeRunInputs.AddPoint(points[0], points[1]);
2912
            }
2913

    
2914
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
2915
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2916

    
2917
            ReleaseCOMObjects(placeRunInputs);
2918
            ReleaseCOMObjects(_LMAItem);
2919
            ReleaseCOMObjects(modelItem);
2920

    
2921
            if (newConnector != null)
2922
            {
2923
                newConnector.Commit();
2924
                if (startSymbol != null && bStart)
2925
                {
2926
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2927
                    placeRunInputs = new PlaceRunInputs();
2928
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
2929
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
2930
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2931
                    if (_LMConnector != null)
2932
                    {
2933
                        _LMConnector.Commit();
2934
                        RemoveConnectorForReModelingLine(newConnector);
2935
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
2936
                        ReleaseCOMObjects(_LMConnector);
2937
                    }
2938
                    ReleaseCOMObjects(placeRunInputs);
2939
                    ReleaseCOMObjects(_LMAItem);
2940
                }
2941

    
2942
                if (endSymbol != null && bEnd)
2943
                {
2944
                    if (startSymbol != null)
2945
                    {
2946
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
2947
                        newConnector = dicVertices.First().Key;
2948
                    }
2949

    
2950
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2951
                    placeRunInputs = new PlaceRunInputs();
2952
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2953
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2954
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2955
                    if (_LMConnector != null)
2956
                    {
2957
                        _LMConnector.Commit();
2958
                        RemoveConnectorForReModelingLine(newConnector);
2959
                        ZeroLengthModelItemIDReverse.Add(_LMConnector.ModelItemID);
2960
                        ReleaseCOMObjects(_LMConnector);
2961
                    }
2962
                    ReleaseCOMObjects(placeRunInputs);
2963
                    ReleaseCOMObjects(_LMAItem);
2964
                }
2965

    
2966
                line.SPPID.ModelItemId = newConnector.ModelItemID;
2967
                ReleaseCOMObjects(newConnector);
2968
            }
2969

    
2970
            ReleaseCOMObjects(modelItem);
2971
        }
2972

    
2973
        /// <summary>
2974
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
2975
        /// </summary>
2976
        /// <param name="connector"></param>
2977
        private void RemoveConnectorForReModelingLine(LMConnector connector)
2978
        {
2979
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
2980
            foreach (var item in dicVertices)
2981
            {
2982
                if (item.Value.Count == 2)
2983
                {
2984
                    bool result = false;
2985
                    foreach (var point in item.Value)
2986
                    {
2987
                        if (point[0] < 0 || point[1] < 0)
2988
                        {
2989
                            result = true;
2990
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
2991
                            break;
2992
                        }
2993
                    }
2994

    
2995
                    if (result)
2996
                        break;
2997
                }
2998
            }
2999
            foreach (var item in dicVertices)
3000
                ReleaseCOMObjects(item.Key);
3001
        }
3002

    
3003
        /// <summary>
3004
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
3005
        /// </summary>
3006
        /// <param name="symbol"></param>
3007
        /// <param name="line"></param>
3008
        /// <returns></returns>
3009
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
3010
        {
3011
            LMSymbol _LMSymbol = null;
3012
            foreach (var connector in symbol.CONNECTORS)
3013
            {
3014
                if (connector.CONNECTEDITEM == line.UID)
3015
                {
3016
                    if (connector.Index == 0)
3017
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3018
                    else
3019
                    {
3020
                        ChildSymbol child = null;
3021
                        foreach (var childSymbol in symbol.ChildSymbols)
3022
                        {
3023
                            if (childSymbol.Connectors.Contains(connector))
3024
                                child = childSymbol;
3025
                            else
3026
                                child = GetChildSymbolByConnector(childSymbol, connector);
3027

    
3028
                            if (child != null)
3029
                                break;
3030
                        }
3031

    
3032
                        if (child != null)
3033
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
3034
                    }
3035

    
3036
                    break;
3037
                }
3038
            }
3039

    
3040
            return _LMSymbol;
3041
        }
3042

    
3043
        /// <summary>
3044
        /// Connector를 가지고 있는 ChildSymbol Object 반환
3045
        /// </summary>
3046
        /// <param name="item"></param>
3047
        /// <param name="connector"></param>
3048
        /// <returns></returns>
3049
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
3050
        {
3051
            foreach (var childSymbol in item.ChildSymbols)
3052
            {
3053
                if (childSymbol.Connectors.Contains(connector))
3054
                    return childSymbol;
3055
                else
3056
                    return GetChildSymbolByConnector(childSymbol, connector);
3057
            }
3058

    
3059
            return null;
3060
        }
3061

    
3062
        /// <summary>
3063
        /// EndBreak 모델링 메서드
3064
        /// </summary>
3065
        /// <param name="endBreak"></param>
3066
        private void EndBreakModeling(EndBreak endBreak)
3067
        {
3068
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
3069
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
3070

    
3071
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
3072
            if (ownerObj.GetType() == typeof(Symbol) && connectedItem.GetType() == typeof(Symbol) && targetLMConnector != null)
3073
                targetLMConnector = ReModelingZeroLengthLMConnectorForSegment(targetLMConnector);
3074

    
3075
            if (targetLMConnector != null)
3076
            {
3077
                // LEADER Line 검사
3078
                bool leaderLine = false;
3079
                SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == endBreak.DBUID);
3080
                if (symbolMapping != null)
3081
                    leaderLine = symbolMapping.LEADERLINE;
3082

    
3083
                SegmentLocation location;
3084
                double[] point = GetSegmentPoint(ownerObj, connectedItem, targetLMConnector, out location);
3085
                Array array = null;
3086
                if (point != null)
3087
                    array = new double[] { 0, point[0], point[1] };
3088
                else
3089
                    array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
3090
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
3091
                if (_LmLabelPersist != null)
3092
                {
3093
                    _LmLabelPersist.Commit();
3094
                    endBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
3095
                    if (_LmLabelPersist.ModelItemObject != null)
3096
                        endBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
3097
                    endBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
3098

    
3099
                    MoveDependencyObject(endBreak.SPPID.GraphicOID, location);
3100

    
3101
                    ReleaseCOMObjects(_LmLabelPersist);
3102
                }
3103
                ReleaseCOMObjects(targetLMConnector);
3104
            }
3105
            else
3106
            {
3107
                Log.Write("End Break UID : " + endBreak.UID);
3108
                Log.Write("Can't find targetLMConnector");
3109
            }
3110
        }
3111

    
3112
        private void MoveDependencyObject(string graphicOID, SegmentLocation location)
3113
        {
3114
            double x = 0, y = 0;
3115
            if (location.HasFlag(SegmentLocation.Up))
3116
                y = GridSetting.GetInstance().Length * 3;
3117
            else if (location.HasFlag(SegmentLocation.Down))
3118
                y = -GridSetting.GetInstance().Length * 3;
3119

    
3120
            if (location.HasFlag(SegmentLocation.Right))
3121
                x = GridSetting.GetInstance().Length * 3;
3122
            else if (location.HasFlag(SegmentLocation.Left))
3123
                x = -GridSetting.GetInstance().Length * 3;
3124

    
3125
            if (x != 0 || y != 0)
3126
            {
3127
                radApp.ActiveSelectSet.RemoveAll();
3128
                DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] as DependencyObject;
3129
                if (dependency != null)
3130
                {
3131
                    radApp.ActiveSelectSet.Add(dependency);
3132
                    Ingr.RAD2D.Transform transform = dependency.GetTransform();
3133
                    transform.DefineByMove2d(x, y);
3134
                    radApp.ActiveSelectSet.Transform(transform, true);
3135
                    radApp.ActiveSelectSet.RemoveAll();
3136
                }
3137
            }
3138
        }
3139

    
3140
        private LMConnector ReModelingZeroLengthLMConnectorForSegment(LMConnector connector, string changeSymbolPath = null)
3141
        {
3142
            string symbolPath = string.Empty;
3143
            #region get symbol path
3144
            if (string.IsNullOrEmpty(changeSymbolPath))
3145
            {
3146
                LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
3147
                symbolPath = GetSPPIDFileName(modelItem);
3148
                ReleaseCOMObjects(modelItem);
3149
            }
3150
            else
3151
                symbolPath = changeSymbolPath;
3152

    
3153
            #endregion
3154

    
3155
            LMConnector newConnector = null;
3156
            dynamic OID = connector.get_GraphicOID().ToString();
3157
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
3158
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3159
            int verticesCount = lineStringGeometry.VertexCount;
3160
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
3161
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
3162

    
3163
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
3164
            {
3165
                double[] vertices = null;
3166
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
3167
                double x = 0;
3168
                double y = 0;
3169
                lineStringGeometry.GetVertex(1, ref x, ref y);
3170

    
3171
                string flowDirection = string.Empty;
3172
                LMAAttribute flowAttribute = connector.ModelItemObject.Attributes["FlowDirection"];
3173
                if (flowAttribute != null && !DBNull.Value.Equals(flowAttribute.get_Value()))
3174
                    flowDirection = flowAttribute.get_Value().ToString();
3175

    
3176
                if (flowDirection == "End 1 is downstream (Outlet)")
3177
                {
3178
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
3179
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
3180
                    flowDirection = "End 1 is upstream (Inlet)";
3181
                }
3182
                else
3183
                {
3184
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
3185
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
3186
                }
3187
                string oldModelItemId = connector.ModelItemID;
3188
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
3189
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
3190
                newConnector.Commit();
3191
                ZeroLengthSymbolToSymbolModelItemID.Add(newConnector.ModelItemID);
3192
                if (!string.IsNullOrEmpty(flowDirection))
3193
                    newConnector.ModelItemObject.Attributes["FlowDirection"].set_Value(flowDirection);
3194
                ReleaseCOMObjects(connector);
3195

    
3196
                foreach (var line in document.LINES.FindAll(z => z.SPPID.ModelItemId == oldModelItemId))
3197
                {
3198
                    foreach (var repId in line.SPPID.Representations)
3199
                    {
3200
                        LMConnector _connector = dataSource.GetConnector(repId);
3201
                        if (_connector != null && _connector.get_ItemStatus() == "Active")
3202
                        {
3203
                            if (line.SPPID.ModelItemId != _connector.ModelItemID)
3204
                            {
3205
                                line.SPPID.ModelItemId = _connector.ModelItemID;
3206
                                line.SPPID.Representations = GetRepresentations(line.SPPID.ModelItemId);
3207
                            }
3208
                        }
3209
                        ReleaseCOMObjects(_connector);
3210
                        _connector = null;
3211
                    }
3212
                }
3213
            }
3214

    
3215
            return newConnector;
3216
        }
3217

    
3218
        /// <summary>
3219
        /// SpecBreak Modeling 메서드
3220
        /// </summary>
3221
        /// <param name="specBreak"></param>
3222
        private void SpecBreakModeling(SpecBreak specBreak)
3223
        {
3224
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
3225
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
3226

    
3227
            if (upStreamObj != null &&
3228
                downStreamObj != null)
3229
            {
3230
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
3231
                if (upStreamObj.GetType() == typeof(Symbol) && downStreamObj.GetType() == typeof(Symbol) &&
3232
                    targetLMConnector != null &&
3233
                    !IsModelingEndBreak(upStreamObj as Symbol, downStreamObj as Symbol))
3234
                    targetLMConnector = ReModelingZeroLengthLMConnectorForSegment(targetLMConnector);
3235

    
3236
                if (targetLMConnector != null)
3237
                {
3238
                    foreach (var attribute in specBreak.ATTRIBUTES)
3239
                    {
3240
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
3241
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
3242
                        {
3243
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
3244
                            SegmentLocation location;
3245
                            double[] point = GetSegmentPoint(upStreamObj, downStreamObj, targetLMConnector, out location);
3246
                            Array array = null;
3247
                            if (point != null)
3248
                                array = new double[] { 0, point[0], point[1] };
3249
                            else
3250
                                array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
3251
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3252

    
3253
                            if (_LmLabelPersist != null)
3254
                            {
3255
                                _LmLabelPersist.Commit();
3256
                                specBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
3257
                                if (_LmLabelPersist.ModelItemObject != null)
3258
                                    specBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
3259
                                specBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
3260

    
3261
                                MoveDependencyObject(specBreak.SPPID.GraphicOID, location);
3262

    
3263
                                ReleaseCOMObjects(_LmLabelPersist);
3264
                            }
3265
                        }
3266
                    }
3267

    
3268
                    Property property = specBreak.PROPERTIES.Find(loop => loop.ATTRIBUTE == "Show");
3269
                    if (property != null && !string.IsNullOrEmpty(property.VALUE) && property.VALUE.Equals("True"))
3270
                    {
3271
                        // temp
3272
                        ReleaseCOMObjects(_placement.PIDPlaceSymbol(@"\Design\Annotation\Graphics\Break.sym", specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y, Rotation: specBreak.ANGLE));
3273
                    }
3274
                    ReleaseCOMObjects(targetLMConnector);
3275
                }
3276
                else
3277
                {
3278
                    Log.Write("Spec Break UID : " + specBreak.UID);
3279
                    Log.Write("Can't find targetLMConnector");
3280
                }
3281
            }
3282
        }
3283

    
3284
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
3285
        {
3286
            LMConnector targetConnector = null;
3287
            Symbol targetSymbol = targetObj as Symbol;
3288
            Symbol connectedSymbol = connectedObj as Symbol;
3289
            Line targetLine = targetObj as Line;
3290
            Line connectedLine = connectedObj as Line;
3291
            if (targetSymbol != null && connectedSymbol != null)
3292
            {
3293
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
3294
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
3295

    
3296
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
3297
                {
3298
                    if (connector.get_ItemStatus() != "Active")
3299
                        continue;
3300

    
3301
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
3302
                    {
3303
                        targetConnector = connector;
3304
                        break;
3305
                    }
3306
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
3307
                    {
3308
                        targetConnector = connector;
3309
                        break;
3310
                    }
3311
                }
3312

    
3313
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
3314
                {
3315
                    if (connector.get_ItemStatus() != "Active")
3316
                        continue;
3317

    
3318
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
3319
                    {
3320
                        targetConnector = connector;
3321
                        break;
3322
                    }
3323
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
3324
                    {
3325
                        targetConnector = connector;
3326
                        break;
3327
                    }
3328
                }
3329

    
3330
                ReleaseCOMObjects(targetLMSymbol);
3331
                ReleaseCOMObjects(connectedLMSymbol);
3332
            }
3333
            else if (targetLine != null && connectedLine != null)
3334
            {
3335
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
3336
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
3337

    
3338
                if (targetModelItem != null && targetModelItem.get_ItemStatus() == "Active" && connectedModelItem != null && connectedModelItem.get_ItemStatus() == "Active")
3339
                {
3340
                    foreach (LMRepresentation rep in targetModelItem.Representations)
3341
                    {
3342
                        if (targetConnector != null)
3343
                            break;
3344

    
3345
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3346
                        {
3347
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3348

    
3349
                            if (IsConnected(_LMConnector, connectedModelItem))
3350
                                targetConnector = _LMConnector;
3351
                            else
3352
                                ReleaseCOMObjects(_LMConnector);
3353
                        }
3354
                    }
3355

    
3356
                    ReleaseCOMObjects(targetModelItem);
3357
                }
3358
            }
3359
            else
3360
            {
3361
                LMSymbol connectedLMSymbol = null;
3362
                if (connectedSymbol != null)
3363
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
3364
                else if (targetSymbol != null)
3365
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
3366
                else
3367
                {
3368

    
3369
                }
3370
                LMModelItem targetModelItem = null;
3371
                if (targetLine != null)
3372
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
3373
                else if (connectedLine != null)
3374
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
3375
                else
3376
                {
3377

    
3378
                }
3379
                if (connectedLMSymbol != null && targetModelItem != null)
3380
                {
3381
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
3382
                    {
3383
                        if (connector.get_ItemStatus() != "Active")
3384
                            continue;
3385

    
3386
                        if (IsConnected(connector, targetModelItem))
3387
                        {
3388
                            targetConnector = connector;
3389
                            break;
3390
                        }
3391
                    }
3392

    
3393
                    if (targetConnector == null)
3394
                    {
3395
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
3396
                        {
3397
                            if (connector.get_ItemStatus() != "Active")
3398
                                continue;
3399

    
3400
                            if (IsConnected(connector, targetModelItem))
3401
                            {
3402
                                targetConnector = connector;
3403
                                break;
3404
                            }
3405
                        }
3406
                    }
3407
                }
3408

    
3409
            }
3410

    
3411
            return targetConnector;
3412
        }
3413

    
3414
        private double[] GetSegmentPoint(object targetObj, object connObj, LMConnector targetConnector, out SegmentLocation location)
3415
        {
3416
            double[] result = null;
3417
            Line targetLine = targetObj as Line;
3418
            Symbol targetSymbol = targetObj as Symbol;
3419
            Line connLine = connObj as Line;
3420
            Symbol connSymbol = connObj as Symbol;
3421
            location = SegmentLocation.None;
3422
            if (Convert.ToBoolean(targetConnector.get_IsZeroLength()))
3423
            {
3424
                result = GetConnectorVertices(targetConnector)[0];
3425
                if (targetSymbol != null && connSymbol != null)
3426
                {
3427
                    SlopeType slopeType = SPPIDUtil.CalcSlope(targetSymbol.SPPID.SPPID_X, targetSymbol.SPPID.SPPID_Y, connSymbol.SPPID.SPPID_X, connSymbol.SPPID.SPPID_Y);
3428
                    result = new double[] { result[0], result[1] };
3429
                    if (slopeType == SlopeType.HORIZONTAL)
3430
                        location = SegmentLocation.Up;
3431
                    else if (slopeType == SlopeType.VERTICAL)
3432
                        location = SegmentLocation.Right;
3433
                }
3434
                else if (targetLine != null)
3435
                {
3436
                    result = new double[] { result[0], result[1] };
3437
                    if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3438
                        location = SegmentLocation.Up;
3439
                    else if (targetLine.SlopeType == SlopeType.VERTICAL)
3440
                        location = SegmentLocation.Right;
3441
                }
3442
                else if (connLine != null)
3443
                {
3444
                    result = new double[] { result[0], result[1] };
3445
                    if (connLine.SlopeType == SlopeType.HORIZONTAL)
3446
                        location = SegmentLocation.Up;
3447
                    else if (connLine.SlopeType == SlopeType.VERTICAL)
3448
                        location = SegmentLocation.Right;
3449
                }
3450
            }
3451
            else
3452
            {
3453
                if (targetObj.GetType() == typeof(Line) && connObj.GetType() == typeof(Line))
3454
                {
3455
                    Line line = connObj as Line;
3456
                    LMConnector connectedConnector = null;
3457
                    int connIndex = 0;
3458
                    LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3459
                    FindConnectedConnector(targetConnector, modelItem, ref connectedConnector, ref connIndex);
3460

    
3461
                    List<double[]> vertices = GetConnectorVertices(targetConnector);
3462

    
3463
                    ReleaseCOMObjects(modelItem);
3464
                    ReleaseCOMObjects(connectedConnector);
3465

    
3466
                    if (vertices.Count > 0)
3467
                    {
3468
                        if (connIndex == 1)
3469
                            result = vertices[0];
3470
                        else if (connIndex == 2)
3471
                            result = vertices[vertices.Count - 1];
3472

    
3473
                        if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3474
                        {
3475
                            result = new double[] { result[0], result[1] };
3476
                            location = SegmentLocation.Up;
3477
                            if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X < targetLine.SPPID.END_X)
3478
                                location = location | SegmentLocation.Right;
3479
                            else if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X > targetLine.SPPID.END_X)
3480
                                location = location | SegmentLocation.Left;
3481
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X < targetLine.SPPID.END_X)
3482
                                location = location | SegmentLocation.Left;
3483
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X > targetLine.SPPID.END_X)
3484
                                location = location | SegmentLocation.Right;
3485
                        }
3486
                        else if (targetLine.SlopeType == SlopeType.VERTICAL)
3487
                        {
3488
                            result = new double[] { result[0], result[1] };
3489
                            location = SegmentLocation.Right;
3490
                            if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y < targetLine.SPPID.END_Y)
3491
                                location = location | SegmentLocation.Up;
3492
                            else if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y > targetLine.SPPID.END_Y)
3493
                                location = location | SegmentLocation.Down;
3494
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y < targetLine.SPPID.END_Y)
3495
                                location = location | SegmentLocation.Down;
3496
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y > targetLine.SPPID.END_Y)
3497
                                location = location | SegmentLocation.Up;
3498
                        }
3499

    
3500
                    }
3501
                }
3502
                else
3503
                {
3504
                    Log.Write("error in GetSegemtPoint");
3505
                }
3506
            }
3507

    
3508
            return result;
3509
        }
3510

    
3511
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
3512
        {
3513
            bool result = false;
3514

    
3515
            foreach (LMRepresentation rep in modelItem.Representations)
3516
            {
3517
                if (result)
3518
                    break;
3519

    
3520
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3521
                {
3522
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3523

    
3524
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
3525
                        connector.ConnectItem1SymbolObject != null &&
3526
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3527
                    {
3528
                        result = true;
3529
                        ReleaseCOMObjects(_LMConnector);
3530
                        break;
3531
                    }
3532
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
3533
                        connector.ConnectItem2SymbolObject != null &&
3534
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3535
                    {
3536
                        result = true;
3537
                        ReleaseCOMObjects(_LMConnector);
3538
                        break;
3539
                    }
3540
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3541
                        connector.ConnectItem1SymbolObject != null &&
3542
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3543
                    {
3544
                        result = true;
3545
                        ReleaseCOMObjects(_LMConnector);
3546
                        break;
3547
                    }
3548
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3549
                        connector.ConnectItem2SymbolObject != null &&
3550
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3551
                    {
3552
                        result = true;
3553
                        ReleaseCOMObjects(_LMConnector);
3554
                        break;
3555
                    }
3556

    
3557
                    ReleaseCOMObjects(_LMConnector);
3558
                }
3559
            }
3560

    
3561

    
3562
            return result;
3563
        }
3564

    
3565
        private void FindConnectedConnector(LMConnector connector, LMModelItem modelItem, ref LMConnector connectedConnector, ref int connectorIndex)
3566
        {
3567
            foreach (LMRepresentation rep in modelItem.Representations)
3568
            {
3569
                if (connectedConnector != null)
3570
                    break;
3571

    
3572
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3573
                {
3574
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3575

    
3576
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
3577
                        connector.ConnectItem1SymbolObject != null &&
3578
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3579
                    {
3580
                        connectedConnector = _LMConnector;
3581
                        connectorIndex = 1;
3582
                        break;
3583
                    }
3584
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
3585
                        connector.ConnectItem2SymbolObject != null &&
3586
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3587
                    {
3588
                        connectedConnector = _LMConnector;
3589
                        connectorIndex = 2;
3590
                        break;
3591
                    }
3592
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3593
                        connector.ConnectItem1SymbolObject != null &&
3594
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3595
                    {
3596
                        connectedConnector = _LMConnector;
3597
                        connectorIndex = 1;
3598
                        break;
3599
                    }
3600
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3601
                        connector.ConnectItem2SymbolObject != null &&
3602
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3603
                    {
3604
                        connectedConnector = _LMConnector;
3605
                        connectorIndex = 2;
3606
                        break;
3607
                    }
3608

    
3609
                    if (connectedConnector == null)
3610
                        ReleaseCOMObjects(_LMConnector);
3611
                }
3612
            }
3613
        }
3614

    
3615
        /// <summary>
3616
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
3617
        /// </summary>
3618
        /// <param name="modelItemID1"></param>
3619
        /// <param name="modelItemID2"></param>
3620
        private void JoinRun(string modelId1, string modelId2, ref string survivorId, bool IsSameConnector = true)
3621
        {
3622
            try
3623
            {
3624
                LMModelItem modelItem1 = dataSource.GetModelItem(modelId1);
3625
                LMConnector connector1 = GetLMConnectorFirst(modelId1);
3626
                List<double[]> vertices1 = null;
3627
                string graphicOID1 = string.Empty;
3628
                if (connector1 != null)
3629
                {
3630
                    vertices1 = GetConnectorVertices(connector1);
3631
                    graphicOID1 = connector1.get_GraphicOID();
3632
                }
3633
                _LMAItem item1 = modelItem1.AsLMAItem();
3634
                ReleaseCOMObjects(connector1);
3635
                connector1 = null;
3636

    
3637
                LMModelItem modelItem2 = dataSource.GetModelItem(modelId2);
3638
                LMConnector connector2 = GetLMConnectorFirst(modelId2);
3639
                List<double[]> vertices2 = null;
3640
                string graphicOID2 = string.Empty;
3641
                if (connector2 != null)
3642
                {
3643
                    vertices2 = GetConnectorVertices(connector2);
3644
                    graphicOID2 = connector2.get_GraphicOID();
3645
                }
3646
                _LMAItem item2 = modelItem2.AsLMAItem();
3647
                ReleaseCOMObjects(connector2);
3648
                connector2 = null;
3649

    
3650
                // item2가 item1으로 조인
3651
                _placement.PIDJoinRuns(ref item1, ref item2);
3652
                item1.Commit();
3653
                item2.Commit();
3654

    
3655
                string beforeID = string.Empty;
3656
                string afterID = string.Empty;
3657

    
3658
                if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() != "Active")
3659
                {
3660
                    beforeID = modelItem2.Id;
3661
                    afterID = modelItem1.Id;
3662
                    survivorId = afterID;
3663
                }
3664
                else if (modelItem1.get_ItemStatus() != "Active" && modelItem2.get_ItemStatus() == "Active")
3665
                {
3666
                    beforeID = modelItem1.Id;
3667
                    afterID = modelItem2.Id;
3668
                    survivorId = afterID;
3669
                }
3670
                else if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() == "Active")
3671
                {
3672
                    int model1Cnt = GetConnectorCount(modelId1);
3673
                    int model2Cnt = GetConnectorCount(modelId2);
3674
                    if (model1Cnt == 0)
3675
                    {
3676
                        beforeID = modelItem1.Id;
3677
                        afterID = modelItem2.Id;
3678
                        survivorId = afterID;
3679
                    }
3680
                    else if (model2Cnt == 0)
3681
                    {
3682
                        beforeID = modelItem2.Id;
3683
                        afterID = modelItem1.Id;
3684
                        survivorId = afterID;
3685
                    }
3686
                    else
3687
                        survivorId = null;
3688
                }
3689
                else
3690
                {
3691
                    Log.Write("잘못된 경우");
3692
                    survivorId = null;
3693
                }
3694

    
3695
                if (!string.IsNullOrEmpty(beforeID) && !string.IsNullOrEmpty(afterID))
3696
                {
3697
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, beforeID);
3698
                    foreach (var line in lines)
3699
                        line.SPPID.ModelItemId = afterID;
3700
                }
3701

    
3702
                ReleaseCOMObjects(modelItem1);
3703
                ReleaseCOMObjects(item1);
3704
                ReleaseCOMObjects(modelItem2);
3705
                ReleaseCOMObjects(item2);
3706
            }
3707
            catch (Exception ex)
3708
            {
3709
                Log.Write("Join Error");
3710
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3711
            }
3712
        }
3713

    
3714
        private bool IsModelingEndBreak(Symbol symbol1, Symbol symbol2)
3715
        {
3716
            bool result = false;
3717
            List<EndBreak> endBreaks = document.EndBreaks.FindAll(x =>
3718
           (x.OWNER == symbol1.UID || x.OWNER == symbol2.UID) &&
3719
           (x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbol1.UID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbol2.UID));
3720

    
3721
            foreach (var item in endBreaks)
3722
            {
3723
                if (!string.IsNullOrEmpty(item.SPPID.RepresentationId))
3724
                {
3725
                    result = true;
3726
                    break;
3727
                }
3728
            }
3729

    
3730
            return result;
3731
        }
3732
        private List<string> FindOtherModelItemBySymbolWhereTypePipeRun(LMSymbol symbol, string modelId)
3733
        {
3734
            List<string> temp = new List<string>();
3735
            List<LMConnector> connectors = new List<LMConnector>();
3736
            foreach (LMConnector connector in symbol.Avoid1Connectors)
3737
            {
3738
                if (connector.get_ItemStatus() != "Active")
3739
                    continue;
3740

    
3741
                LMModelItem modelItem = connector.ModelItemObject;
3742
                LMSymbol connOtherSymbol = FindOtherConnectedSymbol(connector);
3743
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId && !temp.Contains(modelItem.Id))
3744
                    temp.Add(modelItem.Id);
3745

    
3746
                if (temp.Contains(modelItem.Id) &&
3747
                    connOtherSymbol != null &&
3748
                    connOtherSymbol.get_RepresentationType() == "Branch" &&
3749
                    Convert.ToBoolean(connector.get_IsZeroLength()))
3750
                    temp.Remove(modelItem.Id);
3751

    
3752

    
3753
                if (temp.Contains(modelItem.Id))
3754
                    connectors.Add(connector);
3755
                ReleaseCOMObjects(connOtherSymbol);
3756
                connOtherSymbol = null;
3757
                ReleaseCOMObjects(modelItem);
3758
                modelItem = null;
3759
            }
3760

    
3761
            foreach (LMConnector connector in symbol.Avoid2Connectors)
3762
            {
3763
                if (connector.get_ItemStatus() != "Active")
3764
                    continue;
3765

    
3766
                LMModelItem modelItem = connector.ModelItemObject;
3767
                LMSymbol connOtherSymbol = FindOtherConnectedSymbol(connector);
3768
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId && !temp.Contains(modelItem.Id))
3769
                    temp.Add(modelItem.Id);
3770

    
3771
                if (temp.Contains(modelItem.Id) &&
3772
                    connOtherSymbol != null &&
3773
                    connOtherSymbol.get_RepresentationType() == "Branch" &&
3774
                    Convert.ToBoolean(connector.get_IsZeroLength()))
3775
                    temp.Remove(modelItem.Id);
3776

    
3777
                if (temp.Contains(modelItem.Id))
3778
                    connectors.Add(connector);
3779
                ReleaseCOMObjects(connOtherSymbol);
3780
                connOtherSymbol = null;
3781
                ReleaseCOMObjects(modelItem);
3782
                modelItem = null;
3783
            }
3784

    
3785

    
3786
            List<string> result = new List<string>();
3787
            string originalName = GetSPPIDFileName(modelId);
3788
            foreach (var connector in connectors)
3789
            {
3790
                string fileName = GetSPPIDFileName(connector.ModelItemID);
3791
                if (originalName == fileName)
3792
                    result.Add(connector.ModelItemID);
3793
                else
3794
                {
3795
                    if (document.LINES.Find(x => x.SPPID.ModelItemId == connector.ModelItemID) == null && Convert.ToBoolean(connector.get_IsZeroLength()))
3796
                        result.Add(connector.ModelItemID);
3797
                    else
3798
                    {
3799
                        Line line1 = document.LINES.Find(x => x.SPPID.ModelItemId == modelId);
3800
                        Line line2 = document.LINES.Find(x => x.SPPID.ModelItemId == connector.ModelItemID.ToString());
3801
                        if (line1 != null && line2 != null && line1.TYPE == line2.TYPE)
3802
                            result.Add(connector.ModelItemID);
3803
                    }
3804
                }
3805
            }
3806

    
3807
            foreach (var connector in connectors)
3808
                ReleaseCOMObjects(connector);
3809

    
3810
            return result;
3811

    
3812

    
3813
            LMSymbol FindOtherConnectedSymbol(LMConnector connector)
3814
            {
3815
                LMSymbol findResult = null;
3816
                if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.Id != symbol.Id && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
3817
                    findResult = connector.ConnectItem1SymbolObject;
3818
                else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.Id != symbol.Id && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
3819
                    findResult = connector.ConnectItem2SymbolObject;
3820

    
3821
                return findResult;
3822
            }
3823
        }
3824

    
3825
        /// <summary>
3826
        /// PipeRun의 좌표를 가져오는 메서드
3827
        /// </summary>
3828
        /// <param name="modelId"></param>
3829
        /// <returns></returns>
3830
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId, bool ContainZeroLength = true)
3831
        {
3832
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
3833
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
3834

    
3835
            if (modelItem != null)
3836
            {
3837
                foreach (LMRepresentation rep in modelItem.Representations)
3838
                {
3839
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3840
                    {
3841
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3842
                        if (!ContainZeroLength && Convert.ToBoolean(_LMConnector.get_IsZeroLength()))
3843
                        {
3844
                            ReleaseCOMObjects(_LMConnector);
3845
                            _LMConnector = null;
3846
                            continue;
3847
                        }
3848
                        connectorVertices.Add(_LMConnector, new List<double[]>());
3849
                        dynamic OID = rep.get_GraphicOID().ToString();
3850
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
3851
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3852
                        int verticesCount = lineStringGeometry.VertexCount;
3853
                        double[] vertices = null;
3854
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
3855
                        for (int i = 0; i < verticesCount; i++)
3856
                        {
3857
                            double x = 0;
3858
                            double y = 0;
3859
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3860
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
3861
                        }
3862
                    }
3863
                }
3864

    
3865
                ReleaseCOMObjects(modelItem);
3866
            }
3867

    
3868
            return connectorVertices;
3869
        }
3870

    
3871
        private List<double[]> GetConnectorVertices(LMConnector connector)
3872
        {
3873
            List<double[]> vertices = new List<double[]>();
3874
            if (connector != null)
3875
            {
3876
                dynamic OID = connector.get_GraphicOID().ToString();
3877
                DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
3878
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3879
                int verticesCount = lineStringGeometry.VertexCount;
3880
                double[] value = null;
3881
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
3882
                for (int i = 0; i < verticesCount; i++)
3883
                {
3884
                    double x = 0;
3885
                    double y = 0;
3886
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3887
                    vertices.Add(new double[] { x, y });
3888
                }
3889
            }
3890
            return vertices;
3891
        }
3892

    
3893
        private double GetConnectorDistance(LMConnector connector)
3894
        {
3895
            double result = 0;
3896
            List<double[]> vertices = new List<double[]>();
3897
            if (connector != null)
3898
            {
3899
                dynamic OID = connector.get_GraphicOID().ToString();
3900
                DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
3901
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3902
                int verticesCount = lineStringGeometry.VertexCount;
3903
                double[] value = null;
3904
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
3905
                for (int i = 0; i < verticesCount; i++)
3906
                {
3907
                    double x = 0;
3908
                    double y = 0;
3909
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3910
                    vertices.Add(new double[] { x, y });
3911
                    if (vertices.Count > 1)
3912
                    {
3913
                        result += SPPIDUtil.CalcPointToPointdDistance(vertices[vertices.Count - 2][0], vertices[vertices.Count - 2][1], x, y);
3914
                    }
3915
                }
3916
            }
3917
            return result;
3918
        }
3919
        private double[] GetConnectorRange(LMConnector connector)
3920
        {
3921
            double[] result = null;
3922
            List<double[]> vertices = new List<double[]>();
3923
            if (connector != null)
3924
            {
3925
                dynamic OID = connector.get_GraphicOID().ToString();
3926
                DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
3927
                double minX = 0;
3928
                double minY = 0;
3929
                double maxX = 0;
3930
                double maxY = 0;
3931

    
3932
                drawingObject.Range(out minX, out minY, out maxX, out maxY);
3933
                result = new double[] { minX, minY, maxX, maxY };
3934
            }
3935
            return result;
3936
        }
3937
        private List<double[]> GetConnectorVertices(dynamic graphicOID)
3938
        {
3939
            List<double[]> vertices = null;
3940
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID];
3941
            if (drawingObject != null)
3942
            {
3943
                vertices = new List<double[]>();
3944
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3945
                int verticesCount = lineStringGeometry.VertexCount;
3946
                double[] value = null;
3947
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
3948
                for (int i = 0; i < verticesCount; i++)
3949
                {
3950
                    double x = 0;
3951
                    double y = 0;
3952
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3953
                    vertices.Add(new double[] { x, y });
3954
                }
3955
            }
3956
            return vertices;
3957
        }
3958
        /// <summary>
3959
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
3960
        /// </summary>
3961
        /// <param name="connectorVertices"></param>
3962
        /// <param name="connX"></param>
3963
        /// <param name="connY"></param>
3964
        /// <returns></returns>
3965
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
3966
        {
3967
            double length = double.MaxValue;
3968
            LMConnector targetConnector = null;
3969
            foreach (var item in connectorVertices)
3970
            {
3971
                List<double[]> points = item.Value;
3972
                for (int i = 0; i < points.Count - 1; i++)
3973
                {
3974
                    double[] point1 = points[i];
3975
                    double[] point2 = points[i + 1];
3976
                    double x1 = Math.Min(point1[0], point2[0]);
3977
                    double y1 = Math.Min(point1[1], point2[1]);
3978
                    double x2 = Math.Max(point1[0], point2[0]);
3979
                    double y2 = Math.Max(point1[1], point2[1]);
3980

    
3981
                    if ((x1 <= connX && x2 >= connX) ||
3982
                        (y1 <= connY && y2 >= connY))
3983
                    {
3984
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
3985
                        if (length >= distance)
3986
                        {
3987
                            targetConnector = item.Key;
3988
                            length = distance;
3989
                        }
3990

    
3991
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
3992
                        if (length >= distance)
3993
                        {
3994
                            targetConnector = item.Key;
3995
                            length = distance;
3996
                        }
3997
                    }
3998
                }
3999
            }
4000

    
4001
            // 못찾았을때.
4002
            length = double.MaxValue;
4003
            if (targetConnector == null)
4004
            {
4005
                foreach (var item in connectorVertices)
4006
                {
4007
                    List<double[]> points = item.Value;
4008

    
4009
                    foreach (double[] point in points)
4010
                    {
4011
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
4012
                        if (length >= distance)
4013
                        {
4014
                            targetConnector = item.Key;
4015
                            length = distance;
4016
                        }
4017
                    }
4018
                }
4019
            }
4020

    
4021
            return targetConnector;
4022
        }
4023

    
4024
        private LMConnector FindTargetLMConnectorForBranch(Line line, Line targetLine, ref double x, ref double y)
4025
        {
4026
            Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
4027
            if (vertices.Count == 0)
4028
                return null;
4029

    
4030
            double length = double.MaxValue;
4031
            LMConnector targetConnector = null;
4032
            double[] resultPoint = null;
4033
            List<double[]> targetVertices = null;
4034

    
4035
            // Vertices 포인트에 제일 가까운곳
4036
            foreach (var item in vertices)
4037
            {
4038
                List<double[]> points = item.Value;
4039
                for (int i = 0; i < points.Count; i++)
4040
                {
4041
                    double[] point = points[i];
4042
                    double tempX = point[0];
4043
                    double tempY = point[1];
4044

    
4045
                    double distance = SPPIDUtil.CalcPointToPointdDistance(tempX, tempY, x, y);
4046
                    if (length >= distance)
4047
                    {
4048
                        targetConnector = item.Key;
4049
                        length = distance;
4050
                        resultPoint = point;
4051
                        targetVertices = item.Value;
4052
                    }
4053
                }
4054
            }
4055

    
4056
            // Vertices Cross에 제일 가까운곳
4057
            foreach (var item in vertices)
4058
            {
4059
                List<double[]> points = item.Value;
4060
                for (int i = 0; i < points.Count - 1; i++)
4061
                {
4062
                    double[] point1 = points[i];
4063
                    double[] point2 = points[i + 1];
4064

    
4065
                    double maxLineX = Math.Max(point1[0], point2[0]);
4066
                    double minLineX = Math.Min(point1[0], point2[0]);
4067
                    double maxLineY = Math.Max(point1[1], point2[1]);
4068
                    double minLineY = Math.Min(point1[1], point2[1]);
4069

    
4070
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
4071

    
4072
                    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]);
4073
                    if (crossingPoint != null)
4074
                    {
4075
                        double distance = SPPIDUtil.CalcPointToPointdDistance(crossingPoint[0], crossingPoint[1], x, y);
4076
                        if (length >= distance)
4077
                        {
4078
                            if (slope == SlopeType.Slope &&
4079
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
4080
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
4081
                            {
4082
                                targetConnector = item.Key;
4083
                                length = distance;
4084
                                resultPoint = crossingPoint;
4085
                                targetVertices = item.Value;
4086
                            }
4087
                            else if (slope == SlopeType.HORIZONTAL &&
4088
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
4089
                            {
4090
                                targetConnector = item.Key;
4091
                                length = distance;
4092
                                resultPoint = crossingPoint;
4093
                                targetVertices = item.Value;
4094
                            }
4095
                            else if (slope == SlopeType.VERTICAL &&
4096
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
4097
                            {
4098
                                targetConnector = item.Key;
4099
                                length = distance;
4100
                                resultPoint = crossingPoint;
4101
                                targetVertices = item.Value;
4102
                            }
4103
                        }
4104
                    }
4105
                }
4106
            }
4107

    
4108
            foreach (var item in vertices)
4109
                if (item.Key != null && item.Key != targetConnector)
4110
                    ReleaseCOMObjects(item.Key);
4111

    
4112
            if (SPPIDUtil.IsBranchLine(line, targetLine))
4113
            {
4114
                double tempResultX = resultPoint[0];
4115
                double tempResultY = resultPoint[1];
4116
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
4117

    
4118
                GridSetting gridSetting = GridSetting.GetInstance();
4119

    
4120
                for (int i = 0; i < targetVertices.Count; i++)
4121
                {
4122
                    double[] point = targetVertices[i];
4123
                    double tempX = targetVertices[i][0];
4124
                    double tempY = targetVertices[i][1];
4125
                    SPPIDUtil.ConvertGridPoint(ref tempX, ref tempY);
4126
                    if (tempX == tempResultX && tempY == tempResultY)
4127
                    {
4128
                        if (i == 0)
4129
                        {
4130
                            LMSymbol connSymbol = targetConnector.ConnectItem1SymbolObject;
4131
                            bool containZeroLength = false;
4132
                            if (connSymbol != null)
4133
                            {
4134
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
4135
                                {
4136
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
4137
                                        containZeroLength = true;
4138
                                }
4139
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
4140
                                {
4141
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
4142
                                        containZeroLength = true;
4143
                                }
4144
                            }
4145

    
4146
                            if (connSymbol == null ||
4147
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
4148
                                containZeroLength)
4149
                            {
4150
                                bool bCalcX = false;
4151
                                bool bCalcY = false;
4152
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
4153
                                    bCalcX = true;
4154
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
4155
                                    bCalcY = true;
4156
                                else
4157
                                {
4158
                                    bCalcX = true;
4159
                                    bCalcY = true;
4160
                                }
4161

    
4162
                                if (bCalcX)
4163
                                {
4164
                                    double nextX = targetVertices[i + 1][0];
4165
                                    double newX = 0;
4166
                                    if (nextX > tempX)
4167
                                    {
4168
                                        newX = tempX + gridSetting.Length;
4169
                                        if (newX > nextX)
4170
                                            newX = (point[0] + nextX) / 2;
4171
                                    }
4172
                                    else
4173
                                    {
4174
                                        newX = tempX - gridSetting.Length;
4175
                                        if (newX < nextX)
4176
                                            newX = (point[0] + nextX) / 2;
4177
                                    }
4178
                                    resultPoint = new double[] { newX, resultPoint[1] };
4179
                                }
4180

    
4181
                                if (bCalcY)
4182
                                {
4183
                                    double nextY = targetVertices[i + 1][1];
4184
                                    double newY = 0;
4185
                                    if (nextY > tempY)
4186
                                    {
4187
                                        newY = tempY + gridSetting.Length;
4188
                                        if (newY > nextY)
4189
                                            newY = (point[1] + nextY) / 2;
4190
                                    }
4191
                                    else
4192
                                    {
4193
                                        newY = tempY - gridSetting.Length;
4194
                                        if (newY < nextY)
4195
                                            newY = (point[1] + nextY) / 2;
4196
                                    }
4197
                                    resultPoint = new double[] { resultPoint[0], newY };
4198
                                }
4199
                            }
4200
                        }
4201
                        else if (i == targetVertices.Count - 1)
4202
                        {
4203
                            LMSymbol connSymbol = targetConnector.ConnectItem2SymbolObject;
4204
                            bool containZeroLength = false;
4205
                            if (connSymbol != null)
4206
                            {
4207
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
4208
                                {
4209
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
4210
                                        containZeroLength = true;
4211
                                }
4212
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
4213
                                {
4214
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
4215
                                        containZeroLength = true;
4216
                                }
4217
                            }
4218

    
4219
                            if (connSymbol == null ||
4220
                                 (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
4221
                                containZeroLength)
4222
                            {
4223
                                bool bCalcX = false;
4224
                                bool bCalcY = false;
4225
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
4226
                                    bCalcX = true;
4227
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
4228
                                    bCalcY = true;
4229
                                else
4230
                                {
4231
                                    bCalcX = true;
4232
                                    bCalcY = true;
4233
                                }
4234

    
4235
                                if (bCalcX)
4236
                                {
4237
                                    double nextX = targetVertices[i - 1][0];
4238
                                    double newX = 0;
4239
                                    if (nextX > tempX)
4240
                                    {
4241
                                        newX = tempX + gridSetting.Length;
4242
                                        if (newX > nextX)
4243
                                            newX = (point[0] + nextX) / 2;
4244
                                    }
4245
                                    else
4246
                                    {
4247
                                        newX = tempX - gridSetting.Length;
4248
                                        if (newX < nextX)
4249
                                            newX = (point[0] + nextX) / 2;
4250
                                    }
4251
                                    resultPoint = new double[] { newX, resultPoint[1] };
4252
                                }
4253

    
4254
                                if (bCalcY)
4255
                                {
4256
                                    double nextY = targetVertices[i - 1][1];
4257
                                    double newY = 0;
4258
                                    if (nextY > tempY)
4259
                                    {
4260
                                        newY = tempY + gridSetting.Length;
4261
                                        if (newY > nextY)
4262
                                            newY = (point[1] + nextY) / 2;
4263
                                    }
4264
                                    else
4265
                                    {
4266
                                        newY = tempY - gridSetting.Length;
4267
                                        if (newY < nextY)
4268
                                            newY = (point[1] + nextY) / 2;
4269
                                    }
4270
                                    resultPoint = new double[] { resultPoint[0], newY };
4271
                                }
4272
                            }
4273
                        }
4274
                        break;
4275
                    }
4276
                }
4277
            }
4278

    
4279
            x = resultPoint[0];
4280
            y = resultPoint[1];
4281

    
4282
            return targetConnector;
4283
        }
4284

    
4285
        private LMConnector GetLMConnectorOnlyOne(string modelItemID)
4286
        {
4287
            LMConnector result = null;
4288
            List<LMConnector> connectors = new List<LMConnector>();
4289
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4290

    
4291
            if (modelItem != null)
4292
            {
4293
                foreach (LMRepresentation rep in modelItem.Representations)
4294
                {
4295
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
4296
                        connectors.Add(dataSource.GetConnector(rep.Id));
4297
                }
4298

    
4299
                ReleaseCOMObjects(modelItem);
4300
            }
4301

    
4302
            if (connectors.Count == 1)
4303
                result = connectors[0];
4304
            else
4305
                foreach (var item in connectors)
4306
                    ReleaseCOMObjects(item);
4307

    
4308
            return result;
4309
        }
4310

    
4311
        private LMConnector GetLMConnectorFirst(string modelItemID)
4312
        {
4313
            LMConnector result = null;
4314
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4315

    
4316
            if (modelItem != null)
4317
            {
4318
                foreach (LMRepresentation rep in modelItem.Representations)
4319
                {
4320
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" &&
4321
                        rep.Attributes["ItemStatus"].get_Value() == "Active")
4322
                    {
4323
                        LMConnector connector = dataSource.GetConnector(rep.Id);
4324
                        if (!Convert.ToBoolean(connector.get_IsZeroLength()))
4325
                        {
4326
                            result = connector;
4327
                            break;
4328
                        }
4329
                        else
4330
                        {
4331
                            ReleaseCOMObjects(connector);
4332
                            connector = null;
4333
                        }
4334
                    }
4335
                }
4336
                ReleaseCOMObjects(modelItem);
4337
                modelItem = null;
4338
            }
4339

    
4340
            return result;
4341
        }
4342

    
4343
        private int GetConnectorCount(string modelItemID)
4344
        {
4345
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4346
            int result = 0;
4347
            if (modelItem != null)
4348
            {
4349
                foreach (LMRepresentation rep in modelItem.Representations)
4350
                {
4351
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
4352
                        result++;
4353
                    ReleaseCOMObjects(rep);
4354
                }
4355
                ReleaseCOMObjects(modelItem);
4356
            }
4357

    
4358
            return result;
4359
        }
4360

    
4361
        public List<string> GetRepresentations(string modelItemID)
4362
        {
4363
            List<string> result = new List<string>(); ;
4364
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4365
            if (modelItem != null)
4366
            {
4367
                foreach (LMRepresentation rep in modelItem.Representations)
4368
                {
4369
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
4370
                        result.Add(rep.Id);
4371
                }
4372
                ReleaseCOMObjects(modelItem);
4373
            }
4374

    
4375
            return result;
4376
        }
4377

    
4378
        private void LineNumberModeling(LineNumber lineNumber)
4379
        {
4380
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
4381
            if (line != null)
4382
            {
4383
                double x = 0;
4384
                double y = 0;
4385
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
4386

    
4387
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
4388
                LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, x, y);
4389
                if (connectedLMConnector != null)
4390
                {
4391
                    Array points = new double[] { 0, x, y };
4392
                    lineNumber.SPPID.SPPID_X = x;
4393
                    lineNumber.SPPID.SPPID_Y = y;
4394
                    LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: false);
4395

    
4396
                    if (_LmLabelPresist != null)
4397
                    {
4398
                        _LmLabelPresist.Commit();
4399
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
4400
                        ReleaseCOMObjects(_LmLabelPresist);
4401
                    }
4402
                }
4403

    
4404
                foreach (var item in connectorVertices)
4405
                    ReleaseCOMObjects(item.Key);
4406
            }
4407
        }
4408
        private void LineNumberCorrectModeling(LineNumber lineNumber)
4409
        {
4410
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
4411
            if (line == null || line.SPPID.Vertices == null)
4412
                return;
4413

    
4414
            if (!string.IsNullOrEmpty(lineNumber.SPPID.RepresentationId))
4415
            {
4416
                LMLabelPersist removeLabel = dataSource.GetLabelPersist(lineNumber.SPPID.RepresentationId);
4417
                if (removeLabel != null)
4418
                {
4419
                    lineNumber.SPPID.SPPID_X = removeLabel.get_XCoordinate();
4420
                    lineNumber.SPPID.SPPID_Y = removeLabel.get_YCoordinate();
4421

    
4422
                    GridSetting gridSetting = GridSetting.GetInstance();
4423
                    LMConnector connector = dataSource.GetConnector(removeLabel.RepresentationID);
4424

    
4425
                    double[] labelRange = null;
4426
                    GetSPPIDSymbolRange(removeLabel, ref labelRange);
4427
                    List<double[]> vertices = GetConnectorVertices(connector);
4428

    
4429
                    double[] resultStart = null;
4430
                    double[] resultEnd = null;
4431
                    double distance = double.MaxValue;
4432
                    for (int i = 0; i < vertices.Count - 1; i++)
4433
                    {
4434
                        double[] startPoint = vertices[i];
4435
                        double[] endPoint = vertices[i + 1];
4436
                        foreach (var item in line.SPPID.Vertices)
4437
                        {
4438
                            double[] lineStartPoint = item[0];
4439
                            double[] lineEndPoint = item[item.Count - 1];
4440

    
4441
                            double tempDistance = SPPIDUtil.CalcPointToPointdDistance(startPoint[0], startPoint[1], lineStartPoint[0], lineStartPoint[1]) +
4442
                                SPPIDUtil.CalcPointToPointdDistance(endPoint[0], endPoint[1], lineEndPoint[0], lineEndPoint[1]);
4443
                            if (tempDistance < distance)
4444
                            {
4445
                                distance = tempDistance;
4446
                                resultStart = startPoint;
4447
                                resultEnd = endPoint;
4448
                            }
4449
                            tempDistance = SPPIDUtil.CalcPointToPointdDistance(startPoint[0], startPoint[1], lineEndPoint[0], lineEndPoint[1]) +
4450
                                SPPIDUtil.CalcPointToPointdDistance(endPoint[0], endPoint[1], lineStartPoint[0], lineStartPoint[1]);
4451
                            if (tempDistance < distance)
4452
                            {
4453
                                distance = tempDistance;
4454
                                resultStart = startPoint;
4455
                                resultEnd = endPoint;
4456
                            }
4457
                        }
4458
                    }
4459

    
4460
                    if (resultStart != null && resultEnd != null)
4461
                    {
4462
                        SlopeType slope = SPPIDUtil.CalcSlope(resultStart[0], resultStart[1], resultEnd[0], resultEnd[1]);
4463
                        double lineStartX = 0;
4464
                        double lineStartY = 0;
4465
                        double lineEndX = 0;
4466
                        double lineEndY = 0;
4467
                        double lineNumberX = 0;
4468
                        double lineNumberY = 0;
4469
                        SPPIDUtil.ConvertPointBystring(line.STARTPOINT, ref lineStartX, ref lineStartY);
4470
                        SPPIDUtil.ConvertPointBystring(line.ENDPOINT, ref lineEndX, ref lineEndY);
4471

    
4472
                        double lineX = (lineStartX + lineEndX) / 2;
4473
                        double lineY = (lineStartY + lineEndY) / 2;
4474
                        lineNumberX = (lineNumber.X1 + lineNumber.X2) / 2;
4475
                        lineNumberY = (lineNumber.Y1 + lineNumber.Y2) / 2;
4476

    
4477
                        double SPPIDCenterX = (resultStart[0] + resultEnd[0]) / 2;
4478
                        double SPPIDCenterY = (resultStart[1] + resultEnd[1]) / 2;
4479
                        double labelCenterX = (labelRange[0] + labelRange[2]) / 2;
4480
                        double labelCenterY = (labelRange[1] + labelRange[3]) / 2;
4481

    
4482
                        double offsetX = 0;
4483
                        double offsetY = 0;
4484
                        if (slope == SlopeType.HORIZONTAL)
4485
                        {
4486
                            // Line Number 아래
4487
                            if (lineY < lineNumberY)
4488
                            {
4489
                                offsetX = labelCenterX - SPPIDCenterX;
4490
                                offsetY = labelRange[3] - SPPIDCenterY + gridSetting.Length;
4491
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4492
                            }
4493
                            // Line Number 위
4494
                            else
4495
                            {
4496
                                offsetX = labelCenterX - SPPIDCenterX;
4497
                                offsetY = labelRange[1] - SPPIDCenterY - gridSetting.Length;
4498
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4499
                            }
4500
                        }
4501
                        else if (slope == SlopeType.VERTICAL)
4502
                        {
4503
                            // Line Number 오르쪽
4504
                            if (lineX < lineNumberX)
4505
                            {
4506
                                offsetX = labelRange[0] - SPPIDCenterX - gridSetting.Length;
4507
                                offsetY = labelCenterY - SPPIDCenterY;
4508
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4509
                            }
4510
                            // Line Number 왼쪽
4511
                            else
4512
                            {
4513
                                offsetX = labelRange[2] - SPPIDCenterX + gridSetting.Length;
4514
                                offsetY = labelCenterY - SPPIDCenterY;
4515
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4516
                            }
4517
                        }
4518

    
4519
                        if (offsetY != 0 || offsetY != 0)
4520
                        {
4521
                            if (connector.ConnectItem1SymbolObject != null &&
4522
                                connector.ConnectItem1SymbolObject.get_RepresentationType() == "OPC")
4523
                            {
4524
                                Ingr.RAD2D.Symbol2d symbol = radApp.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()];
4525

    
4526
                                double x1, y1, x2, y2, originX, originY;
4527
                                symbol.Range(out x1, out y1, out x2, out y2);
4528
                                symbol.GetOrigin(out originX, out originY);
4529
                                if (originX < lineNumber.SPPID.SPPID_X)
4530
                                    offsetX = -1 * (originX + gridSetting.Length * 30 - labelCenterX);
4531
                                else
4532
                                    offsetX = -1 * (originX - gridSetting.Length * 30 - labelCenterX);
4533
                            }
4534
                            else if (connector.ConnectItem2SymbolObject != null &&
4535
                                    connector.ConnectItem2SymbolObject.get_RepresentationType() == "OPC")
4536
                            {
4537
                                Ingr.RAD2D.Symbol2d symbol = radApp.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()];
4538

    
4539
                                double x1, y1, x2, y2, originX, originY;
4540
                                symbol.Range(out x1, out y1, out x2, out y2);
4541
                                symbol.GetOrigin(out originX, out originY);
4542
                                if (originX < lineNumber.SPPID.SPPID_X)
4543
                                    offsetX = -1 * (originX + gridSetting.Length * 30 - labelCenterX);
4544
                                else
4545
                                    offsetX = -1 * (originX - gridSetting.Length * 30 - labelCenterX);
4546
                            }
4547

    
4548
                            radApp.ActiveSelectSet.RemoveAll();
4549
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[removeLabel.get_GraphicOID().ToString()] as DependencyObject;
4550
                            if (dependency != null)
4551
                            {
4552
                                radApp.ActiveSelectSet.Add(dependency);
4553
                                Ingr.RAD2D.Transform transform = dependency.GetTransform();
4554
                                transform.DefineByMove2d(-offsetX, -offsetY);
4555
                                radApp.ActiveSelectSet.Transform(transform, true);
4556
                                radApp.ActiveSelectSet.RemoveAll();
4557
                            }
4558
                        }
4559

    
4560
                        void MoveLineNumber(LineNumber moveLineNumber, double x, double y)
4561
                        {
4562
                            moveLineNumber.SPPID.SPPID_X = moveLineNumber.SPPID.SPPID_X - x;
4563
                            moveLineNumber.SPPID.SPPID_Y = moveLineNumber.SPPID.SPPID_Y - y;
4564
                        }
4565
                    }
4566

    
4567

    
4568
                    ReleaseCOMObjects(connector);
4569
                    connector = null;
4570
                }
4571

    
4572
                ReleaseCOMObjects(removeLabel);
4573
                removeLabel = null;
4574
            }
4575
        }
4576
        /// <summary>
4577
        /// Flow Mark Modeling
4578
        /// </summary>
4579
        /// <param name="line"></param>
4580
        private void FlowMarkModeling(Line line)
4581
        {
4582
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
4583
            {
4584
                LMConnector connector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
4585
                if (connector != null)
4586
                {
4587
                    string mappingPath = _ETCSetting.FlowMarkSymbolPath;
4588
                    List<double[]> vertices = GetConnectorVertices(connector);
4589
                    vertices = vertices.FindAll(x => x[0] > 0 && x[1] > 0);
4590
                    double[] point = vertices[vertices.Count - 1];
4591
                    Array array = new double[] { 0, point[0], point[1] };
4592
                    LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mappingPath, ref array, LabeledItem: connector.AsLMRepresentation());
4593
                    if (_LMLabelPersist != null)
4594
                    {
4595
                        _LMLabelPersist.Commit();
4596
                        FlowMarkRepIds.Add(_LMLabelPersist.Id);
4597
                        ReleaseCOMObjects(_LMLabelPersist);
4598
                    }
4599
                }
4600
            }
4601
        }
4602

    
4603
        /// <summary>
4604
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
4605
        /// </summary>
4606
        /// <param name="lineNumber"></param>
4607
        private void InputLineNumberAttribute(LineNumber lineNumber, List<string> endLine)
4608
        {
4609
            lineNumber.ATTRIBUTES.Sort(SortAttribute);
4610
            int SortAttribute(BaseModel.Attribute a, BaseModel.Attribute b)
4611
            {
4612
                if (a.ATTRIBUTE == "Tag Seq No")
4613
                    return 1;
4614
                else if (b.ATTRIBUTE == "Tag Seq No")
4615
                    return -1;
4616

    
4617
                return 0;
4618
            }
4619

    
4620
            foreach (LineRun run in lineNumber.RUNS)
4621
            {
4622
                foreach (var item in run.RUNITEMS)
4623
                {
4624
                    if (item.GetType() == typeof(Line))
4625
                    {
4626
                        Line line = item as Line;
4627
                        if (line != null && !endLine.Contains(line.SPPID.ModelItemId))
4628
                        {
4629
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
4630
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
4631
                            {
4632
                                foreach (var attribute in lineNumber.ATTRIBUTES)
4633
                                {
4634
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
4635
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
4636
                                    {
4637
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
4638
                                        if (mapping.SPPIDATTRIBUTENAME == "OperFluidCode" && !string.IsNullOrEmpty(attribute.VALUE))
4639
                                        {
4640
                                            LMAAttribute _FluidSystemAttribute = _LMModelItem.Attributes["FluidSystem"];
4641
                                            if (_FluidSystemAttribute != null)
4642
                                            {
4643
                                                DataTable dt = SPPID_DB.GetFluidSystemInfo(attribute.VALUE);
4644
                                                if (dt.Rows.Count == 1)
4645
                                                {
4646
                                                    string fluidSystem = dt.Rows[0]["CODELIST_TEXT"].ToString();
4647
                                                    if (DBNull.Value.Equals(_FluidSystemAttribute.get_Value()))
4648
                                                        _FluidSystemAttribute.set_Value(fluidSystem);
4649
                                                    else if (_FluidSystemAttribute.get_Value() != fluidSystem)
4650
                                                        _FluidSystemAttribute.set_Value(fluidSystem);
4651

    
4652
                                                    if (_LMAAttribute != null)
4653
                                                    {
4654
                                                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4655
                                                            _LMAAttribute.set_Value(attribute.VALUE);
4656
                                                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
4657
                                                            _LMAAttribute.set_Value(attribute.VALUE);
4658
                                                    }
4659
                                                }
4660
                                                if (dt != null)
4661
                                                    dt.Dispose();
4662
                                            }
4663
                                        }
4664
                                        else if (mapping.SPPIDATTRIBUTENAME.Equals("NominalDiameter") && !string.IsNullOrEmpty(attribute.VALUE) && _LMAAttribute != null)
4665
                                        {
4666
                                            DataRow[] rows = nominalDiameterTable.Select(string.Format("MetricStr = '{0}' OR InchStr = '{0}'", attribute.VALUE));
4667

    
4668
                                            if (rows.Length.Equals(1))
4669
                                            {
4670
                                                if (_ETCSetting.UnitSetting != null && _ETCSetting.UnitSetting.Equals("Metric"))
4671
                                                    attribute.VALUE = rows[0]["MetricStr"].ToString();
4672
                                                else
4673
                                                    attribute.VALUE = rows[0]["InchStr"].ToString();
4674

    
4675
                                                if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4676
                                                    _LMAAttribute.set_Value(attribute.VALUE);
4677
                                                else if (_LMAAttribute.get_Value() != attribute.VALUE)
4678
                                                    _LMAAttribute.set_Value(attribute.VALUE);
4679
                                            }
4680
                                        }
4681
                                        else if (_LMAAttribute != null)
4682
                                        {
4683
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4684
                                                _LMAAttribute.set_Value(attribute.VALUE);
4685
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
4686
                                                _LMAAttribute.set_Value(attribute.VALUE);
4687
                                        }
4688
                                    }
4689
                                }
4690
                                _LMModelItem.Commit();
4691
                            }
4692
                            if (_LMModelItem != null)
4693
                                ReleaseCOMObjects(_LMModelItem);
4694
                            endLine.Add(line.SPPID.ModelItemId);
4695
                        }
4696
                    }
4697
                }
4698
            }
4699
        }
4700

    
4701
        /// <summary>
4702
        /// Symbol Attribute 입력 메서드
4703
        /// </summary>
4704
        /// <param name="item"></param>
4705
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
4706
        {
4707
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
4708
            string sRep = null;
4709
            string sModelID = null;
4710
            if (targetItem.GetType() == typeof(Symbol))
4711
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
4712
            else if (targetItem.GetType() == typeof(Equipment))
4713
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
4714
            else if (targetItem.GetType() == typeof(Line))
4715
                sModelID = ((Line)targetItem).SPPID.ModelItemId;
4716

    
4717
            if (!string.IsNullOrEmpty(sRep))
4718
            {
4719
                LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
4720
                LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
4721
                LMAAttributes _Attributes = _LMModelItem.Attributes;
4722

    
4723
                foreach (var item in targetAttributes)
4724
                {
4725
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
4726
                    if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
4727
                    {
4728
                        if (!mapping.IsText)
4729
                        {
4730
                            LMAAttribute _LMAAttribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
4731
                            if (mapping.SPPIDATTRIBUTENAME.Equals("NominalDiameter") && !string.IsNullOrEmpty(item.VALUE) && _LMAAttribute != null)
4732
                            {
4733
                                DataRow[] rows = nominalDiameterTable.Select(string.Format("MetricStr = '{0}' OR InchStr = '{0}'", item.VALUE));
4734

    
4735
                                if (rows.Length.Equals(1))
4736
                                {
4737
                                    if (_ETCSetting.UnitSetting != null && _ETCSetting.UnitSetting.Equals("Metric"))
4738
                                        item.VALUE = rows[0]["MetricStr"].ToString();
4739
                                    else
4740
                                        item.VALUE = rows[0]["InchStr"].ToString();
4741

    
4742
                                    if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4743
                                        _LMAAttribute.set_Value(item.VALUE);
4744
                                    else if (_LMAAttribute.get_Value() != item.VALUE)
4745
                                        _LMAAttribute.set_Value(item.VALUE);
4746
                                }
4747
                            }
4748
                            else if (_LMAAttribute != null)
4749
                            {
4750
                                _LMAAttribute.set_Value(item.VALUE);
4751
                                // OPC 일경우 Attribute 저장
4752
                                if (targetItem.GetType() == typeof(Symbol))
4753
                                {
4754
                                    Symbol symbol = targetItem as Symbol;
4755
                                    if (symbol.TYPE == "Piping OPC's" || symbol.TYPE == "Instrument OPC's")
4756
                                        symbol.SPPID.Attributes.Add(new string[] { mapping.SPPIDATTRIBUTENAME, item.VALUE });
4757
                                }
4758
                            }
4759
                        }
4760
                        else
4761
                            DefaultTextModeling(item.VALUE, _LMSymbol.get_XCoordinate(), _LMSymbol.get_YCoordinate());
4762
                    }
4763
                }
4764
                _LMModelItem.Commit();
4765

    
4766
                ReleaseCOMObjects(_Attributes);
4767
                ReleaseCOMObjects(_LMModelItem);
4768
                ReleaseCOMObjects(_LMSymbol);
4769
            }
4770
            else if (!string.IsNullOrEmpty(sModelID))
4771
            {
4772
                LMModelItem _LMModelItem = dataSource.GetModelItem(sModelID);
4773
                LMAAttributes _Attributes = _LMModelItem.Attributes;
4774

    
4775
                foreach (var item in targetAttributes)
4776
                {
4777
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
4778
                    if (mapping == null)
4779
                        continue;
4780
                    
4781
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
4782
                    if (mapping.SPPIDATTRIBUTENAME == "OperFluidCode" && !string.IsNullOrEmpty(item.VALUE))
4783
                    {
4784
                        LMAAttribute _FluidSystemAttribute = _LMModelItem.Attributes["FluidSystem"];
4785
                        if (_FluidSystemAttribute != null)
4786
                        {
4787
                            DataTable dt = SPPID_DB.GetFluidSystemInfo(item.VALUE);
4788
                            if (dt.Rows.Count == 1)
4789
                            {
4790
                                string fluidSystem = dt.Rows[0]["CODELIST_TEXT"].ToString();
4791
                                if (DBNull.Value.Equals(_FluidSystemAttribute.get_Value()))
4792
                                    _FluidSystemAttribute.set_Value(fluidSystem);
4793
                                else if (_FluidSystemAttribute.get_Value() != fluidSystem)
4794
                                    _FluidSystemAttribute.set_Value(fluidSystem);
4795

    
4796
                                if (_LMAAttribute != null)
4797
                                {
4798
                                    if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4799
                                        _LMAAttribute.set_Value(item.VALUE);
4800
                                    else if (_LMAAttribute.get_Value() != item.VALUE)
4801
                                        _LMAAttribute.set_Value(item.VALUE);
4802
                                }
4803
                            }
4804
                            if (dt != null)
4805
                                dt.Dispose();
4806
                        }
4807
                    }
4808
                    else if (mapping.SPPIDATTRIBUTENAME.Equals("NominalDiameter") && !string.IsNullOrEmpty(item.VALUE) && _LMAAttribute != null)
4809
                    {
4810
                        DataRow[] rows = nominalDiameterTable.Select(string.Format("MetricStr = '{0}' OR InchStr = '{0}'", item.VALUE));
4811

    
4812
                        if (rows.Length.Equals(1))
4813
                        {
4814
                            if (_ETCSetting.UnitSetting != null && _ETCSetting.UnitSetting.Equals("Metric"))
4815
                                item.VALUE = rows[0]["MetricStr"].ToString();
4816
                            else
4817
                                item.VALUE = rows[0]["InchStr"].ToString();
4818

    
4819
                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4820
                                _LMAAttribute.set_Value(item.VALUE);
4821
                            else if (_LMAAttribute.get_Value() != item.VALUE)
4822
                                _LMAAttribute.set_Value(item.VALUE);
4823
                        }
4824
                    }
4825
                    else if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
4826
                    {
4827
                        if (!mapping.IsText)
4828
                        {
4829
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
4830
                            if (_Attribute != null)
4831
                                _Attribute.set_Value(item.VALUE);
4832
                        }
4833
                    }
4834
                }
4835
                _LMModelItem.Commit();
4836

    
4837
                ReleaseCOMObjects(_Attributes);
4838
                ReleaseCOMObjects(_LMModelItem);
4839
            }
4840
        }
4841

    
4842
        /// <summary>
4843
        /// Input SpecBreak Attribute
4844
        /// </summary>
4845
        /// <param name="specBreak"></param>
4846
        private void InputSpecBreakAttribute(SpecBreak specBreak)
4847
        {
4848
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
4849
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
4850

    
4851
            if (upStreamObj != null &&
4852
                downStreamObj != null)
4853
            {
4854
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
4855

    
4856
                if (targetLMConnector != null)
4857
                {
4858
                    foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
4859
                    {
4860
                        string symbolPath = _LMLabelPersist.get_FileName();
4861
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
4862
                        if (mapping != null)
4863
                        {
4864
                            BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
4865
                            if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
4866
                            {
4867
                                string[] values = attribute.VALUE.Split(new char[] { ',' });
4868
                                if (values.Length == 2)
4869
                                {
4870
                                    string upStreamValue = values[0];
4871
                                    string downStreamValue = values[1];
4872

    
4873
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
4874
                                }
4875
                            }
4876
                        }
4877
                    }
4878

    
4879
                    ReleaseCOMObjects(targetLMConnector);
4880
                }
4881
            }
4882

    
4883

    
4884
            #region 내부에서만 쓰는 메서드
4885
            void InputAttributeForSpecBreak(object _upStreamObj, object _downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
4886
            {
4887
                Symbol upStreamSymbol = _upStreamObj as Symbol;
4888
                Line upStreamLine = _upStreamObj as Line;
4889
                Symbol downStreamSymbol = _downStreamObj as Symbol;
4890
                Line downStreamLine = _downStreamObj as Line;
4891
                // 둘다 Line일 경우
4892
                if (upStreamLine != null && downStreamLine != null)
4893
                {
4894
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
4895
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
4896
                }
4897
                // 둘다 Symbol일 경우
4898
                else if (upStreamSymbol != null && downStreamSymbol != null)
4899
                {
4900
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
4901
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
4902
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
4903

    
4904
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
4905
                    {
4906
                        if (connector.get_ItemStatus() != "Active")
4907
                            continue;
4908

    
4909
                        if (connector.Id != zeroLenthConnector.Id)
4910
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4911
                    }
4912

    
4913
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
4914
                    {
4915
                        if (connector.get_ItemStatus() != "Active")
4916
                            continue;
4917

    
4918
                        if (connector.Id != zeroLenthConnector.Id)
4919
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4920
                    }
4921

    
4922
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
4923
                    {
4924
                        if (connector.get_ItemStatus() != "Active")
4925
                            continue;
4926

    
4927
                        if (connector.Id != zeroLenthConnector.Id)
4928
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4929
                    }
4930

    
4931
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
4932
                    {
4933
                        if (connector.get_ItemStatus() != "Active")
4934
                            continue;
4935

    
4936
                        if (connector.Id != zeroLenthConnector.Id)
4937
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4938
                    }
4939

    
4940
                    ReleaseCOMObjects(zeroLenthConnector);
4941
                    ReleaseCOMObjects(upStreamLMSymbol);
4942
                    ReleaseCOMObjects(downStreamLMSymbol);
4943
                }
4944
                else if (upStreamSymbol != null && downStreamLine != null)
4945
                {
4946
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
4947
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
4948
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
4949

    
4950
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
4951
                    {
4952
                        if (connector.get_ItemStatus() != "Active")
4953
                            continue;
4954

    
4955
                        if (connector.Id == zeroLenthConnector.Id)
4956
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4957
                    }
4958

    
4959
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
4960
                    {
4961
                        if (connector.get_ItemStatus() != "Active")
4962
                            continue;
4963

    
4964
                        if (connector.Id == zeroLenthConnector.Id)
4965
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4966
                    }
4967

    
4968
                    ReleaseCOMObjects(zeroLenthConnector);
4969
                    ReleaseCOMObjects(upStreamLMSymbol);
4970
                }
4971
                else if (upStreamLine != null && downStreamSymbol != null)
4972
                {
4973
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
4974
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
4975
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
4976

    
4977
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
4978
                    {
4979
                        if (connector.get_ItemStatus() != "Active")
4980
                            continue;
4981

    
4982
                        if (connector.Id == zeroLenthConnector.Id)
4983
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4984
                    }
4985

    
4986
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
4987
                    {
4988
                        if (connector.get_ItemStatus() != "Active")
4989
                            continue;
4990

    
4991
                        if (connector.Id == zeroLenthConnector.Id)
4992
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4993
                    }
4994

    
4995
                    ReleaseCOMObjects(zeroLenthConnector);
4996
                    ReleaseCOMObjects(downStreamLMSymbol);
4997
                }
4998
            }
4999

    
5000
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
5001
            {
5002
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
5003
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
5004
                {
5005
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
5006
                    if (_LMAAttribute != null)
5007
                    {
5008
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
5009
                            _LMAAttribute.set_Value(value);
5010
                        else if (_LMAAttribute.get_Value() != value)
5011
                            _LMAAttribute.set_Value(value);
5012
                    }
5013

    
5014
                    _LMModelItem.Commit();
5015
                }
5016
                if (_LMModelItem != null)
5017
                    ReleaseCOMObjects(_LMModelItem);
5018
            }
5019

    
5020
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
5021
            {
5022
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
5023
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
5024
                {
5025
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
5026
                    if (_LMAAttribute != null)
5027
                    {
5028
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
5029
                            _LMAAttribute.set_Value(value);
5030
                        else if (_LMAAttribute.get_Value() != value)
5031
                            _LMAAttribute.set_Value(value);
5032
                    }
5033

    
5034
                    _LMModelItem.Commit();
5035
                }
5036
                if (_LMModelItem != null)
5037
                    ReleaseCOMObjects(_LMModelItem);
5038
            }
5039
            #endregion
5040
        }
5041

    
5042
        private void InputEndBreakAttribute(EndBreak endBreak)
5043
        {
5044
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
5045
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
5046

    
5047
            if ((ownerObj.GetType() == typeof(Symbol) && connectedItem.GetType() == typeof(Line)) ||
5048
                (ownerObj.GetType() == typeof(Line) && connectedItem.GetType() == typeof(Symbol)))
5049
            {
5050
                LMLabelPersist labelPersist = dataSource.GetLabelPersist(endBreak.SPPID.RepresentationId);
5051
                if (labelPersist != null)
5052
                {
5053
                    LMRepresentation representation = labelPersist.RepresentationObject;
5054
                    if (representation != null)
5055
                    {
5056
                        LMConnector connector = dataSource.GetConnector(representation.Id);
5057
                        LMModelItem ZeroLengthModelItem = connector.ModelItemObject;
5058
                        string modelItemID = connector.ModelItemID;
5059
                        if (Convert.ToBoolean(connector.get_IsZeroLength()))
5060
                        {
5061
                            List<string> modelItemIDs = new List<string>();
5062
                            if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
5063
                            {
5064
                                LMSymbol symbol = connector.ConnectItem1SymbolObject;
5065
                                foreach (LMConnector item in symbol.Connect1Connectors)
5066
                                {
5067
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
5068
                                        modelItemIDs.Add(item.ModelItemID);
5069
                                    ReleaseCOMObjects(item);
5070
                                }
5071
                                foreach (LMConnector item in symbol.Connect2Connectors)
5072
                                {
5073
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
5074
                                        modelItemIDs.Add(item.ModelItemID);
5075
                                    ReleaseCOMObjects(item);
5076
                                }
5077
                                ReleaseCOMObjects(symbol);
5078
                                symbol = null;
5079
                            }
5080
                            else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
5081
                            {
5082
                                LMSymbol symbol = connector.ConnectItem2SymbolObject;
5083
                                foreach (LMConnector item in symbol.Connect1Connectors)
5084
                                {
5085
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
5086
                                        modelItemIDs.Add(item.ModelItemID);
5087
                                    ReleaseCOMObjects(item);
5088
                                }
5089
                                foreach (LMConnector item in symbol.Connect2Connectors)
5090
                                {
5091
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
5092
                                        modelItemIDs.Add(item.ModelItemID);
5093
                                    ReleaseCOMObjects(item);
5094
                                }
5095
                                ReleaseCOMObjects(symbol);
5096
                                symbol = null;
5097
                            }
5098

    
5099
                            modelItemIDs = modelItemIDs.Distinct().ToList();
5100
                            if (modelItemIDs.Count == 1)
5101
                            {
5102
                                LMModelItem modelItem = dataSource.GetModelItem(modelItemIDs[0]);
5103
                                LMConnector onlyOne = GetLMConnectorOnlyOne(modelItem.Id);
5104
                                if (onlyOne != null && Convert.ToBoolean(onlyOne.get_IsZeroLength()))
5105
                                {
5106
                                    bool result = false;
5107
                                    foreach (LMLabelPersist loop in onlyOne.LabelPersists)
5108
                                    {
5109
                                        if (document.EndBreaks.Find(x => x.SPPID.RepresentationId == loop.RepresentationID) != null)
5110
                                            result = true;
5111
                                        ReleaseCOMObjects(loop);
5112
                                    }
5113

    
5114
                                    if (result)
5115
                                    {
5116
                                        object value = modelItem.Attributes["TagSequenceNo"].get_Value();
5117
                                        ZeroLengthModelItem.Attributes["TagSequenceNo"].set_Value(value);
5118
                                        ZeroLengthModelItem.Commit();
5119
                                    }
5120
                                    else
5121
                                    {
5122
                                        List<string> loopModelItems = new List<string>();
5123
                                        if (onlyOne.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
5124
                                        {
5125
                                            LMSymbol _symbol = onlyOne.ConnectItem1SymbolObject;
5126
                                            foreach (LMConnector loop in _symbol.Connect1Connectors)
5127
                                            {
5128
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
5129
                                                    loopModelItems.Add(loop.ModelItemID);
5130
                                                ReleaseCOMObjects(loop);
5131
                                            }
5132
                                            foreach (LMConnector loop in _symbol.Connect2Connectors)
5133
                                            {
5134
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
5135
                                                    loopModelItems.Add(loop.ModelItemID);
5136
                                                ReleaseCOMObjects(loop);
5137
                                            }
5138
                                            ReleaseCOMObjects(_symbol);
5139
                                            _symbol = null;
5140
                                        }
5141
                                        else if (onlyOne.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
5142
                                        {
5143
                                            LMSymbol _symbol = onlyOne.ConnectItem2SymbolObject;
5144
                                            foreach (LMConnector loop in _symbol.Connect1Connectors)
5145
                                            {
5146
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
5147
                                                    loopModelItems.Add(loop.ModelItemID);
5148
                                                ReleaseCOMObjects(loop);
5149
                                            }
5150
                                            foreach (LMConnector loop in _symbol.Connect2Connectors)
5151
                                            {
5152
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
5153
                                                    loopModelItems.Add(loop.ModelItemID);
5154
                                                ReleaseCOMObjects(loop);
5155
                                            }
5156
                                            ReleaseCOMObjects(_symbol);
5157
                                            _symbol = null;
5158
                                        }
5159

    
5160
                                        loopModelItems = loopModelItems.Distinct().ToList();
5161
                                        if (loopModelItems.Count == 1)
5162
                                        {
5163
                                            LMModelItem loopModelItem = dataSource.GetModelItem(loopModelItems[0]);
5164
                                            object value = loopModelItem.Attributes["TagSequenceNo"].get_Value();
5165
                                            modelItem.Attributes["TagSequenceNo"].set_Value(value);
5166
                                            modelItem.Commit();
5167
                                            ZeroLengthModelItem.Attributes["TagSequenceNo"].set_Value(value);
5168
                                            ZeroLengthModelItem.Commit();
5169

    
5170
                                            ReleaseCOMObjects(loopModelItem);
5171
                                            loopModelItem = null;
5172
                                        }
5173
                                    }
5174
                                }
5175
                                else
5176
                                {
5177
                                    object value = modelItem.Attributes["TagSequenceNo"].get_Value();
5178
                                    ZeroLengthModelItem.Attributes["TagSequenceNo"].set_Value(value);
5179
                                    ZeroLengthModelItem.Commit();
5180
                                }
5181
                                ReleaseCOMObjects(modelItem);
5182
                                modelItem = null;
5183
                                ReleaseCOMObjects(onlyOne);
5184
                                onlyOne = null;
5185
                            }
5186
                        }
5187
                        ReleaseCOMObjects(connector);
5188
                        connector = null;
5189
                        ReleaseCOMObjects(ZeroLengthModelItem);
5190
                        ZeroLengthModelItem = null;
5191
                    }
5192
                    ReleaseCOMObjects(representation);
5193
                    representation = null;
5194
                }
5195
                ReleaseCOMObjects(labelPersist);
5196
                labelPersist = null;
5197
            }
5198
        }
5199

    
5200
        /// <summary>
5201
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
5202
        /// </summary>
5203
        /// <param name="text"></param>
5204
        private void NormalTextModeling(Text text)
5205
        {
5206
            LMSymbol _LMSymbol = null;
5207

    
5208
            LMItemNote _LMItemNote = null;
5209
            LMAAttribute _LMAAttribute = null;
5210

    
5211
            double x = 0;
5212
            double y = 0;
5213
            double angle = text.ANGLE;
5214
            CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
5215

    
5216
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
5217
            text.SPPID.SPPID_X = x;
5218
            text.SPPID.SPPID_Y = y;
5219

    
5220
            _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, Rotation: angle);
5221
            if (_LMSymbol != null)
5222
            {
5223
                _LMSymbol.Commit();
5224
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5225
                if (_LMItemNote != null)
5226
                {
5227
                    _LMItemNote.Commit();
5228
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5229
                    if (_LMAAttribute != null)
5230
                    {
5231
                        _LMAAttribute.set_Value(text.VALUE);
5232
                        text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5233
                        _LMItemNote.Commit();
5234

    
5235

    
5236
                        double[] range = null;
5237
                        foreach (LMLabelPersist labelPersist in _LMSymbol.LabelPersists)
5238
                        {
5239
                            double[] temp = null;
5240
                            GetSPPIDSymbolRange(labelPersist, ref temp);
5241
                            if (temp != null)
5242
                            {
5243
                                if (range == null)
5244
                                    range = temp;
5245
                                else
5246
                                {
5247
                                    range = new double[] {
5248
                                            Math.Min(range[0], temp[0]),
5249
                                            Math.Min(range[1], temp[1]),
5250
                                            Math.Max(range[2], temp[2]),
5251
                                            Math.Max(range[3], temp[3])
5252
                                        };
5253
                                }
5254
                            }
5255
                        }
5256
                        text.SPPID.Range = range;
5257

    
5258
                        if (_LMAAttribute != null)
5259
                            ReleaseCOMObjects(_LMAAttribute);
5260
                        if (_LMItemNote != null)
5261
                            ReleaseCOMObjects(_LMItemNote);
5262
                    }
5263

    
5264
                    TextCorrectModeling(text);
5265
                }
5266
            }
5267
            if (_LMSymbol != null)
5268
                ReleaseCOMObjects(_LMSymbol);
5269
        }
5270

    
5271
        private void DefaultTextModeling(string value, double x, double y)
5272
        {
5273
            LMSymbol _LMSymbol = null;
5274

    
5275
            LMItemNote _LMItemNote = null;
5276
            LMAAttribute _LMAAttribute = null;
5277

    
5278
            _LMSymbol = _placement.PIDPlaceSymbol(_ETCSetting.TextSymbolPath, x, y, Rotation: 0);
5279
            if (_LMSymbol != null)
5280
            {
5281
                _LMSymbol.Commit();
5282
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5283
                if (_LMItemNote != null)
5284
                {
5285
                    _LMItemNote.Commit();
5286
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5287
                    if (_LMAAttribute != null)
5288
                    {
5289
                        _LMAAttribute.set_Value(value);
5290
                        _LMItemNote.Commit();
5291

    
5292
                        if (_LMAAttribute != null)
5293
                            ReleaseCOMObjects(_LMAAttribute);
5294
                        if (_LMItemNote != null)
5295
                            ReleaseCOMObjects(_LMItemNote);
5296
                    }
5297
                }
5298
            }
5299
            if (_LMSymbol != null)
5300
                ReleaseCOMObjects(_LMSymbol);
5301
        }
5302

    
5303
        private void AssociationTextModeling(Text text)
5304
        {
5305
            LMSymbol _LMSymbol = null;
5306
            LMConnector connectedLMConnector = null;
5307
            object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
5308
            if (owner != null && (owner.GetType() == typeof(Symbol) || owner.GetType() == typeof(Equipment)))
5309
            {
5310
                Symbol symbol = owner as Symbol;
5311
                _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
5312
                if (_LMSymbol != null)
5313
                {
5314
                    foreach (BaseModel.Attribute attribute in symbol.ATTRIBUTES.FindAll(x => x.ASSOCITEM == text.UID))
5315
                    {
5316
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
5317
                        {
5318
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
5319

    
5320
                            if (mapping != null)
5321
                            {
5322
                                double x = 0;
5323
                                double y = 0;
5324

    
5325
                                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, mapping.Location);
5326
                                SPPIDUtil.ConvertGridPoint(ref x, ref y);
5327
                                Array array = new double[] { 0, x, y };
5328
                                text.SPPID.SPPID_X = x;
5329
                                text.SPPID.SPPID_Y = y;
5330
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
5331
                                if (_LMLabelPersist != null)
5332
                                {
5333
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
5334
                                    _LMLabelPersist.Commit();
5335
                                    ReleaseCOMObjects(_LMLabelPersist);
5336
                                }
5337
                            }
5338
                        }
5339
                    }
5340
                }
5341
            }
5342
            else if (owner != null && owner.GetType() == typeof(Line))
5343
            {
5344
                Line line = owner as Line;
5345
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
5346
                connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
5347

    
5348
                if (connectedLMConnector != null)
5349
                {
5350
                    foreach (BaseModel.Attribute attribute in line.ATTRIBUTES.FindAll(x => x.ASSOCITEM == text.UID))
5351
                    {
5352
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
5353
                        {
5354
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
5355

    
5356
                            if (mapping != null)
5357
                            {
5358
                                double x = 0;
5359
                                double y = 0;
5360
                                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, mapping.Location);
5361
                                SPPIDUtil.ConvertGridPoint(ref x, ref y);
5362
                                Array array = new double[] { 0, x, y };
5363

    
5364
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
5365
                                if (_LMLabelPersist != null)
5366
                                {
5367
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
5368
                                    _LMLabelPersist.Commit();
5369

    
5370
                                    DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_LMLabelPersist.get_GraphicOID().ToString()] as DependencyObject;
5371
                                    if (dependency != null)
5372
                                    {
5373
                                        radApp.ActiveSelectSet.RemoveAll();
5374
                                        radApp.ActiveSelectSet.Add(dependency);
5375
                                        Ingr.RAD2D.Transform transform = dependency.GetTransform();
5376
                                        transform.DefineByMove2d(x - _LMLabelPersist.get_XCoordinate(), y - _LMLabelPersist.get_YCoordinate());
5377
                                        radApp.ActiveSelectSet.Transform(transform, true);
5378
                                        radApp.ActiveSelectSet.RemoveAll();
5379
                                    }
5380

    
5381
                                    ReleaseCOMObjects(_LMLabelPersist);
5382
                                }
5383
                            }
5384
                        }
5385
                    }
5386
                }
5387
            }
5388
            if (_LMSymbol != null)
5389
                ReleaseCOMObjects(_LMSymbol);
5390
        }
5391

    
5392
        private void TextCorrectModeling(Text text)
5393
        {
5394
            if (text.SPPID.Range == null)
5395
                return;
5396

    
5397
            bool needRemodeling = false;
5398
            bool loop = true;
5399
            GridSetting gridSetting = GridSetting.GetInstance();
5400
            while (loop)
5401
            {
5402
                loop = false;
5403
                foreach (var overlapText in document.TEXTINFOS)
5404
                {
5405
                    if (overlapText.ASSOCIATION || overlapText == text || overlapText.SPPID.Range == null)
5406
                        continue;
5407

    
5408
                    if (SPPIDUtil.IsOverlap(overlapText.SPPID.Range, text.SPPID.Range))
5409
                    {
5410
                        double percentX = 0;
5411
                        double percentY = 0;
5412
                        if (overlapText.X1 <= text.X2 && overlapText.X2 >= text.X1)
5413
                        {
5414
                            double gapX = Math.Min(overlapText.X2, text.X2) - Math.Max(overlapText.X1, text.X1);
5415
                            percentX = Math.Max(gapX / (overlapText.X2 - overlapText.X1), gapX / (text.X2 - text.X1));
5416
                        }
5417
                        if (overlapText.Y1 <= text.Y2 && overlapText.Y2 >= text.Y1)
5418
                        {
5419
                            double gapY = Math.Min(overlapText.Y2, text.Y2) - Math.Max(overlapText.Y1, text.Y1);
5420
                            percentY = Math.Max(gapY / (overlapText.Y2 - overlapText.Y1), gapY / (text.Y2 - text.Y1));
5421
                        }
5422

    
5423
                        double tempX = 0;
5424
                        double tempY = 0;
5425
                        bool overlapX = false;
5426
                        bool overlapY = false;
5427
                        SPPIDUtil.CalcOverlap(text.SPPID.Range, overlapText.SPPID.Range, ref tempX, ref tempY, ref overlapX, ref overlapY);
5428
                        if (percentX >= percentY)
5429
                        {
5430
                            int count = Convert.ToInt32(tempY / gridSetting.Length) + 1;
5431
                            double move = gridSetting.Length * count;
5432
                            text.SPPID.SPPID_Y = text.SPPID.SPPID_Y - move;
5433
                            text.SPPID.Range = new double[] { text.SPPID.Range[0], text.SPPID.Range[1] - move, text.SPPID.Range[2], text.SPPID.Range[3] - move };
5434
                            needRemodeling = true;
5435
                            loop = true;
5436
                        }
5437
                        else
5438
                        {
5439
                            int count = Convert.ToInt32(tempX / gridSetting.Length) + 1;
5440
                            double move = gridSetting.Length * count;
5441
                            text.SPPID.SPPID_X = text.SPPID.SPPID_X + move;
5442
                            text.SPPID.Range = new double[] { text.SPPID.Range[0] + move, text.SPPID.Range[1], text.SPPID.Range[2] + move, text.SPPID.Range[3] };
5443
                            needRemodeling = true;
5444
                            loop = true;
5445
                        }
5446
                    }
5447
                }
5448
            }
5449

    
5450

    
5451
            if (needRemodeling)
5452
            {
5453
                LMSymbol symbol = dataSource.GetSymbol(text.SPPID.RepresentationId);
5454
                _placement.PIDRemovePlacement(symbol.AsLMRepresentation());
5455
                text.SPPID.RepresentationId = null;
5456

    
5457
                LMItemNote _LMItemNote = null;
5458
                LMAAttribute _LMAAttribute = null;
5459
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, text.SPPID.SPPID_X, text.SPPID.SPPID_Y, Rotation: text.ANGLE);
5460
                if (_LMSymbol != null)
5461
                {
5462
                    _LMSymbol.Commit();
5463
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5464
                    if (_LMItemNote != null)
5465
                    {
5466
                        _LMItemNote.Commit();
5467
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5468
                        if (_LMAAttribute != null)
5469
                        {
5470
                            _LMAAttribute.set_Value(text.VALUE);
5471
                            text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5472
                            _LMItemNote.Commit();
5473

    
5474
                            ReleaseCOMObjects(_LMAAttribute);
5475
                            ReleaseCOMObjects(_LMItemNote);
5476
                        }
5477
                    }
5478
                }
5479

    
5480
                ReleaseCOMObjects(symbol);
5481
                symbol = null;
5482
                ReleaseCOMObjects(_LMItemNote);
5483
                _LMItemNote = null;
5484
                ReleaseCOMObjects(_LMAAttribute);
5485
                _LMAAttribute = null;
5486
                ReleaseCOMObjects(_LMSymbol);
5487
                _LMSymbol = null;
5488
            }
5489
        }
5490

    
5491
        private void AssociationTextCorrectModeling(Text text, List<Text> endTexts)
5492
        {
5493
            if (!string.IsNullOrEmpty(text.SPPID.RepresentationId))
5494
            {
5495
                List<Text> allTexts = new List<Text>();
5496
                LMLabelPersist targetLabel = dataSource.GetLabelPersist(text.SPPID.RepresentationId);
5497
                LMRepresentation representation = targetLabel.RepresentationObject;
5498
                Symbol symbol = document.SYMBOLS.Find(x => x.SPPID.RepresentationId == representation.Id);
5499
                if (targetLabel.RepresentationObject != null && symbol != null)
5500
                {
5501
                    double[] symbolRange = null;
5502
                    GetSPPIDSymbolRange(symbol, ref symbolRange, true, true);
5503
                    if (symbolRange != null)
5504
                    {
5505
                        foreach (LMLabelPersist labelPersist in representation.LabelPersists)
5506
                        {
5507
                            Text findText = document.TEXTINFOS.Find(x => x.SPPID.RepresentationId == labelPersist.AsLMRepresentation().Id && x.ASSOCIATION);
5508
                            if (findText != null)
5509
                            {
5510
                                double[] range = null;
5511
                                GetSPPIDSymbolRange(labelPersist, ref range);
5512
                                findText.SPPID.Range = range;
5513
                                allTexts.Add(findText);
5514
                            }
5515

    
5516
                            ReleaseCOMObjects(labelPersist);
5517
                        }
5518

    
5519
                        if (allTexts.Count > 0)
5520
                        {
5521
                            #region Sort Text By Y
5522
                            allTexts.Sort(SortTextByY);
5523
                            int SortTextByY(Text a, Text b)
5524
                            {
5525
                                return b.SPPID.Range[3].CompareTo(a.SPPID.Range[3]);
5526
                            }
5527
                            #endregion
5528

    
5529
                            #region 정렬하기전 방향
5530
                            List<Text> left = new List<Text>();
5531
                            List<Text> down = new List<Text>();
5532
                            List<Text> right = new List<Text>();
5533
                            List<Text> up = new List<Text>();
5534
                            List<List<Text>> sortTexts = new List<List<Text>>() { left, down, right, up };
5535
                            foreach (var loopText in allTexts)
5536
                            {
5537
                                double textCenterX = (loopText.X1 + loopText.X2) / 2;
5538
                                double textCenterY = (loopText.Y1 + loopText.Y2) / 2;
5539
                                double originX = 0;
5540
                                double originY = 0;
5541
                                SPPIDUtil.ConvertPointBystring(symbol.ORIGINALPOINT, ref originX, ref originY);
5542
                                double angle = SPPIDUtil.CalcAngle(textCenterX, textCenterY, originX, originY);
5543

    
5544
                                if (angle < 45)
5545
                                {
5546
                                    // Text 오른쪽
5547
                                    if (textCenterX > originX)
5548
                                        right.Add(loopText);
5549
                                    // Text 왼쪽
5550
                                    else
5551
                                        left.Add(loopText);
5552
                                }
5553
                                else
5554
                                {
5555
                                    // Text 아래쪽
5556
                                    if (textCenterY > originY)
5557
                                        down.Add(loopText);
5558
                                    // Text 위쪽
5559
                                    else
5560
                                        up.Add(loopText);
5561
                                }
5562
                            }
5563

    
5564
                            #endregion
5565

    
5566
                            foreach (var texts in sortTexts)
5567
                            {
5568
                                if (texts.Count == 0)
5569
                                    continue;
5570

    
5571
                                #region 첫번째 Text로 기준 맞춤
5572
                                for (int i = 0; i < texts.Count; i++)
5573
                                {
5574
                                    if (i != 0)
5575
                                    {
5576
                                        Text currentText = texts[i];
5577
                                        Text prevText = texts[i - 1];
5578
                                        double minY = prevText.SPPID.Range[1];
5579
                                        double centerPrevX = (prevText.SPPID.Range[0] + prevText.SPPID.Range[2]) / 2;
5580
                                        double centerX = (currentText.SPPID.Range[0] + currentText.SPPID.Range[2]) / 2;
5581
                                        double _gapX = centerX - centerPrevX;
5582
                                        double _gapY = currentText.SPPID.Range[3] - minY;
5583
                                        MoveText(currentText, _gapX, _gapY);
5584
                                    }
5585
                                }
5586
                                List<double> rangeMinX = texts.Select(loopX => loopX.SPPID.Range[0]).ToList();
5587
                                List<double> rangeMinY = texts.Select(loopX => loopX.SPPID.Range[1]).ToList();
5588
                                List<double> rangeMaxX = texts.Select(loopX => loopX.SPPID.Range[2]).ToList();
5589
                                List<double> rangeMaxY = texts.Select(loopX => loopX.SPPID.Range[3]).ToList();
5590
                                rangeMinX.Sort();
5591
                                rangeMinY.Sort();
5592
                                rangeMaxX.Sort();
5593
                                rangeMaxY.Sort();
5594
                                double allTextCenterX = (rangeMinX[0] + rangeMaxX[rangeMaxX.Count - 1]) / 2;
5595
                                double allTextCenterY = (rangeMinY[0] + rangeMaxY[rangeMaxY.Count - 1]) / 2;
5596
                                #endregion
5597
                                #region 정렬
5598
                                Text correctBySymbol = texts[0];
5599
                                double textCenterX = (correctBySymbol.X1 + correctBySymbol.X2) / 2;
5600
                                double textCenterY = (correctBySymbol.Y1 + correctBySymbol.Y2) / 2;
5601
                                double originX = 0;
5602
                                double originY = 0;
5603
                                SPPIDUtil.ConvertPointBystring(symbol.ORIGINALPOINT, ref originX, ref originY);
5604
                                double angle = SPPIDUtil.CalcAngle(textCenterX, textCenterY, originX, originY);
5605
                                double symbolCenterX = (symbolRange[0] + symbolRange[2]) / 2;
5606
                                double symbolCenterY = (symbolRange[1] + symbolRange[3]) / 2;
5607

    
5608
                                double gapX = 0;
5609
                                double gapY = 0;
5610
                                if (angle < 45)
5611
                                {
5612
                                    // Text 오른쪽
5613
                                    if (textCenterX > originX)
5614
                                    {
5615
                                        gapX = rangeMinX[0] - symbolRange[2];
5616
                                        gapY = allTextCenterY - symbolCenterY;
5617
                                    }
5618
                                    // Text 왼쪽
5619
                                    else
5620
                                    {
5621
                                        gapX = rangeMaxX[rangeMaxX.Count - 1] - symbolRange[0];
5622
                                        gapY = allTextCenterY - symbolCenterY;
5623
                                    }
5624
                                }
5625
                                else
5626
                                {
5627
                                    // Text 아래쪽
5628
                                    if (textCenterY > originY)
5629
                                    {
5630
                                        gapX = allTextCenterX - symbolCenterX;
5631
                                        gapY = rangeMaxY[rangeMaxY.Count - 1] - symbolRange[1];
5632
                                    }
5633
                                    // Text 위쪽
5634
                                    else
5635
                                    {
5636
                                        gapX = allTextCenterX - symbolCenterX;
5637
                                        gapY = rangeMinY[0] - symbolRange[3];
5638
                                    }
5639
                                }
5640

    
5641
                                foreach (var item in texts)
5642
                                {
5643
                                    MoveText(item, gapX, gapY);
5644
                                    RemodelingAssociationText(item);
5645
                                }
5646
                                #endregion
5647
                            }
5648
                        }
5649
                    }
5650
                }
5651

    
5652
                void MoveText(Text moveText, double x, double y)
5653
                {
5654
                    moveText.SPPID.SPPID_X = moveText.SPPID.SPPID_X - x;
5655
                    moveText.SPPID.SPPID_Y = moveText.SPPID.SPPID_Y - y;
5656
                    moveText.SPPID.Range = new double[] {
5657
                        moveText.SPPID.Range[0] - x,
5658
                        moveText.SPPID.Range[1]- y,
5659
                        moveText.SPPID.Range[2]- x,
5660
                        moveText.SPPID.Range[3]- y
5661
                    };
5662
                }
5663

    
5664
                endTexts.AddRange(allTexts);
5665

    
5666
                ReleaseCOMObjects(targetLabel);
5667
                targetLabel = null;
5668
                ReleaseCOMObjects(representation);
5669
                representation = null;
5670
            }
5671
        }
5672

    
5673
        private void RemodelingAssociationText(Text text)
5674
        {
5675
            LMLabelPersist removeLabel = dataSource.GetLabelPersist(text.SPPID.RepresentationId);
5676
            _placement.PIDRemovePlacement(removeLabel.AsLMRepresentation());
5677
            removeLabel.Commit();
5678
            ReleaseCOMObjects(removeLabel);
5679
            removeLabel = null;
5680

    
5681
            object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
5682
            if (owner != null && owner.GetType() == typeof(Symbol))
5683
            {
5684
                Symbol symbol = owner as Symbol;
5685
                _LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
5686
                if (_LMSymbol != null)
5687
                {
5688
                    BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
5689
                    if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
5690
                    {
5691
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
5692

    
5693
                        if (mapping != null)
5694
                        {
5695
                            double x = 0;
5696
                            double y = 0;
5697

    
5698
                            Array array = new double[] { 0, text.SPPID.SPPID_X, text.SPPID.SPPID_Y };
5699
                            LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
5700
                            if (_LMLabelPersist != null)
5701
                            {
5702
                                text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
5703
                                _LMLabelPersist.Commit();
5704
                            }
5705
                            ReleaseCOMObjects(_LMLabelPersist);
5706
                            _LMLabelPersist = null;
5707
                        }
5708
                    }
5709
                }
5710
                ReleaseCOMObjects(_LMSymbol);
5711
                _LMSymbol = null;
5712
            }
5713
        }
5714

    
5715
        /// <summary>
5716
        /// Note Modeling
5717
        /// </summary>
5718
        /// <param name="note"></param>
5719
        private void NoteModeling(Note note, List<Note> correctList)
5720
        {
5721
            LMSymbol _LMSymbol = null;
5722
            LMItemNote _LMItemNote = null;
5723
            LMAAttribute _LMAAttribute = null;
5724

    
5725
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
5726
            {
5727
                double x = 0;
5728
                double y = 0;
5729

    
5730
                CalcLabelLocation(ref x, ref y, note.SPPID.ORIGINAL_X, note.SPPID.ORIGINAL_Y, note.SPPIDLabelLocation, _ETCSetting.NoteLocation);
5731
                SPPIDUtil.ConvertGridPoint(ref x, ref y);
5732
                note.SPPID.SPPID_X = x;
5733
                note.SPPID.SPPID_Y = y;
5734

    
5735
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
5736
                if (_LMSymbol != null)
5737
                {
5738
                    _LMSymbol.Commit();
5739
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5740
                    if (_LMItemNote != null)
5741
                    {
5742
                        _LMItemNote.Commit();
5743
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5744
                        if (_LMAAttribute != null)
5745
                        {
5746
                            _LMAAttribute.set_Value(note.VALUE);
5747
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5748

    
5749
                            double[] range = null;
5750
                            foreach (LMLabelPersist labelPersist in _LMSymbol.LabelPersists)
5751
                            {
5752
                                double[] temp = null;
5753
                                GetSPPIDSymbolRange(labelPersist, ref temp);
5754
                                if (temp != null)
5755
                                {
5756
                                    if (range == null)
5757
                                        range = temp;
5758
                                    else
5759
                                    {
5760
                                        range = new double[] {
5761
                                            Math.Min(range[0], temp[0]),
5762
                                            Math.Min(range[1], temp[1]),
5763
                                            Math.Max(range[2], temp[2]),
5764
                                            Math.Max(range[3], temp[3])
5765
                                        };
5766
                                    }
5767
                                }
5768
                            }
5769
                            if (range != null)
5770
                                correctList.Add(note);
5771
                            note.SPPID.Range = range;
5772

    
5773

    
5774
                            _LMItemNote.Commit();
5775
                        }
5776
                    }
5777
                }
5778
            }
5779

    
5780
            if (_LMAAttribute != null)
5781
                ReleaseCOMObjects(_LMAAttribute);
5782
            if (_LMItemNote != null)
5783
                ReleaseCOMObjects(_LMItemNote);
5784
            if (_LMSymbol != null)
5785
                ReleaseCOMObjects(_LMSymbol);
5786
        }
5787

    
5788
        private void NoteCorrectModeling(Note note, List<Note> endList)
5789
        {
5790
            bool needRemodeling = false;
5791
            bool loop = true;
5792
            GridSetting gridSetting = GridSetting.GetInstance();
5793
            while (loop)
5794
            {
5795
                loop = false;
5796
                foreach (var overlap in endList)
5797
                {
5798
                    if (SPPIDUtil.IsOverlap(overlap.SPPID.Range, note.SPPID.Range))
5799
                    {
5800
                        double tempX = 0;
5801
                        double tempY = 0;
5802
                        bool overlapX = false;
5803
                        bool overlapY = false;
5804
                        SPPIDUtil.CalcOverlap(note.SPPID.Range, overlap.SPPID.Range, ref tempX, ref tempY, ref overlapX, ref overlapY);
5805
                        double angle = SPPIDUtil.CalcAngle(note.LOCATION_X, note.LOCATION_Y, overlap.LOCATION_X, overlap.LOCATION_Y);
5806
                        if (overlapY && angle >= 45)
5807
                        {
5808
                            int count = Convert.ToInt32(tempY / gridSetting.Length) + 1;
5809
                            double move = gridSetting.Length * count;
5810
                            note.SPPID.SPPID_Y = note.SPPID.SPPID_Y - move;
5811
                            note.SPPID.Range = new double[] { note.SPPID.Range[0], note.SPPID.Range[1] - move, note.SPPID.Range[2], note.SPPID.Range[3] - move };
5812
                            needRemodeling = true;
5813
                            loop = true;
5814
                        }
5815
                        if (overlapX && angle <= 45)
5816
                        {
5817
                            int count = Convert.ToInt32(tempX / gridSetting.Length) + 1;
5818
                            double move = gridSetting.Length * count;
5819
                            note.SPPID.SPPID_X = note.SPPID.SPPID_X + move;
5820
                            note.SPPID.Range = new double[] { note.SPPID.Range[0] + move, note.SPPID.Range[1], note.SPPID.Range[2] + move, note.SPPID.Range[3] };
5821
                            needRemodeling = true;
5822
                            loop = true;
5823
                        }
5824
                    }
5825
                }
5826
            }
5827

    
5828

    
5829
            if (needRemodeling)
5830
            {
5831
                LMSymbol symbol = dataSource.GetSymbol(note.SPPID.RepresentationId);
5832
                _placement.PIDRemovePlacement(symbol.AsLMRepresentation());
5833
                note.SPPID.RepresentationId = null;
5834

    
5835
                LMItemNote _LMItemNote = null;
5836
                LMAAttribute _LMAAttribute = null;
5837
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, note.SPPID.SPPID_X, note.SPPID.SPPID_Y, Rotation: note.ANGLE);
5838
                if (_LMSymbol != null)
5839
                {
5840
                    _LMSymbol.Commit();
5841
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5842
                    if (_LMItemNote != null)
5843
                    {
5844
                        _LMItemNote.Commit();
5845
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5846
                        if (_LMAAttribute != null)
5847
                        {
5848
                            _LMAAttribute.set_Value(note.VALUE);
5849
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5850
                            _LMItemNote.Commit();
5851

    
5852
                            ReleaseCOMObjects(_LMAAttribute);
5853
                            ReleaseCOMObjects(_LMItemNote);
5854
                        }
5855
                    }
5856
                }
5857

    
5858
                ReleaseCOMObjects(symbol);
5859
                symbol = null;
5860
                ReleaseCOMObjects(_LMItemNote);
5861
                _LMItemNote = null;
5862
                ReleaseCOMObjects(_LMAAttribute);
5863
                _LMAAttribute = null;
5864
                ReleaseCOMObjects(_LMSymbol);
5865
                _LMSymbol = null;
5866
            }
5867

    
5868
            endList.Add(note);
5869
        }
5870

    
5871
        private void JoinRunBySameType(string modelItemId, ref string survivorId)
5872
        {
5873
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
5874
            if (modelItem != null)
5875
            {
5876
                foreach (LMRepresentation rep in modelItem.Representations)
5877
                {
5878
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
5879
                    {
5880
                        LMConnector connector = dataSource.GetConnector(rep.Id);
5881
                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
5882
                        {
5883
                            LMSymbol symbol = connector.ConnectItem1SymbolObject;
5884
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
5885
                            if (modelItemIds.Count == 1)
5886
                            {
5887
                                string joinModelItemId = modelItemIds[0];
5888
                                JoinRun(joinModelItemId, modelItemId, ref survivorId, false);
5889
                                if (survivorId != null)
5890
                                    break;
5891
                            }
5892
                        }
5893
                        if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
5894
                        {
5895
                            LMSymbol symbol = connector.ConnectItem2SymbolObject;
5896
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
5897
                            if (modelItemIds.Count == 1)
5898
                            {
5899
                                string joinModelItemId = modelItemIds[0];
5900
                                JoinRun(joinModelItemId, modelItemId, ref survivorId, false);
5901
                                if (survivorId != null)
5902
                                    break;
5903
                            }
5904
                        }
5905
                    }
5906
                }
5907
            }
5908
        }
5909

    
5910
        /// <summary>
5911
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
5912
        /// </summary>
5913
        /// <param name="x"></param>
5914
        /// <param name="y"></param>
5915
        /// <param name="originX"></param>
5916
        /// <param name="originY"></param>
5917
        /// <param name="SPPIDLabelLocation"></param>
5918
        /// <param name="location"></param>
5919
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
5920
        {
5921
            if (location == Location.None)
5922
            {
5923
                x = originX;
5924
                y = originY;
5925
            }
5926
            else
5927
            {
5928
                if (location.HasFlag(Location.Center))
5929
                {
5930
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
5931
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
5932
                }
5933

    
5934
                if (location.HasFlag(Location.Left))
5935
                    x = SPPIDLabelLocation.X1;
5936
                else if (location.HasFlag(Location.Right))
5937
                    x = SPPIDLabelLocation.X2;
5938

    
5939
                if (location.HasFlag(Location.Down))
5940
                    y = SPPIDLabelLocation.Y1;
5941
                else if (location.HasFlag(Location.Up))
5942
                    y = SPPIDLabelLocation.Y2;
5943
            }
5944
        }
5945

    
5946
        /// <summary>
5947
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
5948
        /// 1. Angle Valve
5949
        /// 2. 3개로 이루어진 Symbol Group
5950
        /// </summary>
5951
        /// <returns></returns>
5952
        private List<Symbol> GetPrioritySymbol()
5953
        {
5954
            DataTable symbolTable = document.SymbolTable;
5955
            // List에 순서대로 쌓는다.
5956
            List<Symbol> symbols = new List<Symbol>();
5957

    
5958
            // Angle Valve 부터
5959
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
5960
            {
5961
                if (!symbols.Contains(symbol))
5962
                {
5963
                    double originX = 0;
5964
                    double originY = 0;
5965

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

    
5970
                    SlopeType slopeType1 = SlopeType.None;
5971
                    SlopeType slopeType2 = SlopeType.None;
5972
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
5973
                    {
5974
                        double connectorX = 0;
5975
                        double connectorY = 0;
5976
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
5977
                        if (slopeType1 == SlopeType.None)
5978
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
5979
                        else
5980
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
5981
                    }
5982

    
5983
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
5984
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
5985
                        symbols.Add(symbol);
5986
                }
5987
            }
5988

    
5989
            List<Symbol> tempSymbols = new List<Symbol>();
5990
            // Conn 갯수 기준
5991
            foreach (var item in document.SYMBOLS)
5992
            {
5993
                if (!symbols.Contains(item))
5994
                    tempSymbols.Add(item);
5995
            }
5996
            tempSymbols.Sort(SortSymbolPriority);
5997
            symbols.AddRange(tempSymbols);
5998

    
5999
            return symbols;
6000
        }
6001

    
6002
        private void SetPriorityLine(List<Line> lines)
6003
        {
6004
            lines.Sort(SortLinePriority);
6005

    
6006
            int SortLinePriority(Line a, Line b)
6007
            {
6008
                // Branch 없는것부터
6009
                int branchRetval = CompareBranchLine(a, b);
6010
                if (branchRetval != 0)
6011
                {
6012
                    return branchRetval;
6013
                }
6014
                else
6015
                {
6016
                    // Symbol 연결 갯수
6017
                    int connSymbolRetval = CompareConnSymbol(a, b);
6018
                    if (connSymbolRetval != 0)
6019
                    {
6020
                        return connSymbolRetval;
6021
                    }
6022
                    else
6023
                    {
6024
                        // 아이템 연결 갯수(심볼, Line이면서 Not Branch)
6025
                        int connItemRetval = CompareConnItem(a, b);
6026
                        if (connItemRetval != 0)
6027
                        {
6028
                            return connItemRetval;
6029
                        }
6030
                        else
6031
                        {
6032
                            // ConnectedItem이 없는것
6033
                            int noneConnRetval = CompareNoneConn(a, b);
6034
                            if (noneConnRetval != 0)
6035
                            {
6036
                                return noneConnRetval;
6037
                            }
6038
                            else
6039
                            {
6040

    
6041
                            }
6042
                        }
6043
                    }
6044
                }
6045

    
6046
                return 0;
6047
            }
6048

    
6049
            int CompareNotSegmentLine(Line a, Line b)
6050
            {
6051
                List<Connector> connectorsA = a.CONNECTORS
6052
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
6053
                    .ToList();
6054

    
6055
                List<Connector> connectorsB = b.CONNECTORS
6056
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
6057
                    .ToList();
6058

    
6059
                // 오름차순
6060
                return connectorsB.Count.CompareTo(connectorsA.Count);
6061
            }
6062

    
6063
            int CompareConnSymbol(Line a, Line b)
6064
            {
6065
                List<Connector> connectorsA = a.CONNECTORS
6066
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
6067
                    .ToList();
6068

    
6069
                List<Connector> connectorsB = b.CONNECTORS
6070
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
6071
                    .ToList();
6072

    
6073
                // 오름차순
6074
                return connectorsB.Count.CompareTo(connectorsA.Count);
6075
            }
6076

    
6077
            int CompareConnItem(Line a, Line b)
6078
            {
6079
                List<Connector> connectorsA = a.CONNECTORS
6080
                    .Where(conn => conn.ConnectedObject != null &&
6081
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
6082
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
6083
                    .ToList();
6084

    
6085
                List<Connector> connectorsB = b.CONNECTORS
6086
                    .Where(conn => conn.ConnectedObject != null &&
6087
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
6088
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
6089
                    .ToList();
6090

    
6091
                // 오름차순
6092
                return connectorsB.Count.CompareTo(connectorsA.Count);
6093
            }
6094

    
6095
            int CompareBranchLine(Line a, Line b)
6096
            {
6097
                List<Connector> connectorsA = a.CONNECTORS
6098
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
6099
                    .ToList();
6100
                List<Connector> connectorsB = b.CONNECTORS
6101
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
6102
                    .ToList();
6103

    
6104
                // 내림차순
6105
                return connectorsA.Count.CompareTo(connectorsB.Count);
6106
            }
6107

    
6108
            int CompareNoneConn(Line a, Line b)
6109
            {
6110
                List<Connector> connectorsA = a.CONNECTORS
6111
                    .Where(conn => conn.ConnectedObject == null)
6112
                    .ToList();
6113

    
6114
                List<Connector> connectorsB = b.CONNECTORS
6115
                    .Where(conn => conn.ConnectedObject == null)
6116
                    .ToList();
6117

    
6118
                // 오름차순
6119
                return connectorsB.Count.CompareTo(connectorsA.Count);
6120
            }
6121
        }
6122

    
6123
        private void SortText(List<Text> texts)
6124
        {
6125
            texts.Sort(Sort);
6126

    
6127
            int Sort(Text a, Text b)
6128
            {
6129
                int yRetval = CompareY(a, b);
6130
                if (yRetval != 0)
6131
                {
6132
                    return yRetval;
6133
                }
6134
                else
6135
                {
6136
                    return CompareX(a, b);
6137
                }
6138
            }
6139

    
6140
            int CompareY(Text a, Text b)
6141
            {
6142
                return a.LOCATION_Y.CompareTo(b.LOCATION_Y);
6143
            }
6144

    
6145
            int CompareX(Text a, Text b)
6146
            {
6147
                return a.LOCATION_X.CompareTo(b.LOCATION_X);
6148
            }
6149
        }
6150
        private void SortNote(List<Note> notes)
6151
        {
6152
            notes.Sort(Sort);
6153

    
6154
            int Sort(Note a, Note b)
6155
            {
6156
                int yRetval = CompareY(a, b);
6157
                if (yRetval != 0)
6158
                {
6159
                    return yRetval;
6160
                }
6161
                else
6162
                {
6163
                    return CompareX(a, b);
6164
                }
6165
            }
6166

    
6167
            int CompareY(Note a, Note b)
6168
            {
6169
                return a.LOCATION_Y.CompareTo(b.LOCATION_Y);
6170
            }
6171

    
6172
            int CompareX(Note a, Note b)
6173
            {
6174
                return a.LOCATION_X.CompareTo(b.LOCATION_X);
6175
            }
6176
        }
6177

    
6178
        private void SortBranchLines()
6179
        {
6180
            BranchLines.Sort(SortBranchLine);
6181
            int SortBranchLine(Line a, Line b)
6182
            {
6183
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
6184
                 x.ConnectedObject.GetType() == typeof(Line) &&
6185
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
6186
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
6187

    
6188
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
6189
                 x.ConnectedObject.GetType() == typeof(Line) &&
6190
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
6191
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
6192

    
6193
                // 내림차순
6194
                return countA.CompareTo(countB);
6195
            }
6196
        }
6197

    
6198
        private static int SortSymbolPriority(Symbol a, Symbol b)
6199
        {
6200
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
6201
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
6202
            int retval = countB.CompareTo(countA);
6203
            if (retval != 0)
6204
                return retval;
6205
            else
6206
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
6207
        }
6208

    
6209
        private string GetSPPIDFileName(LMModelItem modelItem)
6210
        {
6211
            string symbolPath = null;
6212
            foreach (LMRepresentation rep in modelItem.Representations)
6213
            {
6214
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
6215
                {
6216
                    symbolPath = rep.get_FileName();
6217
                    break;
6218
                }
6219
            }
6220
            return symbolPath;
6221
        }
6222

    
6223
        private string GetSPPIDFileName(string modelItemId)
6224
        {
6225
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
6226
            string symbolPath = null;
6227
            foreach (LMRepresentation rep in modelItem.Representations)
6228
            {
6229
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
6230
                {
6231
                    symbolPath = rep.get_FileName();
6232
                    break;
6233
                }
6234
            }
6235
            ReleaseCOMObjects(modelItem);
6236
            return symbolPath;
6237
        }
6238

    
6239
        /// <summary>
6240
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
6241
        /// </summary>
6242
        /// <param name="graphicOID"></param>
6243
        /// <param name="milliseconds"></param>
6244
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
6245
        {
6246
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
6247
            {
6248
                double minX = 0;
6249
                double minY = 0;
6250
                double maxX = 0;
6251
                double maxY = 0;
6252
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
6253
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
6254

    
6255
                Thread.Sleep(milliseconds);
6256
            }
6257
        }
6258

    
6259
        /// <summary>
6260
        /// ComObject를 Release
6261
        /// </summary>
6262
        /// <param name="objVars"></param>
6263
        public void ReleaseCOMObjects(params object[] objVars)
6264
        {
6265
            if (objVars != null)
6266
            {
6267
                int intNewRefCount = 0;
6268
                foreach (object obj in objVars)
6269
                {
6270
                    if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
6271
                        intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
6272
                }
6273
            }
6274
        }
6275

    
6276
        /// IDisposable 구현
6277
        ~AutoModeling()
6278
        {
6279
            this.Dispose(false);
6280
        }
6281

    
6282
        private bool disposed;
6283
        public void Dispose()
6284
        {
6285
            this.Dispose(true);
6286
            GC.SuppressFinalize(this);
6287
        }
6288

    
6289
        protected virtual void Dispose(bool disposing)
6290
        {
6291
            if (this.disposed) return;
6292
            if (disposing)
6293
            {
6294
                // IDisposable 인터페이스를 구현하는 멤버들을 여기서 정리합니다.
6295
            }
6296
            // .NET Framework에 의하여 관리되지 않는 외부 리소스들을 여기서 정리합니다.
6297
            this.disposed = true;
6298
        }
6299
    }
6300
}
클립보드 이미지 추가 (최대 크기: 500 MB)