프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 0b885fb1

이력 | 보기 | 이력해설 | 다운로드 (320 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<double[]> itemRange = new List<double[]>();
51

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

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

    
65
            this.closeDocument = closeDocument;
66
            this.document = document;
67
            this._ETCSetting = ETCSetting.GetInstance();
68
        }
69

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

    
82
                }
83
            }
84
        }
85

    
86
        /// <summary>
87
        /// 도면 단위당 실행되는 메서드
88
        /// </summary>
89
        public void Run()
90
        {
91
            string drawingNumber = document.DrawingNumber;
92
            string drawingName = document.DrawingName;
93
            try
94
            {
95
                nominalDiameterTable = Project_DB.SelectProjectNominalDiameter();
96
                _placement = new Placement();
97
                dataSource = _placement.PIDDataSource;
98
                
99
                if (CreateDocument(ref drawingNumber, ref drawingName) && DocumentCoordinateCorrection())
100
                {
101
                    Log.Write("Start Modeling");
102
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
103
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetParent, (IntPtr)radApp.HWnd);
104
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllStepCount, 25);
105
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetDocumentName, DocumentLabelText);
106

    
107
                    // VendorPackage Modeling
108
                    // ID2에서 VendorPackage로 된 Symbol을 SPPID에서 그림
109
                    RunVendorPackageModeling();
110
                    // Equipment Modeling
111
                    // Id2에서 인식한 Equipment일 경우 SPPID에 Draft
112
                    RunEquipmentModeling();
113
                    // Symbol Modeling
114
                    // ID2의 Symbol Draft
115
                    // 단 Symbol draft할 때 붙어 있는 symbol도 draft함
116
                    RunSymbolModeling();
117
                    // LineRun Line Modeling
118
                    // Line 그리는 우선 순위 
119
                    // 1. Branch 없는 것
120
                    // 2. Symbol 연결 개수
121
                    // 3. Symbol 제외 Item 연결 개수
122
                    // 4. ConnectedItem이 없는것
123
                    RunLineModeling();
124
                    // Vent Drain Modeling
125
                    // Vent/Drain으로 인식한 Item draft
126
                    // 인식 조건
127
                    // 1. ID2에서 Line이 하나며 Branch된 Line이 있고
128
                    // 2. Valve가 line에 붙어있다.
129
                    RunVentDrainModeling();
130
                    // Clear Attribute
131
                    // SPPID에서 Line 생성 시 자동으로 Nominal Diameter가 입력되는 경우가 있음
132
                    // 모든 Item의 Nominal Diameter 속성값 초기화
133
                    RunClearNominalDiameter();
134
                    // Join SameConnector
135
                    // 기존 Line을 그릴때 SPPID에서는 같은 Run으로 생성되지 않고 각각 PipeRun이 생성됨
136
                    // ID2의 EndBreak등 segmentbreak가 없으면 Line을 합침
137
                    RunJoinRunForSameConnector();
138
                    // Join Run
139
                    // 같은 Type의 Line일 경우 Join함
140
                    RunJoinRun();
141

    
142
                    // avoid interference
143
                    SetConnectorAndSymbolRange();
144
                    // EndBreak Modeling
145
                    RunEndBreakModeling();
146
                    // avoid interference
147
                    SetConnectorAndSymbolRange();
148
                    // SpecBreak Modeling
149
                    RunSpecBreakModeling();
150
                    //Line Number Modeling
151
                    // Label만 draft
152
                    RunLineNumberModeling();
153
                    // Note Modeling
154
                    RunNoteModeling();
155
                    // Text Modeling
156
                    RunTextModeling();
157
                    // Input LineNumber Attribute
158
                    RunInputLineNumberAttribute();
159
                    // Input Symbol Attribute
160
                    RunInputSymbolAttribute();
161
                    // Input SpecBreak Attribute
162
                    RunInputSpecBreakAttribute();
163
                    // Input EndBreak Attribute
164
                    RunInputEndBreakAttribute();
165
                    // Label Symbol Modeling
166
                    RunLabelSymbolModeling();
167

    
168
                    // Correct Text
169
                    // LabelPersist 정렬 로직
170
                    // 예) Valve Size label 등
171
                    RunCorrectAssociationText();
172
                    // ETC
173
                    // Label을 Front로 옮김
174
                    RunETC();
175
                    // input bulk attribute
176
                    RunBulkAttribute();
177
                    // log file 생성
178
                    document.CheckModelingResult();
179
                }
180
            }
181
            catch (Exception ex)
182
            {
183
                if (SplashScreenManager.Default != null && SplashScreenManager.Default.IsSplashFormVisible)
184
                {
185
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
186
                    SplashScreenManager.CloseForm(false);
187
                    Log.Write("\r\n");
188
                }
189
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
190
            }
191
            finally
192
            {
193
                Project_DB.InsertDrawingInfoAndOPCInfo(document.PATH, drawingNumber, drawingName, document);
194
                //Project_DB.InsertLineNumberInfo(document.PATH, drawingNumber, drawingName, document);
195

    
196
                if (SplashScreenManager.Default != null && SplashScreenManager.Default.IsSplashFormVisible)
197
                {
198
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
199
                    SplashScreenManager.CloseForm(false);
200
                    Log.Write("\r\n");
201
                }
202
                Thread.Sleep(1000);
203

    
204
                Log.Write("End Modeling");
205
                radApp.ActiveWindow.Fit();
206

    
207
                ReleaseCOMObjects(application);
208
                application = null;
209
                if (radApp.ActiveDocument != null)
210
                {
211
                    if (closeDocument && newDrawing != null)
212
                    {
213
                        newDrawing.Save();
214
                        newDrawing.CloseDrawing(true);
215
                        ReleaseCOMObjects(newDrawing); 
216
                        newDrawing = null;
217
                    }
218
                    else if (newDrawing == null)
219
                    {
220
                        Log.Write("error document");
221
                    }
222
                }
223

    
224
                ReleaseCOMObjects(dataSource);
225
                dataSource = null;
226
                ReleaseCOMObjects(_placement);
227
                _placement = null;
228

    
229
                Thread.Sleep(1000);
230
            }
231
        }
232

    
233
        private void RunVendorPackageModeling()
234
        {
235
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.VendorPackages.Count);
236
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "VendorPackages Modeling");
237
            foreach (VendorPackage item in document.VendorPackages)
238
            {
239
                try
240
                {
241
                    VendorPackageModeling(item);
242
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
243
                }
244
                catch (Exception ex)
245
                {
246
                    Log.Write("Error in RunVendorPackageModeling");
247
                    Log.Write("UID : " + item.UID);
248
                    Log.Write(ex.Message);
249
                    Log.Write(ex.StackTrace);
250
                }
251
            }
252
        }
253
        private void RunEquipmentModeling()
254
        {
255
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Equipments.Count);
256
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
257
            for (int i = 0; i < document.Equipments.Count; i++)
258
            {
259
                Equipment item = document.Equipments[i];
260
                try
261
                {
262
                    if (!string.IsNullOrEmpty(item.SPPID.RepresentationId))
263
                        continue;
264
                    EquipmentModeling(item);
265
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.Equipments.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
266
                    if (!string.IsNullOrEmpty(item.SPPID.RepresentationId))
267
                        i = -1;
268
                }
269
                catch (Exception ex)
270
                {
271
                    Log.Write("Error in EquipmentModeling");
272
                    Log.Write("UID : " + item.UID);
273
                    Log.Write(ex.Message);
274
                    Log.Write(ex.StackTrace);
275
                }
276
            }
277
        }
278
        private void RunSymbolModeling()
279
        {
280
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
281
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
282
            prioritySymbols = GetPrioritySymbol();
283
            foreach (var item in prioritySymbols)
284
            {
285
                try
286
                {
287
                    if (document.VentDrainSymbol.Contains(item))
288
                        continue;
289
                    SymbolModelingBySymbol(item);
290
                }
291
                catch (Exception ex)
292
                {
293
                    Log.Write("Error in SymbolModelingByPriority");
294
                    Log.Write("UID : " + item.UID);
295
                    Log.Write(ex.Message);
296
                    Log.Write(ex.StackTrace);
297
                }
298
            }
299
        }
300
        private void RunLineModeling()
301
        {
302
            List<Line> AllLine = document.LINES.ToList();
303
            List<Line> stepLast_Line = document.LINES.FindAll(x => x.CONNECTORS.FindAll(y => y.ConnectedObject != null
304
                                                                                     && y.ConnectedObject.GetType() == typeof(Symbol)).Count == 2
305
                                                                                     && !SPPIDUtil.IsBranchedLine(document, x));
306
            List<Line> step1_Line = AllLine.FindAll(x => !stepLast_Line.Contains(x));
307

    
308
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, step1_Line.Count);
309
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 1");
310

    
311
            SetPriorityLine(step1_Line);
312
            foreach (var item in step1_Line)
313
            {
314
                try
315
                {
316
                    if (document.VentDrainLine.Contains(item))
317
                        continue;
318
                    NewLineModeling(item);
319
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
320
                }
321
                catch (Exception ex)
322
                {
323
                    Log.Write("Error in NewLineModeling");
324
                    Log.Write("UID : " + item.UID);
325
                    Log.Write(ex.Message);
326
                    Log.Write(ex.StackTrace);
327
                }
328
            }
329

    
330
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, BranchLines.Count);
331
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 2");
332
            int branchCount = BranchLines.Count;
333
            while (BranchLines.Count > 0)
334
            {
335
                try
336
                {
337
                    SortBranchLines();
338
                    Line item = BranchLines[0];
339
                    NewLineModeling(item, true);
340
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
341
                }
342
                catch (Exception ex)
343
                {
344
                    Log.Write("Error in NewLineModeling");
345
                    Log.Write("UID : " + BranchLines[0].UID);
346
                    Log.Write(ex.Message);
347
                    Log.Write(ex.StackTrace);
348
                    BranchLines.Remove(BranchLines[0]);
349
                }
350
            }
351

    
352
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, stepLast_Line.Count);
353
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 3");
354
            foreach (var item in stepLast_Line)
355
            {
356
                try
357
                {
358
                    if (document.VentDrainLine.Contains(item))
359
                        continue;
360
                    NewLineModeling(item);
361
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
362
                }
363
                catch (Exception ex)
364
                {
365
                    Log.Write("Error in NewLineModeling");
366
                    Log.Write("UID : " + item.UID);
367
                    Log.Write(ex.Message);
368
                    Log.Write(ex.StackTrace);
369
                }
370
            }
371
        }
372
        private void RunVentDrainModeling()
373
        {
374
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.VentDrainLine.Count);
375
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Vent Drain Modeling");
376
            foreach (var item in document.VentDrainLine)
377
            {
378
                try
379
                {
380
                    Connector connector = item.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
381
                    if (connector != null)
382
                    {
383
                        SetCoordinate();
384
                        Symbol connSymbol = connector.ConnectedObject as Symbol;
385
                        SymbolModeling(connSymbol, null);
386
                        NewLineModeling(item, true);
387

    
388
                        GridSetting grid = GridSetting.GetInstance();
389
                        int count = grid.DrainValveCellCount;
390
                        double length = grid.Length;
391

    
392
                        // 길이 확인
393
                        if (!string.IsNullOrEmpty(item.SPPID.ModelItemId))
394
                        {
395
                            LMConnector _LMConnector = GetLMConnectorOnlyOne(item.SPPID.ModelItemId);
396
                            if (_LMConnector != null)
397
                            {
398
                                double[] connectorRange = GetConnectorRange(_LMConnector);
399
                                double connectorLength = double.NaN;
400
                                if (item.SlopeType == SlopeType.HORIZONTAL)
401
                                    connectorLength = connectorRange[2] - connectorRange[0];
402
                                else if (item.SlopeType == SlopeType.VERTICAL)
403
                                    connectorLength = connectorRange[3] - connectorRange[1];
404

    
405
                                if (!double.IsNaN(connectorLength) && connectorLength != count * length)
406
                                {
407
                                    double move = count * length - connectorLength;
408
                                    List<Symbol> group = new List<Symbol>();
409
                                    SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
410
                                    foreach (var symbol in group)
411
                                    {
412
                                        int connSymbolIndex = item.CONNECTORS.IndexOf(item.CONNECTORS.Find(x => x.ConnectedObject == connSymbol));
413
                                        if (item.SlopeType == SlopeType.HORIZONTAL)
414
                                        {
415
                                            if (connSymbolIndex == 0)
416
                                            {
417
                                                if (item.SPPID.START_X > item.SPPID.END_X)
418
                                                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X + move;
419
                                                else
420
                                                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X - move;
421
                                            }
422
                                            else
423
                                            {
424
                                                if (item.SPPID.START_X < item.SPPID.END_X)
425
                                                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X + move;
426
                                                else
427
                                                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X - move;
428
                                            }
429
                                        }
430
                                        else if (item.SlopeType == SlopeType.VERTICAL)
431
                                        {
432
                                            if (connSymbolIndex == 0)
433
                                            {
434
                                                if (item.SPPID.START_Y > item.SPPID.END_Y)
435
                                                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y + move;
436
                                                else
437
                                                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y - move;
438
                                            }
439
                                            else
440
                                            {
441
                                                if (item.SPPID.START_Y < item.SPPID.END_Y)
442
                                                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y + move;
443
                                                else
444
                                                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y - move;
445
                                            }
446
                                        }
447
                                    }
448

    
449
                                    // 제거
450
                                    RemoveSymbol(connSymbol);
451
                                    RemoveLine(item);
452

    
453
                                    // 재생성
454
                                    SymbolModelingBySymbol(connSymbol);
455
                                    NewLineModeling(item, true);
456
                                }
457
                            }
458

    
459
                            ReleaseCOMObjects(_LMConnector);
460
                            _LMConnector = null;
461
                        }
462
                    }
463
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
464
                }
465
                catch (Exception ex)
466
                {
467
                    Log.Write("Error in NewLineModeling");
468
                    Log.Write("UID : " + item.UID);
469
                    Log.Write(ex.Message);
470
                    Log.Write(ex.StackTrace);
471
                }
472

    
473
                void SetCoordinate()
474
                {
475
                    Connector branchConnector = item.CONNECTORS.Find(loop => loop.ConnectedObject != null && loop.ConnectedObject.GetType() == typeof(Line));
476
                    if (branchConnector != null)
477
                    {
478
                        Line connLine = branchConnector.ConnectedObject as Line;
479
                        double x = 0;
480
                        double y = 0;
481
                        GetTargetLineConnectorPoint(branchConnector, item, ref x, ref y);
482
                        LMConnector targetConnector = FindTargetLMConnectorForBranch(item, connLine, ref x, ref y);
483
                        if (targetConnector != null)
484
                        {
485
                            List<Symbol> group = new List<Symbol>();
486
                            SPPIDUtil.FindConnectedSymbolGroup(document, item.CONNECTORS.Find(loop => loop != branchConnector).ConnectedObject as Symbol, group);
487
                            if (item.SlopeType == SlopeType.HORIZONTAL)
488
                            {
489
                                item.SPPID.START_Y = y;
490
                                item.SPPID.END_Y = y;
491
                                foreach (var symbol in group)
492
                                {
493
                                    symbol.SPPID.ORIGINAL_Y = y;
494
                                    symbol.SPPID.SPPID_Y = y;
495
                                }
496
                            }
497
                            else if (item.SlopeType == SlopeType.VERTICAL)
498
                            {
499
                                item.SPPID.START_X = x;
500
                                item.SPPID.END_X = x;
501
                                foreach (var symbol in group)
502
                                {
503
                                    symbol.SPPID.ORIGINAL_X = x;
504
                                    symbol.SPPID.SPPID_X = x;
505
                                }
506
                            }
507
                        }
508
                        ReleaseCOMObjects(targetConnector);
509
                        targetConnector = null;
510
                    }
511
                }
512
            }
513
        }
514
        private void RunClearNominalDiameter()
515
        {
516
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count + document.LINES.Count);
517
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Clear Attribute");
518
            return;
519

    
520
            List<string> endClearModelItemID = new List<string>();
521
            for (int i = 0; i < document.LINES.Count; i++)
522
            {
523
                Line item = document.LINES[i];
524
                string modelItemID = item.SPPID.ModelItemId;
525
                if (!string.IsNullOrEmpty(modelItemID))
526
                {
527
                    LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
528
                    if (modelItem != null)
529
                    {
530
                        LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
531
                        if (attribute != null)
532
                            attribute.set_Value(DBNull.Value);
533

    
534
                        modelItem.Commit();
535
                        ReleaseCOMObjects(modelItem);
536
                        modelItem = null;
537
                    }
538
                }
539
                if (!endClearModelItemID.Contains(modelItemID))
540
                    endClearModelItemID.Add(modelItemID);
541
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
542
            }
543
            for (int i = 0; i < document.SYMBOLS.Count; i++)
544
            {
545
                Symbol item = document.SYMBOLS[i];
546
                string repID = item.SPPID.RepresentationId;
547
                string modelItemID = item.SPPID.ModelItemID;
548
                if (!string.IsNullOrEmpty(modelItemID))
549
                {
550
                    LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
551
                    if (modelItem != null)
552
                    {
553
                        LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
554
                        if (attribute != null)
555
                            attribute.set_Value(DBNull.Value);
556
                        int index = 1;
557
                        while (true)
558
                        {
559
                            attribute = modelItem.Attributes[string.Format("PipingPoint{0}.NominalDiameter", index)];
560
                            if (attribute != null)
561
                                attribute.set_Value(DBNull.Value);
562
                            else
563
                                break;
564
                            index++;
565
                        }
566
                        modelItem.Commit();
567
                        ReleaseCOMObjects(modelItem);
568
                        modelItem = null;
569
                    }
570
                }
571
                if (!string.IsNullOrEmpty(repID))
572
                {
573
                    LMSymbol symbol = dataSource.GetSymbol(repID);
574
                    if (symbol != null)
575
                    {
576
                        foreach (LMConnector connector in symbol.Connect1Connectors)
577
                        {
578
                            if (connector.get_ItemStatus() == "Active" && !endClearModelItemID.Contains(connector.ModelItemID))
579
                            {
580
                                endClearModelItemID.Add(connector.ModelItemID);
581
                                LMModelItem modelItem = connector.ModelItemObject;
582
                                if (modelItem != null)
583
                                {
584
                                    LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
585
                                    if (attribute != null)
586
                                        attribute.set_Value(DBNull.Value);
587

    
588
                                    modelItem.Commit();
589
                                    ReleaseCOMObjects(modelItem);
590
                                    modelItem = null;
591
                                }
592
                            }
593
                        }
594
                        foreach (LMConnector connector in symbol.Connect2Connectors)
595
                        {
596
                            if (connector.get_ItemStatus() == "Active" && !endClearModelItemID.Contains(connector.ModelItemID))
597
                            {
598
                                endClearModelItemID.Add(connector.ModelItemID);
599
                                LMModelItem modelItem = connector.ModelItemObject;
600
                                if (modelItem != null)
601
                                {
602
                                    LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
603
                                    if (attribute != null)
604
                                        attribute.set_Value(DBNull.Value);
605

    
606
                                    modelItem.Commit();
607
                                    ReleaseCOMObjects(modelItem);
608
                                    modelItem = null;
609
                                }
610
                            }
611
                        }
612
                    }
613
                    ReleaseCOMObjects(symbol);
614
                    symbol = null;
615
                }
616
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
617
            }
618
        }
619
        private void RunClearValueInconsistancy()
620
        {
621
            int count = 1;
622
            bool loop = true;
623
            while (loop)
624
            {
625
                loop = false;
626
                LMAFilter filter = new LMAFilter();
627
                LMACriterion criterion = new LMACriterion();
628
                filter.ItemType = "Relationship";
629
                criterion.SourceAttributeName = "SP_DRAWINGID";
630
                criterion.Operator = "=";
631
                criterion.set_ValueAttribute(drawingID);
632
                filter.get_Criteria().Add(criterion);
633

    
634
                LMRelationships relationships = new LMRelationships();
635
                relationships.Collect(dataSource, Filter: filter);
636

    
637
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, relationships.Count);
638
                if (count > 1)
639
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStepMinus, null);
640
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Clear Inconsistent Property Value - " + count);
641
                foreach (LMRelationship relationship in relationships)
642
                {
643
                    foreach (LMInconsistency inconsistency in relationship.Inconsistencies)
644
                    {
645
                        if (inconsistency.get_InconsistencyTypeIndex() == 1)
646
                        {
647
                            LMModelItem modelItem1 = relationship.Item1RepresentationObject == null ? null : relationship.Item1RepresentationObject.ModelItemObject;
648
                            LMModelItem modelItem2 = relationship.Item2RepresentationObject == null ? null : relationship.Item2RepresentationObject.ModelItemObject;
649
                            string[] array = inconsistency.get_Name().ToString().Split(new char[] { '=' });
650
                            if (modelItem1 != null)
651
                            {
652
                                string attrName = array[0];
653
                                if (attrName.Contains("PipingPoint"))
654
                                {
655
                                    string originalAttr = attrName.Split(new char[] { '.' })[1];
656
                                    int index = Convert.ToInt32(relationship.get_Item1Location());
657
                                    LMAAttribute attribute1 = modelItem1.Attributes["PipingPoint" + index + "." + originalAttr];
658
                                    if (attribute1 != null && !DBNull.Value.Equals(attribute1.get_Value()))
659
                                    {
660
                                        loop = true;
661
                                        attribute1.set_Value(DBNull.Value);
662
                                    }
663
                                    attribute1 = null;
664
                                }
665
                                else
666
                                {
667
                                    LMAAttribute attribute1 = modelItem1.Attributes[attrName];
668
                                    if (attribute1 != null && !DBNull.Value.Equals(attribute1.get_Value()))
669
                                    {
670
                                        loop = true;
671
                                        attribute1.set_Value(DBNull.Value);
672
                                    }
673
                                    attribute1 = null;
674
                                }
675
                                modelItem1.Commit();
676
                            }
677
                            if (modelItem2 != null)
678
                            {
679
                                string attrName = array[1];
680
                                if (attrName.Contains("PipingPoint"))
681
                                {
682
                                    string originalAttr = attrName.Split(new char[] { '.' })[1];
683
                                    int index = Convert.ToInt32(relationship.get_Item2Location());
684
                                    LMAAttribute attribute2 = modelItem2.Attributes["PipingPoint" + index + "." + originalAttr];
685
                                    if (attribute2 != null && !DBNull.Value.Equals(attribute2.get_Value()))
686
                                    {
687
                                        attribute2.set_Value(DBNull.Value);
688
                                        loop = true;
689
                                    }
690
                                    attribute2 = null;
691
                                }
692
                                else
693
                                {
694
                                    LMAAttribute attribute2 = modelItem2.Attributes[attrName];
695
                                    if (attribute2 != null && !DBNull.Value.Equals(attribute2.get_Value()))
696
                                    {
697
                                        attribute2.set_Value(DBNull.Value);
698
                                        loop = true;
699
                                    }
700
                                    attribute2 = null;
701
                                }
702
                                modelItem2.Commit();
703
                            }
704
                            ReleaseCOMObjects(modelItem1);
705
                            modelItem1 = null;
706
                            ReleaseCOMObjects(modelItem2);
707
                            modelItem2 = null;
708
                            inconsistency.Commit();
709
                        }
710
                        ReleaseCOMObjects(inconsistency);
711
                    }
712
                    relationship.Commit();
713
                    ReleaseCOMObjects(relationship);
714
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
715
                }
716
                ReleaseCOMObjects(filter);
717
                filter = null;
718
                ReleaseCOMObjects(criterion);
719
                criterion = null;
720
                ReleaseCOMObjects(relationships);
721
                relationships = null;
722
                count++;
723
            }
724
        }
725
        private void RunEndBreakModeling()
726
        {
727
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.EndBreaks.Count);
728
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
729
            foreach (var item in document.EndBreaks)
730
                try
731
                {
732
                    EndBreakModeling(item);
733
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
734
                }
735
                catch (Exception ex)
736
                {
737
                    Log.Write("Error in EndBreakModeling");
738
                    Log.Write("UID : " + item.UID);
739
                    Log.Write(ex.Message);
740
                    Log.Write(ex.StackTrace);
741
                }
742
        }
743
        private void RunSpecBreakModeling()
744
        {
745
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SpecBreaks.Count);
746
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "SpecBreaks Modeling");
747
            foreach (var item in document.SpecBreaks)
748
                try
749
                {
750
                    SpecBreakModeling(item);
751
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
752
                }
753
                catch (Exception ex)
754
                {
755
                    Log.Write("Error in SpecBreakModeling");
756
                    Log.Write("UID : " + item.UID);
757
                    Log.Write(ex.Message);
758
                    Log.Write(ex.StackTrace);
759
                }
760
        }
761
        private void RunJoinRunForSameConnector()
762
        {
763
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
764
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "PipeRun Join - 1");
765
            foreach (var line in document.LINES)
766
            {
767
                Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(line.SPPID.ModelItemId, false);
768
                List<List<double[]>> result = new List<List<double[]>>();
769
                foreach (var item in vertices)
770
                {
771
                    ReleaseCOMObjects(item.Key);
772
                    result.Add(item.Value);
773
                }
774
                line.SPPID.Vertices = result;
775
                vertices = null;
776
            }
777

    
778
            foreach (var line in document.LINES)
779
            {
780
                foreach (var connector in line.CONNECTORS)
781
                {
782
                    if (connector.ConnectedObject != null &&
783
                        connector.ConnectedObject.GetType() == typeof(Line) &&
784
                        !SPPIDUtil.IsBranchLine(line, connector.ConnectedObject as Line))
785
                    {
786
                        Line connLine = connector.ConnectedObject as Line;
787
                        if (line.SPPID.ModelItemId != connLine.SPPID.ModelItemId &&
788
                            !string.IsNullOrEmpty(line.SPPID.ModelItemId) &&
789
                            !string.IsNullOrEmpty(connLine.SPPID.ModelItemId) &&
790
                            !SPPIDUtil.IsSegment(document, line, connLine))
791
                        {
792
                            string survivorId = string.Empty;
793
                            JoinRun(connLine.SPPID.ModelItemId, line.SPPID.ModelItemId, ref survivorId);
794
                        }
795

    
796
                    }
797
                }
798
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
799
            }
800

    
801
            foreach (var line in document.LINES)
802
                line.SPPID.Representations = GetRepresentations(line.SPPID.ModelItemId);
803
        }
804
        private void RunJoinRun()
805
        {
806
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
807
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "PipeRun Join - 2");
808
            List<string> endModelID = new List<string>();
809
            foreach (var line in document.LINES)
810
            {
811
                if (!endModelID.Contains(line.SPPID.ModelItemId))
812
                {
813
                    while (!endModelID.Contains(line.SPPID.ModelItemId))
814
                    {
815
                        string survivorId = string.Empty;
816
                        JoinRunBySameType(line.SPPID.ModelItemId, ref survivorId);
817
                        if (string.IsNullOrEmpty(survivorId))
818
                        {
819
                            endModelID.Add(line.SPPID.ModelItemId);
820
                        }
821
                    }
822
                }
823
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
824
            }
825
        }
826
        private void RunLineNumberModeling()
827
        {
828
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINENUMBERS.Count);
829
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Number Modeling");
830
            foreach (var item in document.LINENUMBERS)
831
            {
832
                LMLabelPersist label = dataSource.GetLabelPersist(item.SPPID.RepresentationId);
833
                if (label == null || (label != null && label.get_ItemStatus() != "Active"))
834
                {
835
                    ReleaseCOMObjects(label);
836
                    item.SPPID.RepresentationId = null;
837
                    LineNumberModeling(item);
838
                }
839
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
840
            }
841
        }
842
        private void RunNoteModeling()
843
        {
844
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
845
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
846
            List<Note> correctList = new List<Note>();
847
            foreach (var item in document.NOTES)
848
                try
849
                {
850
                    NoteModeling(item, correctList);
851
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
852
                }
853
                catch (Exception ex)
854
                {
855
                    Log.Write("Error in NoteModeling");
856
                    Log.Write("UID : " + item.UID);
857
                    Log.Write(ex.Message);
858
                    Log.Write(ex.StackTrace);
859
                }
860

    
861
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, correctList.Count);
862
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Correct Note");
863
            SortNote(correctList);
864
            List<Note> endList = new List<Note>();
865
            if (correctList.Count > 0)
866
                endList.Add(correctList[0]);
867
            foreach (var item in correctList)
868
                try
869
                {
870
                    if (!endList.Contains(item))
871
                        NoteCorrectModeling(item, endList);
872
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
873
                }
874
                catch (Exception ex)
875
                {
876
                    Log.Write("Error in NoteModeling");
877
                    Log.Write("UID : " + item.UID);
878
                    Log.Write(ex.Message);
879
                    Log.Write(ex.StackTrace);
880
                }
881
        }
882
        private void RunTextModeling()
883
        {
884
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.TEXTINFOS.Count);
885
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
886
            SortText(document.TEXTINFOS);
887
            foreach (var item in document.TEXTINFOS)
888
                try
889
                {
890
                    if (item.ASSOCIATION)
891
                        AssociationTextModeling(item);
892
                    else
893
                        NormalTextModeling(item);
894
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
895
                }
896
                catch (Exception ex)
897
                {
898
                    Log.Write("Error in TextModeling");
899
                    Log.Write("UID : " + item.UID);
900
                    Log.Write(ex.Message);
901
                    Log.Write(ex.StackTrace);
902
                }
903
        }
904
        private void RunInputLineNumberAttribute()
905
        {
906
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINENUMBERS.Count);
907
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set LineNumbers Attribute");
908
            List<string> endLine = new List<string>();
909
            foreach (var item in document.LINENUMBERS)
910
                try
911
                {
912
                    InputLineNumberAttribute(item, endLine);
913
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
914
                }
915
                catch (Exception ex)
916
                {
917
                    Log.Write("Error in InputLineNumberAttribute");
918
                    Log.Write("UID : " + item.UID);
919
                    Log.Write(ex.Message);
920
                    Log.Write(ex.StackTrace);
921
                }
922
        }
923
        private void RunInputSymbolAttribute()
924
        {
925
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count + document.Equipments.Count + document.LINES.Count);
926
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
927
            foreach (var item in document.SYMBOLS)
928
                try
929
                {
930
                    InputSymbolAttribute(item, item.ATTRIBUTES);
931
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
932
                }
933
                catch (Exception ex)
934
                {
935
                    Log.Write("Error in InputSymbolAttribute");
936
                    Log.Write("UID : " + item.UID);
937
                    Log.Write(ex.Message);
938
                    Log.Write(ex.StackTrace);
939
                }
940

    
941
            foreach (var item in document.Equipments)
942
                try
943
                {
944
                    InputSymbolAttribute(item, item.ATTRIBUTES);
945
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
946
                }
947
                catch (Exception ex)
948
                {
949
                    Log.Write("Error in InputSymbolAttribute");
950
                    Log.Write("UID : " + item.UID);
951
                    Log.Write(ex.Message);
952
                    Log.Write(ex.StackTrace);
953
                }
954
            foreach (var item in document.LINES)
955
                try
956
                {
957
                    InputSymbolAttribute(item, item.ATTRIBUTES);
958
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
959
                }
960
                catch (Exception ex)
961
                {
962
                    Log.Write("Error in InputSymbolAttribute");
963
                    Log.Write("UID : " + item.UID);
964
                    Log.Write(ex.Message);
965
                    Log.Write(ex.StackTrace);
966
                }
967
        }
968
        private void RunInputSpecBreakAttribute()
969
        {
970
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SpecBreaks.Count);
971
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set SpecBreak Attribute");
972
            foreach (var item in document.SpecBreaks)
973
                try
974
                {
975
                    InputSpecBreakAttribute(item);
976
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
977
                }
978
                catch (Exception ex)
979
                {
980
                    Log.Write("Error in InputSpecBreakAttribute");
981
                    Log.Write("UID : " + item.UID);
982
                    Log.Write(ex.Message);
983
                    Log.Write(ex.StackTrace);
984
                }
985
        }
986
        private void RunInputEndBreakAttribute()
987
        {
988
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.EndBreaks.Count);
989
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set EndBreak Attribute");
990
            foreach (var item in document.EndBreaks)
991
                try
992
                {
993
                    InputEndBreakAttribute(item);
994
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
995
                }
996
                catch (Exception ex)
997
                {
998
                    Log.Write("Error in RunInputEndBreakAttribute");
999
                    Log.Write("UID : " + item.UID);
1000
                    Log.Write(ex.Message);
1001
                    Log.Write(ex.StackTrace);
1002
                }
1003
        }
1004
        private void RunLabelSymbolModeling()
1005
        {
1006
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
1007
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
1008
            foreach (var item in document.SYMBOLS)
1009
                try
1010
                {
1011
                    LabelSymbolModeling(item);
1012
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1013
                }
1014
                catch (Exception ex)
1015
                {
1016
                    Log.Write("Error in LabelSymbolModeling");
1017
                    Log.Write("UID : " + item.UID);
1018
                    Log.Write(ex.Message);
1019
                    Log.Write(ex.StackTrace);
1020
                }
1021

    
1022
        }
1023
        private void RunCorrectAssociationText()
1024
        {
1025
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.TEXTINFOS.Count + document.LINENUMBERS.Count);
1026
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Correct Labels");
1027
            List<Text> endTexts = new List<Text>();
1028
            foreach (var item in document.TEXTINFOS)
1029
            {
1030
                try
1031
                {
1032
                    if (item.ASSOCIATION && !endTexts.Contains(item))
1033
                        AssociationTextCorrectModeling(item, endTexts);
1034
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1035
                }
1036
                catch (Exception ex)
1037
                {
1038
                    Log.Write("Error in RunCorrectAssociationText");
1039
                    Log.Write("UID : " + item.UID);
1040
                    Log.Write(ex.Message);
1041
                    Log.Write(ex.StackTrace);
1042
                }
1043

    
1044
            }
1045

    
1046
            foreach (var item in document.LINENUMBERS)
1047
            {
1048
                try
1049
                {
1050
                    LineNumberCorrectModeling(item);
1051
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1052
                }
1053
                catch (Exception ex)
1054
                {
1055
                    Log.Write("Error in RunCorrectAssociationText");
1056
                    Log.Write("UID : " + item.UID);
1057
                    Log.Write(ex.Message);
1058
                    Log.Write(ex.StackTrace);
1059
                }
1060
            }
1061
        }
1062
        private void RunETC()
1063
        {
1064
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, FlowMarkRepIds.Count);
1065
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "ETC");
1066
            foreach (var item in FlowMarkRepIds)
1067
            {
1068
                LMLabelPersist label = dataSource.GetLabelPersist(item);
1069
                if (label != null)
1070
                {
1071
                    label.get_GraphicOID();
1072
                    DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[label.get_GraphicOID().ToString()] as DependencyObject;
1073
                    if (dependency != null)
1074
                        dependency.BringToFront();
1075
                }
1076
                ReleaseCOMObjects(label);
1077
                label = null;
1078
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1079
            }
1080
        }
1081
        private void RunBulkAttribute()
1082
        {
1083
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, 2);
1084
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Bulk Attribute");
1085

    
1086
            List<SPPIDModel.BulkAttribute> select = _ETCSetting.BulkAttributes.FindAll(x => x.RuleName.Equals(document.BulkAttributeName));
1087
            if (select.Count > 0)
1088
                SPPIDUtil.BulkAttribute(dataSource, select, BulkAttributeItemType.PipeRun);
1089
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1090
            if (select.Count > 0)
1091
                SPPIDUtil.BulkAttribute(dataSource, select, BulkAttributeItemType.Symbol);
1092
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1093
        }
1094
        /// <summary>
1095
        /// 도면 생성 메서드
1096
        /// </summary>
1097
        private bool CreateDocument(ref string drawingNumber, ref string drawingName)
1098
        {
1099
            Log.Write("------------------ Start create document ------------------");
1100
            GetDrawingNameAndNumber(ref drawingName, ref drawingNumber);
1101
            Log.Write("Drawing name : " + drawingName);
1102
            Log.Write("Drawing number : " + drawingNumber);
1103
            Thread.Sleep(1000);
1104
            try
1105
            {
1106
                newDrawing = application.Drawings.Add(document.Unit, document.Template, drawingNumber, drawingName);
1107
            }
1108
            catch (Exception ex)
1109
            {
1110
                if (ex.Message == "Invalid procedure call or argument")
1111
                {
1112
                    Exception newEx = new Exception(string.Format("Invalid Unit [0]", document.Unit), ex.InnerException);
1113
                    throw newEx;
1114
                }
1115
                else
1116
                {
1117
                    throw ex;
1118
                }
1119
            }
1120
            
1121
            if (newDrawing != null)
1122
            {
1123
                document.SPPID_DrawingNumber = drawingNumber;
1124
                document.SPPID_DrawingName = drawingName;
1125
                Thread.Sleep(1000);
1126
                radApp.ActiveWindow.Fit();
1127
                Thread.Sleep(1000);
1128
                radApp.ActiveWindow.Zoom = 2000;
1129
                Thread.Sleep(2000);
1130

    
1131
                //current LMDrawing 가져오기
1132
                LMAFilter filter = new LMAFilter();
1133
                LMACriterion criterion = new LMACriterion();
1134
                filter.ItemType = "Drawing";
1135
                criterion.SourceAttributeName = "Name";
1136
                criterion.Operator = "=";
1137
                criterion.set_ValueAttribute(drawingName);
1138
                filter.get_Criteria().Add(criterion);
1139

    
1140
                LMDrawings drawings = new LMDrawings();
1141
                drawings.Collect(dataSource, Filter: filter);
1142

    
1143
                // Input Drawing Attribute
1144
                LMDrawing drawing = ((dynamic)drawings).Nth(1);
1145
                if (drawing != null)
1146
                {
1147
                    using (DataTable drawingAttributeDT = Project_DB.SelectDrawingProjectAttribute())
1148
                    {
1149
                        foreach (DataRow row in drawingAttributeDT.Rows)
1150
                        {
1151
                            string mappingName = DBNull.Value.Equals(row["SPPID_ATTRIBUTE"]) ? string.Empty : row["SPPID_ATTRIBUTE"].ToString();
1152
                            if (!string.IsNullOrEmpty(mappingName))
1153
                            {
1154
                                string uid = row["UID"].ToString();
1155
                                string name = row["NAME"].ToString();
1156
                                Text text = document.TEXTINFOS.Find(x => x.AREA == uid);
1157
                                if (text != null)
1158
                                {
1159
                                    string value = text.VALUE;
1160
                                    LMAAttribute attribute = drawing.Attributes[mappingName];
1161
                                    if (attribute != null)
1162
                                        attribute.set_Value(value);
1163
                                    ReleaseCOMObjects(attribute);
1164
                                    document.TEXTINFOS.Remove(text);
1165
                                }
1166
                            }
1167
                        }
1168

    
1169
                        drawingAttributeDT.Dispose();
1170
                    }
1171

    
1172
                    ReleaseCOMObjects(drawing);
1173
                }
1174

    
1175
                drawingID = ((dynamic)drawings).Nth(1).Id;
1176
                ReleaseCOMObjects(filter);
1177
                ReleaseCOMObjects(criterion);
1178
                ReleaseCOMObjects(drawings);
1179
                filter = null;
1180
                criterion = null;
1181
                drawings = null;
1182
            }
1183
            else
1184
                Log.Write("Fail Create Drawing");
1185

    
1186
            if (newDrawing != null)
1187
            {
1188
                SetBorderFile();
1189
                return true;
1190
            }
1191
            else
1192
                return false;
1193
        }
1194

    
1195
        private void SetBorderFile()
1196
        {
1197
            ETCSetting setting = ETCSetting.GetInstance();
1198

    
1199
            if (!string.IsNullOrEmpty(setting.BorderFilePath) && System.IO.File.Exists(setting.BorderFilePath))
1200
            {
1201
                foreach (Ingr.RAD2D.SmartFrame2d smartFrame in radApp.ActiveDocument.ActiveSheet.SmartFrames2d)
1202
                {
1203
                    if (!string.IsNullOrEmpty(smartFrame.LinkMoniker) && smartFrame.LinkMoniker != setting.BorderFilePath)
1204
                    {
1205
                        smartFrame.ChangeSource(Ingr.RAD2D.OLEInsertionTypeConstant.igOLELinked, setting.BorderFilePath, true);
1206
                        smartFrame.Update();
1207
                    }
1208

    
1209
                }
1210
            }
1211
        }
1212

    
1213
        /// <summary>
1214
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
1215
        /// </summary>
1216
        /// <param name="drawingName"></param>
1217
        /// <param name="drawingNumber"></param>
1218
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
1219
        {
1220
            LMDrawings drawings = new LMDrawings();
1221
            drawings.Collect(dataSource);
1222

    
1223
            List<string> drawingNameList = new List<string>();
1224
            List<string> drawingNumberList = new List<string>();
1225

    
1226
            foreach (LMDrawing item in drawings)
1227
            {
1228
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
1229
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
1230
            }
1231

    
1232
            int nameLength = drawingName.Length;
1233
            while (drawingNameList.Contains(drawingName))
1234
            {
1235
                if (nameLength == drawingName.Length)
1236
                    drawingName += "-1";
1237
                else
1238
                {
1239
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
1240
                    drawingName = drawingName.Substring(0, nameLength + 1);
1241
                    drawingName += ++index;
1242
                }
1243
            }
1244

    
1245
            int numberLength = drawingNumber.Length;
1246
            while (drawingNameList.Contains(drawingNumber))
1247
            {
1248
                if (numberLength == drawingNumber.Length)
1249
                    drawingNumber += "-1";
1250
                else
1251
                {
1252
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
1253
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
1254
                    drawingNumber += ++index;
1255
                }
1256
            }
1257
            ReleaseCOMObjects(drawings);
1258
            drawings = null;
1259
        }
1260

    
1261
        /// <summary>
1262
        /// 도면 크기 구하는 메서드
1263
        /// </summary>
1264
        /// <returns></returns>
1265
        private bool DocumentCoordinateCorrection()
1266
        {
1267
            //if (radApp.ActiveDocument.ActiveSheet.SmartFrames2d.Count > 0)
1268
            //{
1269
            //    double x = 0;
1270
            //    double y = 0;
1271
            //    foreach (Ingr.RAD2D.SmartFrame2d smartFrame in radApp.ActiveDocument.ActiveSheet.SmartFrames2d)
1272
            //    {
1273
            //        x = Math.Max(smartFrame.CropRight, x);
1274
            //        y = Math.Max(smartFrame.CropTop, y);
1275
            //    }
1276
            //    document.SetSPPIDLocation(x, y);
1277
            //    document.CoordinateCorrection();
1278
            //    return true;
1279
            //}
1280
            //else
1281
            //{
1282
            //    Log.Write("Need Border!");
1283
            //    return false;
1284
            //}
1285

    
1286
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
1287
            {
1288
                Log.Write("Setting Drawing X, Drawing Y");
1289
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
1290
                Log.Write("Start coordinate correction");
1291
                document.CoordinateCorrection();
1292
                return true;
1293
            }
1294
            else
1295
            {
1296
                Log.Write("Need Drawing X, Y");
1297
                return false;
1298
            }
1299
        }
1300

    
1301
        /// <summary>
1302
        /// 심볼을 실제로 Modeling 메서드
1303
        /// </summary>
1304
        /// <param name="symbol">생성할 심볼</param>
1305
        /// <param name="targetSymbol">연결되어 있는 심볼</param>
1306
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
1307
        {
1308
            // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
1309
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
1310
            if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
1311
                return;
1312
            // 이미 모델링 됐을 경우
1313
            else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1314
                return;
1315

    
1316
            LMSymbol _LMSymbol = null;
1317

    
1318
            string mappingPath = symbol.SPPID.MAPPINGNAME;
1319
            double x = symbol.SPPID.ORIGINAL_X;
1320
            double y = symbol.SPPID.ORIGINAL_Y;
1321
            int mirror = 0;
1322
            double angle = symbol.ANGLE;
1323

    
1324
            // OPC 일경우 180도 일때 Mirror
1325
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
1326
                mirror = 1;
1327

    
1328
            // Mirror 계산
1329
            if (symbol.FLIP == 1)
1330
            {
1331
                mirror = 1;
1332
                if (angle == Math.PI || angle == 0)
1333
                    angle += Math.PI;
1334
            }
1335

    
1336
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
1337
            {
1338
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);   /// RepresentationId로 SPPID 심볼을 찾음
1339
                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
1340
                if (connector != null)
1341
                    GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
1342

    
1343
                LMConnector temp = LineModelingForSymbolZeroLength(symbol, _TargetItem, x, y);
1344
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
1345
                if (temp != null)
1346
                    _placement.PIDRemovePlacement(temp.AsLMRepresentation());
1347
                ReleaseCOMObjects(temp);
1348
                temp = null;
1349

    
1350
                if (_LMSymbol != null && _TargetItem != null)
1351
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1352

    
1353
                ReleaseCOMObjects(_TargetItem);
1354
            }
1355
            else
1356
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1357

    
1358
            if (_LMSymbol != null)
1359
            {
1360
                _LMSymbol.Commit();
1361

    
1362
                // ConnCheck
1363
                List<string> ids = new List<string>();
1364
                foreach (LMConnector item in _LMSymbol.Connect1Connectors)
1365
                {
1366
                    if (item.get_ItemStatus() == "Active" && !ids.Contains(item.Id))
1367
                        ids.Add(item.Id);
1368
                    ReleaseCOMObjects(item);
1369
                }
1370
                foreach (LMConnector item in _LMSymbol.Connect2Connectors)
1371
                {
1372
                    if (item.get_ItemStatus() == "Active" && !ids.Contains(item.Id))
1373
                        ids.Add(item.Id);
1374
                    ReleaseCOMObjects(item);
1375
                }
1376

    
1377
                int createdSymbolCount = document.SYMBOLS.FindAll(i => i.CONNECTORS.Find(j => j.CONNECTEDITEM == symbol.UID) != null && !string.IsNullOrEmpty(i.SPPID.RepresentationId)).Count;
1378
                if (targetSymbol == null && ids.Count != createdSymbolCount)
1379
                {
1380
                    double currentX = _LMSymbol.get_XCoordinate();
1381
                    double currentY = _LMSymbol.get_YCoordinate();
1382
                }
1383

    
1384
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1385
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
1386
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
1387

    
1388
                foreach (var item in symbol.ChildSymbols)
1389
                    CreateChildSymbol(item, _LMSymbol, symbol);
1390

    
1391
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
1392
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
1393

    
1394
                double[] range = null;
1395
                GetSPPIDSymbolRange(symbol, ref range);
1396
                symbol.SPPID.SPPID_Min_X = range[0];
1397
                symbol.SPPID.SPPID_Min_Y = range[1];
1398
                symbol.SPPID.SPPID_Max_X = range[2];
1399
                symbol.SPPID.SPPID_Max_Y = range[3];
1400

    
1401
                double xGap = symbol.SPPID.SPPID_X - symbol.SPPID.ORIGINAL_X;
1402
                double yGap = symbol.SPPID.SPPID_Y - symbol.SPPID.ORIGINAL_Y;
1403

    
1404
                List<Symbol> xGroupSymbols = symbol.SPPID.CorrectionX_GroupSymbols;
1405
                List<Symbol> yGroupSymbols = symbol.SPPID.CorrectionY_GroupSymbols;
1406
                xGroupSymbols.Sort(SPPIDUtil.SortSymbolPriority);
1407
                yGroupSymbols.Sort(SPPIDUtil.SortSymbolPriority);
1408
                int xDrawnCount = xGroupSymbols.Count(c => c.SPPID.RepresentationId != null);
1409
                int yDrawnCount = yGroupSymbols.Count(c => c.SPPID.RepresentationId != null);
1410

    
1411
                if (xDrawnCount == 1 || xDrawnCount == 2)
1412
                {
1413
                    for (int i = 0; i < xGroupSymbols.Count; i++)
1414
                    {
1415
                        var item = xGroupSymbols[i];
1416
                        if (!string.IsNullOrWhiteSpace(item.SPPID.RepresentationId)) continue;
1417

    
1418
                        if (xDrawnCount == 1)
1419
                        {                            
1420
                            item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
1421
                            item.SPPID.ORIGINAL_Y = item.SPPID.ORIGINAL_Y + yGap;
1422
                        }
1423
                        else if (xDrawnCount == 2)
1424
                        {
1425
                            if (item.SPPID.IsEqualSpacingY)
1426
                            {
1427
                                double ppValue = xGroupSymbols[i - 2].SPPID.SPPID_Y != 0 ? xGroupSymbols[i - 2].SPPID.SPPID_Y : xGroupSymbols[i - 2].SPPID.ORIGINAL_Y;
1428
                                double pValue = xGroupSymbols[i - 1].SPPID.SPPID_Y != 0 ? xGroupSymbols[i - 1].SPPID.SPPID_Y : xGroupSymbols[i - 1].SPPID.ORIGINAL_Y;
1429
                                double gap = pValue - ppValue;
1430
                                double value = pValue + gap;
1431
                                item.SPPID.ORIGINAL_Y = value;
1432
                            }
1433
                        }
1434
                    }
1435
                }
1436

    
1437
                if (yDrawnCount == 1 || yDrawnCount == 2)
1438
                {
1439
                    for (int i = 0; i < yGroupSymbols.Count; i++)
1440
                    {
1441
                        var item = yGroupSymbols[i];
1442
                        if (!string.IsNullOrWhiteSpace(item.SPPID.RepresentationId)) continue;
1443

    
1444
                        if (yDrawnCount == 1)
1445
                        {
1446
                            item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
1447
                            item.SPPID.ORIGINAL_X = item.SPPID.ORIGINAL_X + xGap;
1448
                        }
1449
                        else if (yDrawnCount == 2)
1450
                        {
1451
                            if (item.SPPID.IsEqualSpacingX)
1452
                            {
1453
                                double ppValue = yGroupSymbols[i - 2].SPPID.SPPID_X != 0 ? yGroupSymbols[i - 2].SPPID.SPPID_X : yGroupSymbols[i - 2].SPPID.ORIGINAL_X;
1454
                                double pValue = yGroupSymbols[i - 1].SPPID.SPPID_X != 0 ? yGroupSymbols[i - 1].SPPID.SPPID_X : yGroupSymbols[i - 1].SPPID.ORIGINAL_X;
1455
                                double gap = pValue - ppValue;
1456
                                double value = pValue + gap;
1457
                                item.SPPID.ORIGINAL_X = value;
1458
                            }
1459
                        }
1460
                    }
1461
                }
1462

    
1463
                ReleaseCOMObjects(_LMSymbol);
1464
            }
1465
        }
1466
        /// <summary>
1467
        /// targetX와 targetY 기준 제일 먼 PipingPoint에 TempLine Modeling
1468
        /// Signal Point는 고려하지 않음
1469
        /// </summary>
1470
        /// <param name="symbol"></param>
1471
        /// <param name="_TargetItem"></param>
1472
        /// <param name="targetX"></param>
1473
        /// <param name="targetY"></param>
1474
        /// <returns></returns>
1475
        private LMConnector LineModelingForSymbolZeroLength(Symbol symbol, LMSymbol _TargetItem, double targetX, double targetY)
1476
        {
1477
            LMConnector tempConnector = null;
1478

    
1479
            List<Symbol> group = new List<Symbol>();
1480
            SPPIDUtil.FindConnectedSymbolGroup(document, symbol, group);
1481
            if (group.FindAll(loopX => !string.IsNullOrEmpty(loopX.SPPID.RepresentationId)).Count == 1)
1482
            {
1483
                List<Connector> connectors = new List<Connector>();
1484
                foreach (var item in group)
1485
                    connectors.AddRange(item.CONNECTORS.FindAll(loopX => loopX.ConnectedObject != null && loopX.ConnectedObject.GetType() == typeof(Line)));
1486
                /// Primary or Secondary Type Line만 고려
1487
                Connector _connector = connectors.Find(loopX => loopX.ConnectedObject != null && loopX.ConnectedObject.GetType() == typeof(Line) &&
1488
                (((Line)loopX.ConnectedObject).TYPE == "Primary" || ((Line)loopX.ConnectedObject).TYPE == "Secondary"));
1489
                if (_connector != null)
1490
                {
1491
                    string sppidLine = ((Line)_connector.ConnectedObject).SPPID.MAPPINGNAME;
1492
                    List<double[]> pointInfos = getPipingPoints(_TargetItem);
1493
                    /// PipingPoint가 2개 이상만
1494
                    if (pointInfos.Count >= 2)
1495
                    {
1496
                        double lineX = 0;
1497
                        double lineY = 0;
1498
                        double length = 0;
1499
                        foreach (var item in pointInfos)
1500
                        {
1501
                            double tempX = item[1];
1502
                            double tempY = item[2];
1503

    
1504
                            double calcDistance = SPPIDUtil.CalcPointToPointdDistance(targetX, targetY, tempX, tempY);
1505
                            if (calcDistance > length)
1506
                            {
1507
                                lineX = tempX;
1508
                                lineY = tempY;
1509
                            }
1510
                        }
1511

    
1512
                        _LMAItem _LMAItem = _placement.PIDCreateItem(sppidLine);
1513
                        PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1514
                        placeRunInputs.AddPoint(-1, -1);
1515
                        placeRunInputs.AddSymbolTarget(_TargetItem, lineX, lineY);
1516
                        tempConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1517
                        if (tempConnector != null)
1518
                            tempConnector.Commit();
1519
                        ReleaseCOMObjects(_LMAItem);
1520
                        _LMAItem = null;
1521
                        ReleaseCOMObjects(placeRunInputs);
1522
                        placeRunInputs = null;
1523
                    }
1524
                }
1525
            }
1526

    
1527
            return tempConnector;
1528
        }
1529
        /// <summary>
1530
        /// Symbol의 PipingPoints를 구함
1531
        /// SignalPoint는 고려하지 않음
1532
        /// </summary>
1533
        /// <param name="symbol"></param>
1534
        /// <returns></returns>
1535
        private List<double[]> getPipingPoints(LMSymbol symbol)
1536
        {
1537
            LMModelItem modelItem = symbol.ModelItemObject;
1538
            LMPipingPoints pipingPoints = null;
1539
            if (modelItem.get_ItemTypeName() == "PipingComp")
1540
            {
1541
                LMPipingComp pipingComp = dataSource.GetPipingComp(modelItem.Id);
1542
                pipingPoints = pipingComp.PipingPoints;
1543
                ReleaseCOMObjects(pipingComp);
1544
                pipingComp = null;
1545
            }
1546
            else if (modelItem.get_ItemTypeName() == "Instrument")
1547
            {
1548
                LMInstrument instrument = dataSource.GetInstrument(modelItem.Id);
1549
                pipingPoints = instrument.PipingPoints;
1550
                ReleaseCOMObjects(instrument);
1551
                instrument = null;
1552
            }
1553
            else
1554
                Log.Write("다른 Type");
1555

    
1556
            List<double[]> info = new List<double[]>();
1557
            if (pipingPoints != null)
1558
            {
1559
                foreach (LMPipingPoint pipingPoint in pipingPoints)
1560
                {
1561
                    foreach (LMAAttribute attribute in pipingPoint.Attributes)
1562
                    {
1563
                        if (attribute.Name == "PipingPointNumber")
1564
                        {
1565
                            int index = Convert.ToInt32(attribute.get_Value());
1566
                            if (info.Find(loopX => loopX[0] == index) == null)
1567
                            {
1568
                                double x = 0;
1569
                                double y = 0;
1570
                                if (_placement.PIDConnectPointLocation(symbol, index, ref x, ref y))
1571
                                    info.Add(new double[] { index, x, y });
1572
                            }
1573
                        }
1574
                    }
1575
                }
1576
            }
1577
            ReleaseCOMObjects(modelItem);
1578
            modelItem = null;
1579
            ReleaseCOMObjects(pipingPoints);
1580
            pipingPoints = null;
1581

    
1582
            return info;
1583
        }
1584

    
1585
        private void RemoveSymbol(Symbol symbol)
1586
        {
1587
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1588
            {
1589
                LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1590
                if (_LMSymbol != null)
1591
                {
1592
                    _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
1593
                    ReleaseCOMObjects(_LMSymbol);
1594
                }
1595
            }
1596

    
1597
            symbol.SPPID.RepresentationId = string.Empty;
1598
            symbol.SPPID.ModelItemID = string.Empty;
1599
            symbol.SPPID.SPPID_X = double.NaN;
1600
            symbol.SPPID.SPPID_Y = double.NaN;
1601
            symbol.SPPID.SPPID_Min_X = double.NaN;
1602
            symbol.SPPID.SPPID_Min_Y = double.NaN;
1603
            symbol.SPPID.SPPID_Max_X = double.NaN;
1604
            symbol.SPPID.SPPID_Max_Y = double.NaN;
1605
        }
1606

    
1607
        private void RemoveSymbol(List<Symbol> symbols)
1608
        {
1609
            foreach (var symbol in symbols)
1610
            {
1611
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1612
                {
1613
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1614
                    if (_LMSymbol != null)
1615
                    {
1616
                        _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
1617
                        ReleaseCOMObjects(_LMSymbol);
1618
                    }
1619
                }
1620

    
1621
                symbol.SPPID.RepresentationId = string.Empty;
1622
                symbol.SPPID.ModelItemID = string.Empty;
1623
                symbol.SPPID.SPPID_X = double.NaN;
1624
                symbol.SPPID.SPPID_Y = double.NaN;
1625
                symbol.SPPID.SPPID_Min_X = double.NaN;
1626
                symbol.SPPID.SPPID_Min_Y = double.NaN;
1627
                symbol.SPPID.SPPID_Max_X = double.NaN;
1628
                symbol.SPPID.SPPID_Max_Y = double.NaN;
1629
            }
1630
        }
1631

    
1632
        /// <summary>
1633
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
1634
        /// </summary>
1635
        /// <param name="targetConnector"></param>
1636
        /// <param name="targetSymbol"></param>
1637
        /// <param name="x"></param>
1638
        /// <param name="y"></param>
1639
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
1640
        {
1641
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
1642

    
1643
            double[] range = null;
1644
            List<double[]> points = new List<double[]>();
1645
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
1646
            double x1 = range[0];
1647
            double y1 = range[1];
1648
            double x2 = range[2];
1649
            double y2 = range[3];
1650

    
1651
            // Origin 기준 Connector의 위치차이
1652
            double sceneX = 0;
1653
            double sceneY = 0;
1654
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
1655
            double originX = 0;
1656
            double originY = 0;
1657
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
1658
            double gapX = originX - sceneX;
1659
            double gapY = originY - sceneY;
1660

    
1661
            // SPPID Symbol과 ID2 심볼의 크기 차이
1662
            double sizeWidth = 0;
1663
            double sizeHeight = 0;
1664
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
1665
            if (sizeWidth == 0 || sizeHeight == 0)
1666
                throw new Exception("Check symbol size! \r\nUID : " + targetSymbol.UID);
1667

    
1668
            double percentX = (x2 - x1) / sizeWidth;
1669
            double percentY = (y2 - y1) / sizeHeight;
1670

    
1671
            double SPPIDgapX = gapX * percentX;
1672
            double SPPIDgapY = gapY * percentY;
1673

    
1674
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
1675
            double distance = double.MaxValue;
1676
            double[] resultPoint;
1677
            foreach (var point in points)
1678
            {
1679
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
1680
                if (distance > result)
1681
                {
1682
                    distance = result;
1683
                    resultPoint = point;
1684
                    x = point[0];
1685
                    y = point[1];
1686
                }
1687
            }
1688

    
1689
            ReleaseCOMObjects(_TargetItem);
1690
        }
1691

    
1692
        private void GetTargetLineConnectorPoint(Connector targetConnector, Line targetLine, ref double x, ref double y)
1693
        {
1694
            int index = targetLine.CONNECTORS.IndexOf(targetConnector);
1695
            if (index == 0)
1696
            {
1697
                x = targetLine.SPPID.START_X;
1698
                y = targetLine.SPPID.START_Y;
1699
            }
1700
            else
1701
            {
1702
                x = targetLine.SPPID.END_X;
1703
                y = targetLine.SPPID.END_Y;
1704
            }
1705
        }
1706

    
1707
        /// <summary>
1708
        /// SPPID Symbol의 Range를 구한다.
1709
        /// </summary>
1710
        /// <param name="symbol"></param>
1711
        /// <param name="range"></param>
1712
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
1713
        {
1714
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1715
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1716
            double x1 = 0;
1717
            double y1 = 0;
1718
            double x2 = 0;
1719
            double y2 = 0;
1720
            symbol2d.Range(out x1, out y1, out x2, out y2);
1721
            range = new double[] { x1, y1, x2, y2 };
1722

    
1723
            for (int i = 1; i < 30; i++)
1724
            {
1725
                double connX = 0;
1726
                double connY = 0;
1727
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
1728
                    points.Add(new double[] { connX, connY });
1729
            }
1730

    
1731
            foreach (var childSymbol in symbol.ChildSymbols)
1732
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
1733

    
1734
            ReleaseCOMObjects(_TargetItem);
1735
        }
1736

    
1737
        private void GetSPPIDSymbolRange(Symbol symbol, ref double[] range, bool bOnlySymbol = false, bool bForGraphic = false)
1738
        {
1739
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1740
            if (_TargetItem != null)
1741
            {
1742
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1743
                double x1 = 0;
1744
                double y1 = 0;
1745
                double x2 = 0;
1746
                double y2 = 0;
1747
                if (!bForGraphic)
1748
                {
1749
                    symbol2d.Range(out x1, out y1, out x2, out y2);
1750
                    range = new double[] { x1, y1, x2, y2 };
1751
                }
1752
                else
1753
                {
1754
                    x1 = double.MaxValue;
1755
                    y1 = double.MaxValue;
1756
                    x2 = double.MinValue;
1757
                    y2 = double.MinValue;
1758
                    range = new double[] { x1, y1, x2, y2 };
1759

    
1760
                    foreach (var item in symbol2d.DrawingObjects)
1761
                    {
1762
                        if (item.GetType() == typeof(Ingr.RAD2D.Line2d))
1763
                        {
1764
                            Ingr.RAD2D.Line2d rangeObject = item as Ingr.RAD2D.Line2d;
1765
                            if (rangeObject.Layer == "Default")
1766
                            {
1767
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1768
                                range = new double[] {
1769
                                Math.Min(x1, range[0]),
1770
                                Math.Min(y1, range[1]),
1771
                                Math.Max(x2, range[2]),
1772
                                Math.Max(y2, range[3])
1773
                            };
1774
                            }
1775
                        }
1776
                        else if (item.GetType() == typeof(Ingr.RAD2D.Circle2d))
1777
                        {
1778
                            Ingr.RAD2D.Circle2d rangeObject = item as Ingr.RAD2D.Circle2d;
1779
                            if (rangeObject.Layer == "Default")
1780
                            {
1781
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1782
                                range = new double[] {
1783
                                Math.Min(x1, range[0]),
1784
                                Math.Min(y1, range[1]),
1785
                                Math.Max(x2, range[2]),
1786
                                Math.Max(y2, range[3])
1787
                            };
1788
                            }
1789
                        }
1790
                        else if (item.GetType() == typeof(Ingr.RAD2D.Rectangle2d))
1791
                        {
1792
                            Ingr.RAD2D.Rectangle2d rangeObject = item as Ingr.RAD2D.Rectangle2d;
1793
                            if (rangeObject.Layer == "Default")
1794
                            {
1795
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1796
                                range = new double[] {
1797
                                Math.Min(x1, range[0]),
1798
                                Math.Min(y1, range[1]),
1799
                                Math.Max(x2, range[2]),
1800
                                Math.Max(y2, range[3])
1801
                            };
1802
                            }
1803
                        }
1804
                        else if (item.GetType() == typeof(Ingr.RAD2D.Arc2d))
1805
                        {
1806
                            Ingr.RAD2D.Arc2d rangeObject = item as Ingr.RAD2D.Arc2d;
1807
                            if (rangeObject.Layer == "Default")
1808
                            {
1809
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1810
                                range = new double[] {
1811
                                Math.Min(x1, range[0]),
1812
                                Math.Min(y1, range[1]),
1813
                                Math.Max(x2, range[2]),
1814
                                Math.Max(y2, range[3])
1815
                            };
1816
                            }
1817
                        }
1818
                    }
1819
                }
1820

    
1821
                if (!bOnlySymbol)
1822
                {
1823
                    foreach (var childSymbol in symbol.ChildSymbols)
1824
                        GetSPPIDChildSymbolRange(childSymbol, ref range);
1825
                }
1826
                ReleaseCOMObjects(_TargetItem);
1827
            }
1828
        }
1829

    
1830
        private void GetSPPIDSymbolRange(LMLabelPersist labelPersist, ref double[] range)
1831
        {
1832
            if (labelPersist != null)
1833
            {
1834
                double x1 = double.MaxValue;
1835
                double y1 = double.MaxValue;
1836
                double x2 = double.MinValue;
1837
                double y2 = double.MinValue;
1838
                range = new double[] { x1, y1, x2, y2 };
1839

    
1840
                Ingr.RAD2D.DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[labelPersist.get_GraphicOID().ToString()] as DependencyObject;
1841
                foreach (var item in dependency.DrawingObjects)
1842
                {
1843
                    Ingr.RAD2D.TextBox textBox = item as Ingr.RAD2D.TextBox;
1844
                    if (textBox != null)
1845
                    {
1846
                        if (dependency != null)
1847
                        {
1848
                            double tempX1;
1849
                            double tempY1;
1850
                            double tempX2;
1851
                            double tempY2;
1852
                            textBox.Range(out tempX1, out tempY1, out tempX2, out tempY2);
1853
                            x1 = Math.Min(x1, tempX1);
1854
                            y1 = Math.Min(y1, tempY1);
1855
                            x2 = Math.Max(x2, tempX2);
1856
                            y2 = Math.Max(y2, tempY2);
1857

    
1858
                            range = new double[] { x1, y1, x2, y2 };
1859
                        }
1860
                    }
1861
                }
1862

    
1863
            }
1864
        }
1865

    
1866
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range, bool bOnlySymbol = false, bool bForGraphic = false)
1867
        {
1868
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
1869
            foreach (var symbol in symbols)
1870
            {
1871
                double[] symbolRange = null;
1872
                GetSPPIDSymbolRange(symbol, ref symbolRange, bOnlySymbol, bForGraphic);
1873

    
1874
                tempRange[0] = Math.Min(tempRange[0], symbolRange[0]);
1875
                tempRange[1] = Math.Min(tempRange[1], symbolRange[1]);
1876
                tempRange[2] = Math.Max(tempRange[2], symbolRange[2]);
1877
                tempRange[3] = Math.Max(tempRange[3], symbolRange[3]);
1878

    
1879
                foreach (var childSymbol in symbol.ChildSymbols)
1880
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
1881
            }
1882

    
1883
            range = tempRange;
1884
        }
1885

    
1886
        /// <summary>
1887
        /// Child Modeling 된 Symbol의 Range를 구한다.
1888
        /// </summary>
1889
        /// <param name="childSymbol"></param>
1890
        /// <param name="range"></param>
1891
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
1892
        {
1893
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1894
            if (_ChildSymbol != null)
1895
            {
1896
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1897
                double x1 = 0;
1898
                double y1 = 0;
1899
                double x2 = 0;
1900
                double y2 = 0;
1901
                symbol2d.Range(out x1, out y1, out x2, out y2);
1902
                range[0] = Math.Min(range[0], x1);
1903
                range[1] = Math.Min(range[1], y1);
1904
                range[2] = Math.Max(range[2], x2);
1905
                range[3] = Math.Max(range[3], y2);
1906

    
1907
                for (int i = 1; i < int.MaxValue; i++)
1908
                {
1909
                    double connX = 0;
1910
                    double connY = 0;
1911
                    if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
1912
                        points.Add(new double[] { connX, connY });
1913
                    else
1914
                        break;
1915
                }
1916

    
1917
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1918
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
1919

    
1920
                ReleaseCOMObjects(_ChildSymbol);
1921
            }
1922
        }
1923

    
1924
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range)
1925
        {
1926
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1927
            if (_ChildSymbol != null)
1928
            {
1929
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1930
                double x1 = 0;
1931
                double y1 = 0;
1932
                double x2 = 0;
1933
                double y2 = 0;
1934
                symbol2d.Range(out x1, out y1, out x2, out y2);
1935
                range[0] = Math.Min(range[0], x1);
1936
                range[1] = Math.Min(range[1], y1);
1937
                range[2] = Math.Max(range[2], x2);
1938
                range[3] = Math.Max(range[3], y2);
1939

    
1940
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1941
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
1942
                ReleaseCOMObjects(_ChildSymbol);
1943
            }
1944
        }
1945

    
1946
        /// <summary>
1947
        /// Label Symbol Modeling
1948
        /// </summary>
1949
        /// <param name="symbol"></param>
1950
        private void LabelSymbolModeling(Symbol symbol)
1951
        {
1952
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1953
            {
1954
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
1955
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
1956
                    return;
1957
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
1958

    
1959
                string symbolUID = itemAttribute.VALUE;
1960
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
1961
                if (targetItem != null &&
1962
                    (targetItem.GetType() == typeof(Symbol) ||
1963
                    targetItem.GetType() == typeof(Equipment)))
1964
                {
1965
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
1966
                    string sRep = null;
1967
                    if (targetItem.GetType() == typeof(Symbol))
1968
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
1969
                    else if (targetItem.GetType() == typeof(Equipment))
1970
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
1971
                    if (!string.IsNullOrEmpty(sRep))
1972
                    {
1973
                        // LEADER Line 검사
1974
                        bool leaderLine = false;
1975
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1976
                        if (symbolMapping != null)
1977
                            leaderLine = symbolMapping.LEADERLINE;
1978

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

    
1983
                        //Leader 선 센터로
1984
                        if (_LMLabelPresist != null)
1985
                        {
1986
                            // Target Item에 Label의 Attribute Input
1987
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1988

    
1989
                            string OID = _LMLabelPresist.get_GraphicOID().ToString();
1990
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
1991
                            if (dependency != null)
1992
                            {
1993
                                bool result = false;
1994
                                foreach (var attributes in dependency.AttributeSets)
1995
                                {
1996
                                    foreach (var attribute in attributes)
1997
                                    {
1998
                                        string name = attribute.Name;
1999
                                        string value = attribute.GetValue().ToString();
2000
                                        if (name == "DrawingItemType" && value == "LabelPersist")
2001
                                        {
2002
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
2003
                                            {
2004
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
2005
                                                {
2006
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
2007
                                                    double prevX = _TargetItem.get_XCoordinate();
2008
                                                    double prevY = _TargetItem.get_YCoordinate();
2009
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
2010
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
2011
                                                    result = true;
2012
                                                    break;
2013
                                                }
2014
                                            }
2015
                                        }
2016

    
2017
                                        if (result)
2018
                                            break;
2019
                                    }
2020

    
2021
                                    if (result)
2022
                                        break;
2023
                                }
2024
                            }
2025

    
2026
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
2027
                            _LMLabelPresist.Commit();
2028
                            ReleaseCOMObjects(_LMLabelPresist);
2029
                        }
2030

    
2031
                        ReleaseCOMObjects(_TargetItem);
2032
                    }
2033
                }
2034
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
2035
                {
2036
                    Line targetLine = targetItem as Line;
2037
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
2038
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
2039
                    if (connectedLMConnector != null)
2040
                    {
2041
                        // Target Item에 Label의 Attribute Input
2042
                        InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
2043

    
2044
                        // LEADER Line 검사
2045
                        bool leaderLine = false;
2046
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
2047
                        if (symbolMapping != null)
2048
                            leaderLine = symbolMapping.LEADERLINE;
2049

    
2050
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: symbol.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
2051
                        if (_LMLabelPresist != null)
2052
                        {
2053
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
2054
                            _LMLabelPresist.Commit();
2055
                            ReleaseCOMObjects(_LMLabelPresist);
2056
                        }
2057
                        ReleaseCOMObjects(connectedLMConnector);
2058
                    }
2059

    
2060
                    foreach (var item in connectorVertices)
2061
                        if (item.Key != null)
2062
                            ReleaseCOMObjects(item.Key);
2063
                }
2064
            }
2065
        }
2066

    
2067
        /// <summary>
2068
        /// Equipment를 실제로 Modeling 메서드
2069
        /// </summary>
2070
        /// <param name="equipment"></param>
2071
        private void EquipmentModeling(Equipment equipment)
2072
        {
2073
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
2074
                return;
2075

    
2076
            LMSymbol _LMSymbol = null;
2077
            LMSymbol targetItem = null;
2078
            string mappingPath = equipment.SPPID.MAPPINGNAME;
2079
            double x = equipment.SPPID.ORIGINAL_X;
2080
            double y = equipment.SPPID.ORIGINAL_Y;
2081
            int mirror = 0;
2082
            double angle = equipment.ANGLE;
2083

    
2084
            // Mirror 계산
2085
            if (equipment.FLIP == 1)
2086
            {
2087
                mirror = 1;
2088
                if (angle == Math.PI || angle == 0)
2089
                    angle += Math.PI;
2090
            }
2091

    
2092
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
2093

    
2094
            Connector connector = equipment.CONNECTORS.Find(conn =>
2095
            !string.IsNullOrEmpty(conn.CONNECTEDITEM) &&
2096
            conn.CONNECTEDITEM != "None" &&
2097
            conn.ConnectedObject.GetType() == typeof(Equipment));
2098

    
2099
            if (connector != null)
2100
            {
2101
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
2102
                VendorPackage connVendorPackage = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as VendorPackage;
2103
                if (connEquipment != null)
2104
                {
2105
                    //if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
2106
                    //    EquipmentModeling(connEquipment);
2107

    
2108
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
2109
                    {
2110
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
2111
                        if (targetItem != null)
2112
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
2113
                        else
2114
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2115
                    }
2116
                    else
2117
                    {
2118
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2119
                    }
2120
                }
2121
                else if (connVendorPackage != null)
2122
                {
2123
                    if (!string.IsNullOrEmpty(connVendorPackage.SPPID.RepresentationId))
2124
                    {
2125
                        targetItem = dataSource.GetSymbol(connVendorPackage.SPPID.RepresentationId);
2126
                        if (targetItem != null)
2127
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
2128
                        else
2129
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2130
                    }
2131
                }
2132
                else
2133
                {
2134
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2135
                }
2136
            }
2137
            else
2138
            {
2139
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2140
            }
2141

    
2142
            if (_LMSymbol != null)
2143
            {
2144
                _LMSymbol.Commit();
2145
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
2146
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
2147
                ReleaseCOMObjects(_LMSymbol);
2148
            }
2149

    
2150
            if (targetItem != null)
2151
            {
2152
                ReleaseCOMObjects(targetItem);
2153
            }
2154

    
2155
            ReleaseCOMObjects(_LMSymbol);
2156
        }
2157

    
2158
        private void VendorPackageModeling(VendorPackage vendorPackage)
2159
        {
2160
            ETCSetting setting = ETCSetting.GetInstance();
2161
            if (!string.IsNullOrEmpty(setting.VendorPackageSymbolPath))
2162
            {
2163
                string symbolPath = setting.VendorPackageSymbolPath;
2164
                double x = vendorPackage.SPPID.ORIGINAL_X;
2165
                double y = vendorPackage.SPPID.ORIGINAL_Y;
2166

    
2167
                LMSymbol symbol = _placement.PIDPlaceSymbol(symbolPath, x, y);
2168
                if (symbol != null)
2169
                {
2170
                    symbol.Commit();
2171
                    vendorPackage.SPPID.RepresentationId = symbol.AsLMRepresentation().Id;
2172
                }
2173

    
2174
                ReleaseCOMObjects(symbol);
2175
                symbol = null;
2176
            }
2177
        }
2178

    
2179
        /// <summary>
2180
        /// 첫 진입점
2181
        /// </summary>
2182
        /// <param name="symbol"></param>
2183
        private void SymbolModelingBySymbol(Symbol symbol)
2184
        {
2185
            SymbolModeling(symbol, null);   /// 심볼을 생성한다
2186
            List<object> endObjects = new List<object>();
2187
            endObjects.Add(symbol);
2188

    
2189
            // 심볼에 연결되어 있는 항목들을 모델링한다
2190
            foreach (var connector in symbol.CONNECTORS)
2191
            {
2192
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
2193
                if (connItem != null && connItem.GetType() != typeof(Equipment))
2194
                {
2195
                    endObjects.Add(connItem);
2196
                    if (connItem.GetType() == typeof(Symbol))
2197
                    {
2198
                        Symbol connSymbol = connItem as Symbol;
2199
                        if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
2200
                        {
2201
                            SymbolModeling(connSymbol, symbol);
2202
                        }
2203
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
2204
                        SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
2205
                    }
2206
                    else if (connItem.GetType() == typeof(Line))
2207
                    {
2208
                        Line connLine = connItem as Line;
2209
                        SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
2210
                    }
2211
                }
2212
            }
2213
        }
2214

    
2215
        private void SymbolModelingByNeerSymbolLoop(Symbol symbol, List<object> endObjects)
2216
        {
2217
            foreach (var connector in symbol.CONNECTORS)
2218
            {
2219
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
2220
                if (connItem != null && connItem.GetType() != typeof(Equipment))
2221
                {
2222
                    if (!endObjects.Contains(connItem))
2223
                    {
2224
                        endObjects.Add(connItem);
2225
                        if (connItem.GetType() == typeof(Symbol))
2226
                        {
2227
                            Symbol connSymbol = connItem as Symbol;
2228
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
2229
                            {
2230
                                SymbolModeling(connSymbol, symbol);
2231
                            }
2232
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
2233
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
2234
                        }
2235
                        else if (connItem.GetType() == typeof(Line))
2236
                        {
2237
                            Line connLine = connItem as Line;
2238
                            SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
2239
                        }
2240
                    }
2241
                }
2242
            }
2243
        }
2244

    
2245
        private void SymbolModelingByNeerLineLoop(Line line, List<object> endObjects, Symbol prevSymbol)
2246
        {
2247
            foreach (var connector in line.CONNECTORS)
2248
            {
2249
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
2250
                if (connItem != null && connItem.GetType() != typeof(Equipment))
2251
                {
2252
                    if (!endObjects.Contains(connItem))
2253
                    {
2254
                        endObjects.Add(connItem);
2255
                        if (connItem.GetType() == typeof(Symbol))
2256
                        {
2257
                            Symbol connSymbol = connItem as Symbol;
2258
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
2259
                            {
2260
                                Line connLine = SPPIDUtil.GetConnectedLine(prevSymbol, connSymbol);
2261
                                int branchCount = 0;
2262
                                if (connLine != null)
2263
                                    branchCount = SPPIDUtil.GetBranchLineCount(document, connLine);
2264

    
2265
                                List<Symbol> group = new List<Symbol>();
2266
                                SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
2267
                                Symbol priority = prioritySymbols.Find(x => group.Contains(x));
2268
                                List<Symbol> endModelingGroup = new List<Symbol>();
2269
                                if (priority != null)
2270
                                {
2271
                                    SymbolGroupModeling(priority, group);
2272

    
2273
                                    // Range 겹치는지 확인해야함
2274
                                    double[] prevRange = null;
2275
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange, bForGraphic: true);
2276
                                    double[] groupRange = null;
2277
                                    GetSPPIDSymbolRange(group, ref groupRange, bForGraphic: true);
2278

    
2279
                                    double distanceX = 0;
2280
                                    double distanceY = 0;
2281
                                    bool overlapX = false;
2282
                                    bool overlapY = false;
2283
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
2284
                                    SPPIDUtil.CalcOverlap(prevRange, groupRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
2285
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
2286
                                        (slopeType == SlopeType.VERTICAL && overlapY))
2287
                                    {
2288
                                        RemoveSymbol(group);
2289
                                        foreach (var _temp in group)
2290
                                            SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, distanceX, distanceY);
2291

    
2292
                                        SymbolGroupModeling(priority, group);
2293
                                    }
2294
                                    else if (branchCount > 0)
2295
                                    {
2296
                                        LMConnector _connector = JustLineModeling(connLine);
2297
                                        if (_connector != null)
2298
                                        {
2299
                                            double distance = GetConnectorDistance(_connector);
2300
                                            int cellCount = (int)Math.Truncate(distance / GridSetting.GetInstance().Length);
2301
                                            _placement.PIDRemovePlacement(_connector.AsLMRepresentation());
2302
                                            _connector.Commit();
2303
                                            ReleaseCOMObjects(_connector);
2304
                                            _connector = null;
2305
                                            if (cellCount < branchCount + 1)
2306
                                            {
2307
                                                int moveCount = branchCount - cellCount;
2308
                                                RemoveSymbol(group);
2309
                                                foreach (var _temp in group)
2310
                                                    SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, moveCount * GridSetting.GetInstance().Length, moveCount * GridSetting.GetInstance().Length);
2311

    
2312
                                                SymbolGroupModeling(priority, group);
2313
                                            }
2314
                                        }
2315
                                    }
2316
                                }
2317
                                else
2318
                                {
2319
                                    SymbolModeling(connSymbol, null);
2320
                                    // Range 겹치는지 확인해야함
2321
                                    double[] prevRange = null;
2322
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange, bForGraphic: true);
2323
                                    double[] connRange = null;
2324
                                    GetSPPIDSymbolRange(connSymbol, ref connRange, bForGraphic: true);
2325

    
2326
                                    double distanceX = 0;
2327
                                    double distanceY = 0;
2328
                                    bool overlapX = false;
2329
                                    bool overlapY = false;
2330
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
2331
                                    SPPIDUtil.CalcOverlap(prevRange, connRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
2332
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
2333
                                        (slopeType == SlopeType.VERTICAL && overlapY))
2334
                                    {
2335
                                        RemoveSymbol(connSymbol);
2336
                                        SPPIDUtil.CalcNewCoordinateForSymbol(connSymbol, prevSymbol, distanceX, distanceY);
2337

    
2338
                                        SymbolModeling(connSymbol, null);
2339
                                    }
2340
                                    else if (branchCount > 0)
2341
                                    {
2342
                                        LMConnector _connector = JustLineModeling(connLine);
2343
                                        if (_connector != null)
2344
                                        {
2345
                                            double distance = GetConnectorDistance(_connector);
2346
                                            int cellCount = (int)Math.Truncate(distance / GridSetting.GetInstance().Length);
2347
                                            _placement.PIDRemovePlacement(_connector.AsLMRepresentation());
2348
                                            _connector.Commit();
2349
                                            ReleaseCOMObjects(_connector);
2350
                                            _connector = null;
2351
                                            if (cellCount < branchCount + 1)
2352
                                            {
2353
                                                int moveCount = branchCount - cellCount;
2354
                                                RemoveSymbol(group);
2355
                                                foreach (var _temp in group)
2356
                                                    SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, moveCount * GridSetting.GetInstance().Length, moveCount * GridSetting.GetInstance().Length);
2357

    
2358
                                                SymbolGroupModeling(priority, group);
2359
                                            }
2360
                                        }
2361
                                    }
2362
                                }
2363
                            }
2364
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
2365
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
2366
                        }
2367
                        else if (connItem.GetType() == typeof(Line))
2368
                        {
2369
                            Line connLine = connItem as Line;
2370
                            if (!SPPIDUtil.IsBranchLine(connLine, line))
2371
                                SymbolModelingByNeerLineLoop(connLine, endObjects, prevSymbol);
2372
                        }
2373
                    }
2374
                }
2375
            }
2376
        }
2377

    
2378
        private void SymbolGroupModeling(Symbol firstSymbol, List<Symbol> group)
2379
        {
2380
            List<Symbol> endModelingGroup = new List<Symbol>();
2381
            SymbolModeling(firstSymbol, null);
2382
            endModelingGroup.Add(firstSymbol);
2383
            while (endModelingGroup.Count != group.Count)
2384
            {
2385
                foreach (var _symbol in group)
2386
                {
2387
                    if (!endModelingGroup.Contains(_symbol))
2388
                    {
2389
                        foreach (var _connector in _symbol.CONNECTORS)
2390
                        {
2391
                            Symbol _connSymbol = SPPIDUtil.FindObjectByUID(document, _connector.CONNECTEDITEM) as Symbol;
2392
                            if (_connSymbol != null && endModelingGroup.Contains(_connSymbol))
2393
                            {
2394
                                SymbolModeling(_symbol, _connSymbol);
2395
                                endModelingGroup.Add(_symbol);
2396
                                break;
2397
                            }
2398
                        }
2399
                    }
2400
                }
2401
            }
2402
        }
2403

    
2404
        /// <summary>
2405
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
2406
        /// </summary>
2407
        /// <param name="childSymbol"></param>
2408
        /// <param name="parentSymbol"></param>
2409
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol, Symbol parent)
2410
        {
2411
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
2412
            double x1 = 0;
2413
            double x2 = 0;
2414
            double y1 = 0;
2415
            double y2 = 0;
2416
            symbol2d.Range(out x1, out y1, out x2, out y2);
2417

    
2418
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
2419
            if (_LMSymbol != null)
2420
            {
2421
                _LMSymbol.Commit();
2422
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
2423
                foreach (var item in childSymbol.ChildSymbols)
2424
                    CreateChildSymbol(item, _LMSymbol, parent);
2425
            }
2426

    
2427

    
2428
            ReleaseCOMObjects(_LMSymbol);
2429
        }
2430
        double index = 0;
2431
        private void NewLineModeling(Line line, bool isBranchModeling = false)
2432
        {
2433
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) || (BranchLines.Contains(line) && !isBranchModeling))
2434
                return;
2435

    
2436
            List<Line> group = new List<Line>();
2437
            GetConnectedLineGroup(line, group);
2438
            LineCoordinateCorrection(group);
2439

    
2440
            foreach (var groupLine in group)
2441
            {
2442
                if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
2443
                {
2444
                    BranchLines.Add(groupLine);
2445
                    continue;
2446
                }
2447

    
2448
                bool diagonal = false;
2449
                if (groupLine.SlopeType != SlopeType.HORIZONTAL && groupLine.SlopeType != SlopeType.VERTICAL)
2450
                    diagonal = true;
2451
                _LMAItem _LMAItem = _placement.PIDCreateItem(groupLine.SPPID.MAPPINGNAME);
2452
                LMSymbol _LMSymbolStart = null;
2453
                LMSymbol _LMSymbolEnd = null;
2454
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2455
                foreach (var connector in groupLine.CONNECTORS)
2456
                {
2457
                    double x = 0;
2458
                    double y = 0;
2459
                    GetTargetLineConnectorPoint(connector, groupLine, ref x, ref y);
2460
                    if (connector.ConnectedObject == null)
2461
                        placeRunInputs.AddPoint(x, y);
2462
                    else if (connector.ConnectedObject.GetType() == typeof(Symbol))
2463
                    {
2464
                        Symbol targetSymbol = connector.ConnectedObject as Symbol;
2465
                        GetTargetSymbolConnectorPoint(targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == groupLine), targetSymbol, ref x, ref y);
2466
                        if (groupLine.CONNECTORS.IndexOf(connector) == 0)
2467
                        {
2468
                            _LMSymbolStart = GetTargetSymbol(targetSymbol, groupLine);
2469
                            if (_LMSymbolStart != null)
2470
                                placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y, diagonal);
2471
                            else
2472
                                placeRunInputs.AddPoint(x, y);
2473
                        }
2474
                        else
2475
                        {
2476
                            _LMSymbolEnd = GetTargetSymbol(targetSymbol, groupLine);
2477
                            if (_LMSymbolEnd != null)
2478
                                placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y, diagonal);
2479
                            else
2480
                                placeRunInputs.AddPoint(x, y);
2481
                        }
2482
                    }
2483
                    else if (connector.ConnectedObject.GetType() == typeof(Line))
2484
                    {
2485
                        Line targetLine = connector.ConnectedObject as Line;
2486
                        if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
2487
                        {
2488
                            LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
2489
                            if (targetConnector != null)
2490
                            {
2491
                                if (targetLine.SlopeType != SlopeType.HORIZONTAL && targetLine.SlopeType != SlopeType.VERTICAL)
2492
                                    diagonal = true;
2493
                                placeRunInputs.AddConnectorTarget(targetConnector, x, y, diagonal);
2494
                                ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
2495
                            }
2496
                            else
2497
                            {
2498
                                placeRunInputs.AddPoint(x, y);
2499
                                ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
2500
                            }
2501
                        }
2502
                        else
2503
                        {
2504
                            if (groupLine.CONNECTORS.IndexOf(connector) == 0)
2505
                            {
2506
                                index += 0.01;
2507
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
2508
                                    placeRunInputs.AddPoint(x, -0.1 - index);
2509
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
2510
                                    placeRunInputs.AddPoint(-0.1 - index, y);
2511
                                else
2512
                                {
2513
                                    Line nextLine = groupLine.CONNECTORS[0].ConnectedObject as Line;
2514
                                    if (SPPIDUtil.CalcAngle(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y) < 45)
2515
                                        placeRunInputs.AddPoint(-0.1 - index, y);
2516
                                    else
2517
                                        placeRunInputs.AddPoint(x, -0.1 - index);
2518
                                }
2519
                            }
2520

    
2521
                            placeRunInputs.AddPoint(x, y);
2522

    
2523
                            if (groupLine.CONNECTORS.IndexOf(connector) == 1)
2524
                            {
2525
                                index += 0.01;
2526
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
2527
                                    placeRunInputs.AddPoint(x, -0.1 - index);
2528
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
2529
                                    placeRunInputs.AddPoint(-0.1 - index, y);
2530
                                else
2531
                                {
2532
                                    Line nextLine = groupLine.CONNECTORS[1].ConnectedObject as Line;
2533
                                    if (SPPIDUtil.CalcAngle(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y) < 45)
2534
                                        placeRunInputs.AddPoint(-0.1 - index, y);
2535
                                    else
2536
                                        placeRunInputs.AddPoint(x, -0.1 - index);
2537
                                }
2538
                            }
2539
                        }
2540
                    }
2541
                    else
2542
                        placeRunInputs.AddPoint(x, y);
2543
                }
2544

    
2545
                LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2546
                if (_lMConnector != null)
2547
                {
2548
                    _lMConnector.Commit();
2549
                    groupLine.SPPID.ModelItemId = _lMConnector.ModelItemID;
2550

    
2551
                    bool bRemodelingStart = false;
2552
                    if (_LMSymbolStart != null)
2553
                        NeedReModeling(groupLine, _LMSymbolStart, ref bRemodelingStart);
2554
                    bool bRemodelingEnd = false;
2555
                    if (_LMSymbolEnd != null)
2556
                        NeedReModeling(groupLine, _LMSymbolEnd, ref bRemodelingEnd);
2557

    
2558
                    if (bRemodelingStart || bRemodelingEnd)
2559
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
2560

    
2561
                    FlowMarkModeling(groupLine);
2562

    
2563
                    ReleaseCOMObjects(_lMConnector);
2564

    
2565
                    LMModelItem modelItem = dataSource.GetModelItem(groupLine.SPPID.ModelItemId);
2566
                    if (modelItem != null)
2567
                    {
2568
                        LMAAttribute attribute = modelItem.Attributes["FlowDirection"];
2569
                        if (attribute != null)
2570
                            attribute.set_Value("End 1 is upstream (Inlet)");
2571
                        modelItem.Commit();
2572
                    }
2573
                    ReleaseCOMObjects(modelItem);
2574
                    modelItem = null;
2575
                }
2576
                else if (!isBranchModeling)
2577
                {
2578
                    Log.Write("Main Line Modeling : " + groupLine.UID);
2579
                }
2580

    
2581
                List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
2582
                x.ConnectedObject != null &&
2583
                x.ConnectedObject.GetType() == typeof(Line) &&
2584
                !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
2585
                .Select(x => x.ConnectedObject)
2586
                .ToList();
2587

    
2588
                foreach (var item in removeLines)
2589
                    RemoveLineForModeling(item as Line);
2590

    
2591
                ReleaseCOMObjects(_LMAItem);
2592
                _LMAItem = null;
2593
                ReleaseCOMObjects(placeRunInputs);
2594
                placeRunInputs = null;
2595
                ReleaseCOMObjects(_LMSymbolStart);
2596
                _LMSymbolStart = null;
2597
                ReleaseCOMObjects(_LMSymbolEnd);
2598
                _LMSymbolEnd = null;
2599

    
2600
                if (isBranchModeling && BranchLines.Contains(groupLine))
2601
                    BranchLines.Remove(groupLine);
2602
            }
2603
        }
2604

    
2605
        private LMConnector JustLineModeling(Line line)
2606
        {
2607
            bool diagonal = false;
2608
            if (line.SlopeType != SlopeType.HORIZONTAL && line.SlopeType != SlopeType.VERTICAL)
2609
                diagonal = true;
2610
            _LMAItem _LMAItem = _placement.PIDCreateItem(line.SPPID.MAPPINGNAME);
2611
            LMSymbol _LMSymbolStart = null;
2612
            LMSymbol _LMSymbolEnd = null;
2613
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2614
            foreach (var connector in line.CONNECTORS)
2615
            {
2616
                double x = 0;
2617
                double y = 0;
2618
                GetTargetLineConnectorPoint(connector, line, ref x, ref y);
2619
                if (connector.ConnectedObject == null)
2620
                {
2621
                    placeRunInputs.AddPoint(x, y);
2622
                }
2623
                else if (connector.ConnectedObject.GetType() == typeof(Symbol))
2624
                {
2625
                    Symbol targetSymbol = connector.ConnectedObject as Symbol;
2626
                    GetTargetSymbolConnectorPoint(targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line), targetSymbol, ref x, ref y);
2627
                    if (line.CONNECTORS.IndexOf(connector) == 0)
2628
                    {
2629
                        _LMSymbolStart = GetTargetSymbol(targetSymbol, line);
2630
                        if (_LMSymbolStart != null)
2631
                            placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y, diagonal);
2632
                        else
2633
                            placeRunInputs.AddPoint(x, y);
2634
                    }
2635
                    else
2636
                    {
2637
                        _LMSymbolEnd = GetTargetSymbol(targetSymbol, line);
2638
                        if (_LMSymbolEnd != null)
2639
                            placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y, diagonal);
2640
                        else
2641
                            placeRunInputs.AddPoint(x, y);
2642
                    }
2643
                }
2644
                else if (connector.ConnectedObject.GetType() == typeof(Line))
2645
                {
2646
                    Line targetLine = connector.ConnectedObject as Line;
2647
                    if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
2648
                    {
2649
                        LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
2650
                        if (targetConnector != null)
2651
                        {
2652
                            placeRunInputs.AddConnectorTarget(targetConnector, x, y, diagonal);
2653
                            ChangeLineSPPIDCoordinateByConnector(line, targetLine, x, y, false);
2654
                        }
2655
                        else
2656
                        {
2657
                            placeRunInputs.AddPoint(x, y);
2658
                            ChangeLineSPPIDCoordinateByConnector(line, targetLine, x, y, false);
2659
                        }
2660
                    }
2661
                    else
2662
                        placeRunInputs.AddPoint(x, y);
2663
                }
2664
            }
2665

    
2666
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2667
            if (_lMConnector != null)
2668
                _lMConnector.Commit();
2669

    
2670
            ReleaseCOMObjects(_LMAItem);
2671
            _LMAItem = null;
2672
            ReleaseCOMObjects(placeRunInputs);
2673
            placeRunInputs = null;
2674
            ReleaseCOMObjects(_LMSymbolStart);
2675
            _LMSymbolStart = null;
2676
            ReleaseCOMObjects(_LMSymbolEnd);
2677
            _LMSymbolEnd = null;
2678

    
2679
            return _lMConnector;
2680
        }
2681

    
2682
        private void RemoveLineForModeling(Line line)
2683
        {
2684
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2685
            if (modelItem != null)
2686
            {
2687
                foreach (LMRepresentation rep in modelItem.Representations)
2688
                {
2689
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2690
                    {
2691
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2692
                        dynamic OID = rep.get_GraphicOID().ToString();
2693
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2694
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2695
                        int verticesCount = lineStringGeometry.VertexCount;
2696
                        double[] vertices = null;
2697
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2698
                        for (int i = 0; i < verticesCount; i++)
2699
                        {
2700
                            double x = 0;
2701
                            double y = 0;
2702
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2703
                            if (verticesCount == 2 && (x < 0 || y < 0))
2704
                                _placement.PIDRemovePlacement(rep);
2705
                        }
2706
                        ReleaseCOMObjects(_LMConnector);
2707
                    }
2708
                }
2709

    
2710
                ReleaseCOMObjects(modelItem);
2711
            }
2712
        }
2713

    
2714
        private void RemoveLine(Line line)
2715
        {
2716
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2717
            if (modelItem != null)
2718
            {
2719
                foreach (LMRepresentation rep in modelItem.Representations)
2720
                {
2721
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2722
                        _placement.PIDRemovePlacement(rep);
2723
                }
2724
                ReleaseCOMObjects(modelItem);
2725
            }
2726
            line.SPPID.ModelItemId = null;
2727
        }
2728

    
2729
        private void GetConnectedLineGroup(Line line, List<Line> group)
2730
        {
2731
            if (!group.Contains(line))
2732
                group.Add(line);
2733
            foreach (var connector in line.CONNECTORS)
2734
            {
2735
                if (connector.ConnectedObject != null &&
2736
                    connector.ConnectedObject.GetType() == typeof(Line) &&
2737
                    !group.Contains(connector.ConnectedObject) &&
2738
                    string.IsNullOrEmpty(((Line)connector.ConnectedObject).SPPID.ModelItemId))
2739
                {
2740
                    Line connLine = connector.ConnectedObject as Line;
2741
                    if (!SPPIDUtil.IsBranchLine(connLine, line))
2742
                    {
2743
                        if (line.CONNECTORS.IndexOf(connector) == 0)
2744
                            group.Insert(0, connLine);
2745
                        else
2746
                            group.Add(connLine);
2747
                        GetConnectedLineGroup(connLine, group);
2748
                    }
2749
                }
2750
            }
2751
        }
2752

    
2753
        private void LineCoordinateCorrection(List<Line> group)
2754
        {
2755
            // 순서대로 전 Item 기준 정렬
2756
            LineCoordinateCorrectionByStart(group);
2757

    
2758
            // 역으로 심볼이 있을 경우 좌표 보정
2759
            LineCoordinateCorrectionForLastLine(group);
2760
        }
2761

    
2762
        private void LineCoordinateCorrectionByStart(List<Line> group)
2763
        {
2764
            for (int i = 0; i < group.Count; i++)
2765
            {
2766
                Line line = group[i];
2767
                if (i == 0)
2768
                {
2769
                    Connector symbolConnector = line.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
2770
                    if (symbolConnector != null)
2771
                        LineCoordinateCorrectionByConnItem(line, symbolConnector.ConnectedObject);
2772
                }
2773
                else if (i != 0)
2774
                {
2775
                    LineCoordinateCorrectionByConnItem(line, group[i - 1]);
2776
                }
2777
            }
2778
        }
2779

    
2780
        private void LineCoordinateCorrectionForLastLine(List<Line> group)
2781
        {
2782
            Line checkLine = group[group.Count - 1];
2783
            Connector lastSymbolConnector = checkLine.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
2784
            if (lastSymbolConnector != null)
2785
            {
2786
                LineCoordinateCorrectionByConnItem(checkLine, lastSymbolConnector.ConnectedObject);
2787
                for (int i = group.Count - 2; i >= 0; i--)
2788
                {
2789
                    Line line = group[i + 1];
2790
                    Line prevLine = group[i];
2791

    
2792
                    // 같으면 보정
2793
                    if (line.SlopeType == prevLine.SlopeType)
2794
                        LineCoordinateCorrectionByConnItem(prevLine, line);
2795
                    else
2796
                    {
2797
                        if (line.SlopeType == SlopeType.HORIZONTAL)
2798
                        {
2799
                            double prevX = 0;
2800
                            double prevY = 0;
2801
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
2802
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(line, prevLine, prevX);
2803

    
2804
                            double x = 0;
2805
                            double y = 0;
2806
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
2807
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(prevLine, line, y);
2808
                        }
2809
                        else if (line.SlopeType == SlopeType.VERTICAL)
2810
                        {
2811
                            double prevX = 0;
2812
                            double prevY = 0;
2813
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
2814
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(line, prevLine, prevY);
2815

    
2816
                            double x = 0;
2817
                            double y = 0;
2818
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
2819
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
2820
                        }
2821
                        break;
2822
                    }
2823
                }
2824
            }
2825
        }
2826

    
2827
        private void LineCoordinateCorrectionByConnItem(Line line, object connItem)
2828
        {
2829
            double x = 0;
2830
            double y = 0;
2831
            if (connItem.GetType() == typeof(Symbol))
2832
            {
2833
                Symbol targetSymbol = connItem as Symbol;
2834
                Connector targetConnector = targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line);
2835
                if (targetConnector != null)
2836
                    GetTargetSymbolConnectorPoint(targetConnector, targetSymbol, ref x, ref y);
2837
                else
2838
                    throw new Exception("Target symbol UID : " + targetSymbol.UID + "\r\nLine UID : " + line.UID);
2839
            }
2840
            else if (connItem.GetType() == typeof(Line))
2841
            {
2842
                Line targetLine = connItem as Line;
2843
                GetTargetLineConnectorPoint(targetLine.CONNECTORS.Find(z => z.ConnectedObject == line), targetLine, ref x, ref y);
2844
            }
2845

    
2846
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
2847
        }
2848

    
2849
        private void ChangeLineSPPIDCoordinateByConnector(Line line, object connItem, double x, double y, bool changeOtherCoordinate = true)
2850
        {
2851
            bool isReverseX = line.SPPID.END_X - line.SPPID.START_X < 0;
2852
            bool isReverseY = line.SPPID.END_Y - line.SPPID.START_Y < 0;
2853

    
2854
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2855
            int index = line.CONNECTORS.IndexOf(connector);
2856
            if (index == 0)
2857
            {                
2858
                line.SPPID.START_X = x;
2859
                line.SPPID.START_Y = y;
2860
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
2861
                {
2862
                    line.SPPID.END_Y = y;
2863
                    // START_X가 END_X 값을 벗어날 경우 END_X 값 보정
2864
                    if ((line.SPPID.END_X - line.SPPID.START_X) * (!isReverseX ? 1 : -1) <= 0)
2865
                    {
2866
                        line.SPPID.END_X = line.SPPID.START_X + (GridSetting.GetInstance().Length * (!isReverseX ? 1 : -1));
2867
                    }
2868
                }
2869
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
2870
                {
2871
                    line.SPPID.END_X = x;
2872
                    // START_Y가 END_Y 값을 벗어날 경우 END_Y 값 보정
2873
                    if ((line.SPPID.END_Y - line.SPPID.START_Y) * (!isReverseY ? 1 : -1) <= 0)
2874
                    {
2875
                        line.SPPID.END_Y = line.SPPID.START_Y + (GridSetting.GetInstance().Length * (!isReverseY ? 1 : -1));
2876
                    }
2877
                }
2878
            }
2879
            else
2880
            {
2881
                line.SPPID.END_X = x;
2882
                line.SPPID.END_Y = y;
2883
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
2884
                {
2885
                    line.SPPID.START_Y = y;
2886
                    // END_X가 START_X 값을 벗어날 경우 START_X 값 보정
2887
                    if ((line.SPPID.END_X - line.SPPID.START_X) * (isReverseX ? 1 : -1) <= 0)
2888
                    {
2889
                        line.SPPID.START_X = line.SPPID.END_X - (GridSetting.GetInstance().Length * (!isReverseX ? 1 : -1));
2890
                    }
2891
                }
2892
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
2893
                {
2894
                    line.SPPID.START_X = x;
2895
                    // END_Y가 START_Y 값을 벗어날 경우 START_Y 값 보정
2896
                    if ((line.SPPID.END_Y - line.SPPID.START_Y) * (isReverseY ? 1 : -1) <= 0)
2897
                    {
2898
                        line.SPPID.START_Y = line.SPPID.END_Y - (GridSetting.GetInstance().Length * (!isReverseY ? 1 : -1));
2899
                    }
2900
                }
2901
            }
2902
        }
2903

    
2904
        private void ChangeLineSPPIDCoordinateByConnectorOnlyX(Line line, object connItem, double x)
2905
        {
2906
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2907
            int index = line.CONNECTORS.IndexOf(connector);
2908
            if (index == 0)
2909
            {
2910
                line.SPPID.START_X = x;
2911
                if (line.SlopeType == SlopeType.VERTICAL)
2912
                    line.SPPID.END_X = x;
2913
            }
2914
            else
2915
            {
2916
                line.SPPID.END_X = x;
2917
                if (line.SlopeType == SlopeType.VERTICAL)
2918
                    line.SPPID.START_X = x;
2919
            }
2920
        }
2921

    
2922
        private void ChangeLineSPPIDCoordinateByConnectorOnlyY(Line line, object connItem, double y)
2923
        {
2924
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2925
            int index = line.CONNECTORS.IndexOf(connector);
2926
            if (index == 0)
2927
            {
2928
                line.SPPID.START_Y = y;
2929
                if (line.SlopeType == SlopeType.HORIZONTAL)
2930
                    line.SPPID.END_Y = y;
2931
            }
2932
            else
2933
            {
2934
                line.SPPID.END_Y = y;
2935
                if (line.SlopeType == SlopeType.HORIZONTAL)
2936
                    line.SPPID.START_Y = y;
2937
            }
2938
        }
2939

    
2940
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
2941
        {
2942
            if (symbol != null)
2943
            {
2944
                string repID = symbol.AsLMRepresentation().Id;
2945
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
2946
                string lineUID = line.UID;
2947

    
2948
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
2949
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
2950
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
2951

    
2952
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
2953
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
2954
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
2955

    
2956
                if (startSpecBreak != null || startEndBreak != null)
2957
                    result = true;
2958
            }
2959
        }
2960

    
2961
        /// <summary>
2962
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
2963
        /// </summary>
2964
        /// <param name="lines"></param>
2965
        /// <param name="prevLMConnector"></param>
2966
        /// <param name="startSymbol"></param>
2967
        /// <param name="endSymbol"></param>
2968
        private void ReModelingLine(Line line, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
2969
        {
2970
            string symbolPath = string.Empty;
2971
            #region get symbol path
2972
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
2973
            symbolPath = GetSPPIDFileName(modelItem);
2974
            ReleaseCOMObjects(modelItem);
2975
            #endregion
2976
            bool diagonal = false;
2977
            if (line.SlopeType != SlopeType.HORIZONTAL && line.SlopeType != SlopeType.VERTICAL)
2978
                diagonal = true;
2979
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2980
            LMConnector newConnector = null;
2981
            dynamic OID = prevLMConnector.get_GraphicOID().ToString();
2982
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2983
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2984
            int verticesCount = lineStringGeometry.VertexCount;
2985
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2986

    
2987
            List<double[]> vertices = new List<double[]>();
2988
            for (int i = 1; i <= verticesCount; i++)
2989
            {
2990
                double x = 0;
2991
                double y = 0;
2992
                lineStringGeometry.GetVertex(i, ref x, ref y);
2993
                vertices.Add(new double[] { x, y });
2994
            }
2995

    
2996
            for (int i = 0; i < vertices.Count; i++)
2997
            {
2998
                double[] points = vertices[i];
2999
                // 시작 심볼이 있고 첫번째 좌표일 때
3000
                if (startSymbol != null && i == 0)
3001
                {
3002
                    if (bStart)
3003
                    {
3004
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
3005
                        if (slopeType == SlopeType.HORIZONTAL)
3006
                            placeRunInputs.AddPoint(points[0], -0.1);
3007
                        else if (slopeType == SlopeType.VERTICAL)
3008
                            placeRunInputs.AddPoint(-0.1, points[1]);
3009
                        else
3010
                            placeRunInputs.AddPoint(points[0], -0.1);
3011

    
3012
                        placeRunInputs.AddPoint(points[0], points[1]);
3013
                    }
3014
                    else
3015
                    {
3016
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1], diagonal);
3017
                    }
3018
                }
3019
                // 마지막 심볼이 있고 마지막 좌표일 때
3020
                else if (endSymbol != null && i == vertices.Count - 1)
3021
                {
3022
                    if (bEnd)
3023
                    {
3024
                        placeRunInputs.AddPoint(points[0], points[1]);
3025

    
3026
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
3027
                        if (slopeType == SlopeType.HORIZONTAL)
3028
                            placeRunInputs.AddPoint(points[0], -0.1);
3029
                        else if (slopeType == SlopeType.VERTICAL)
3030
                            placeRunInputs.AddPoint(-0.1, points[1]);
3031
                        else
3032
                            placeRunInputs.AddPoint(points[0], -0.1);
3033
                    }
3034
                    else
3035
                    {
3036
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1], diagonal);
3037
                    }
3038
                }
3039
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
3040
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
3041
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1], diagonal);
3042
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
3043
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
3044
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1], diagonal);
3045
                else
3046
                    placeRunInputs.AddPoint(points[0], points[1]);
3047
            }
3048

    
3049
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
3050
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
3051

    
3052
            ReleaseCOMObjects(placeRunInputs);
3053
            ReleaseCOMObjects(_LMAItem);
3054
            ReleaseCOMObjects(modelItem);
3055

    
3056
            if (newConnector != null)
3057
            {
3058
                newConnector.Commit();
3059
                if (startSymbol != null && bStart)
3060
                {
3061
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
3062
                    placeRunInputs = new PlaceRunInputs();
3063
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
3064
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
3065
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
3066
                    if (_LMConnector != null)
3067
                    {
3068
                        _LMConnector.Commit();
3069
                        RemoveConnectorForReModelingLine(newConnector);
3070
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
3071
                        ReleaseCOMObjects(_LMConnector);
3072
                    }
3073
                    ReleaseCOMObjects(placeRunInputs);
3074
                    ReleaseCOMObjects(_LMAItem);
3075
                }
3076

    
3077
                if (endSymbol != null && bEnd)
3078
                {
3079
                    if (startSymbol != null)
3080
                    {
3081
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
3082
                        newConnector = dicVertices.First().Key;
3083
                    }
3084

    
3085
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
3086
                    placeRunInputs = new PlaceRunInputs();
3087
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
3088
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
3089
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
3090
                    if (_LMConnector != null)
3091
                    {
3092
                        _LMConnector.Commit();
3093
                        RemoveConnectorForReModelingLine(newConnector);
3094
                        ZeroLengthModelItemIDReverse.Add(_LMConnector.ModelItemID);
3095
                        ReleaseCOMObjects(_LMConnector);
3096
                    }
3097
                    ReleaseCOMObjects(placeRunInputs);
3098
                    ReleaseCOMObjects(_LMAItem);
3099
                }
3100

    
3101
                line.SPPID.ModelItemId = newConnector.ModelItemID;
3102
                ReleaseCOMObjects(newConnector);
3103
            }
3104

    
3105
            ReleaseCOMObjects(modelItem);
3106
        }
3107

    
3108
        /// <summary>
3109
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
3110
        /// </summary>
3111
        /// <param name="connector"></param>
3112
        private void RemoveConnectorForReModelingLine(LMConnector connector)
3113
        {
3114
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
3115
            foreach (var item in dicVertices)
3116
            {
3117
                if (item.Value.Count == 2)
3118
                {
3119
                    bool result = false;
3120
                    foreach (var point in item.Value)
3121
                    {
3122
                        if (point[0] < 0 || point[1] < 0)
3123
                        {
3124
                            result = true;
3125
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
3126
                            break;
3127
                        }
3128
                    }
3129

    
3130
                    if (result)
3131
                        break;
3132
                }
3133
            }
3134
            foreach (var item in dicVertices)
3135
                ReleaseCOMObjects(item.Key);
3136
        }
3137

    
3138
        /// <summary>
3139
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
3140
        /// </summary>
3141
        /// <param name="symbol"></param>
3142
        /// <param name="line"></param>
3143
        /// <returns></returns>
3144
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
3145
        {
3146
            LMSymbol _LMSymbol = null;
3147
            foreach (var connector in symbol.CONNECTORS)
3148
            {
3149
                if (connector.CONNECTEDITEM == line.UID)
3150
                {
3151
                    if (connector.Index == 0)
3152
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3153
                    else
3154
                    {
3155
                        ChildSymbol child = null;
3156
                        foreach (var childSymbol in symbol.ChildSymbols)
3157
                        {
3158
                            if (childSymbol.Connectors.Contains(connector))
3159
                                child = childSymbol;
3160
                            else
3161
                                child = GetChildSymbolByConnector(childSymbol, connector);
3162

    
3163
                            if (child != null)
3164
                                break;
3165
                        }
3166

    
3167
                        if (child != null)
3168
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
3169
                    }
3170

    
3171
                    break;
3172
                }
3173
            }
3174

    
3175
            return _LMSymbol;
3176
        }
3177

    
3178
        /// <summary>
3179
        /// Connector를 가지고 있는 ChildSymbol Object 반환
3180
        /// </summary>
3181
        /// <param name="item"></param>
3182
        /// <param name="connector"></param>
3183
        /// <returns></returns>
3184
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
3185
        {
3186
            foreach (var childSymbol in item.ChildSymbols)
3187
            {
3188
                if (childSymbol.Connectors.Contains(connector))
3189
                    return childSymbol;
3190
                else
3191
                    return GetChildSymbolByConnector(childSymbol, connector);
3192
            }
3193

    
3194
            return null;
3195
        }
3196

    
3197
        /// <summary>
3198
        /// EndBreak 모델링 메서드
3199
        /// </summary>
3200
        /// <param name="endBreak"></param>
3201
        private void EndBreakModeling(EndBreak endBreak)
3202
        {
3203
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
3204
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
3205

    
3206
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
3207
            if (ownerObj.GetType() == typeof(Symbol) && connectedItem.GetType() == typeof(Symbol) && targetLMConnector != null)
3208
                targetLMConnector = ReModelingZeroLengthLMConnectorForSegment(targetLMConnector);
3209

    
3210
            if (targetLMConnector != null)
3211
            {
3212
                // LEADER Line 검사
3213
                bool leaderLine = false;
3214
                SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == endBreak.DBUID);
3215
                if (symbolMapping != null)
3216
                    leaderLine = symbolMapping.LEADERLINE;
3217

    
3218
                SegmentLocation location;
3219
                double[] point = GetSegmentPoint(ownerObj, connectedItem, targetLMConnector, out location);
3220
                Array array = null;
3221
                if (point != null)
3222
                    array = new double[] { 0, point[0], point[1] };
3223
                else
3224
                    array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
3225

    
3226
                LMLabelPersist _LmLabelPersist;
3227

    
3228
                Property property = endBreak.PROPERTIES.Find(loop => loop.ATTRIBUTE == "Freeze");
3229
                if (property != null && !string.IsNullOrEmpty(property.VALUE) && property.VALUE.Equals("True"))
3230
                {
3231
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, null, Rotation: endBreak.ANGLE, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
3232
                }
3233
                else
3234
                {
3235
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, null, null, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
3236
                }
3237
                
3238
                if (_LmLabelPersist != null)
3239
                {
3240
                    _LmLabelPersist.Commit();
3241
                    endBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
3242
                    if (_LmLabelPersist.ModelItemObject != null)
3243
                        endBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
3244
                    endBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
3245

    
3246
                    MoveDependencyObject(endBreak.SPPID.GraphicOID, location);
3247

    
3248
                    // end break arrange
3249
                    if (property == null || string.IsNullOrEmpty(property.VALUE) || !property.VALUE.Equals("True"))
3250
                    {
3251
                        MoveSegmentBreak(_LmLabelPersist.RepresentationObject.Id, _LmLabelPersist);
3252
                    }
3253

    
3254
                    ReleaseCOMObjects(_LmLabelPersist);
3255
                }
3256
                ReleaseCOMObjects(targetLMConnector);
3257
            }
3258
            else
3259
            {
3260
                Log.Write("End Break UID : " + endBreak.UID);
3261
                Log.Write("Can't find targetLMConnector");
3262
            }
3263
        }
3264

    
3265
        private void MoveDependencyObject(string graphicOID, SegmentLocation location)
3266
        {
3267
            double x = 0, y = 0;
3268
            if (location.HasFlag(SegmentLocation.Up))
3269
                y = GridSetting.GetInstance().Length * 3;
3270
            else if (location.HasFlag(SegmentLocation.Down))
3271
                y = -GridSetting.GetInstance().Length * 3;
3272

    
3273
            if (location.HasFlag(SegmentLocation.Right))
3274
                x = GridSetting.GetInstance().Length * 3;
3275
            else if (location.HasFlag(SegmentLocation.Left))
3276
                x = -GridSetting.GetInstance().Length * 3;
3277

    
3278
            if (x != 0 || y != 0)
3279
            {
3280
                radApp.ActiveSelectSet.RemoveAll();
3281
                DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] as DependencyObject;
3282
                if (dependency != null)
3283
                {
3284
                    radApp.ActiveSelectSet.Add(dependency);
3285
                    Ingr.RAD2D.Transform transform = dependency.GetTransform();
3286
                    transform.DefineByMove2d(x, y);
3287
                    radApp.ActiveSelectSet.Transform(transform, true);
3288
                    radApp.ActiveSelectSet.RemoveAll();
3289
                }
3290
            }
3291
        }
3292

    
3293
        private LMConnector ReModelingZeroLengthLMConnectorForSegment(LMConnector connector, string changeSymbolPath = null)
3294
        {
3295
            string symbolPath = string.Empty;
3296
            #region get symbol path
3297
            if (string.IsNullOrEmpty(changeSymbolPath))
3298
            {
3299
                LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
3300
                symbolPath = GetSPPIDFileName(modelItem);
3301
                ReleaseCOMObjects(modelItem);
3302
            }
3303
            else
3304
                symbolPath = changeSymbolPath;
3305

    
3306
            #endregion
3307

    
3308
            LMConnector newConnector = null;
3309
            dynamic OID = connector.get_GraphicOID().ToString();
3310
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
3311
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3312
            int verticesCount = lineStringGeometry.VertexCount;
3313
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
3314
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
3315

    
3316
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
3317
            {
3318
                double[] vertices = null;
3319
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
3320
                double x = 0;
3321
                double y = 0;
3322
                lineStringGeometry.GetVertex(1, ref x, ref y);
3323

    
3324
                string flowDirection = string.Empty;
3325
                LMAAttribute flowAttribute = connector.ModelItemObject.Attributes["FlowDirection"];
3326
                if (flowAttribute != null && !DBNull.Value.Equals(flowAttribute.get_Value()))
3327
                    flowDirection = flowAttribute.get_Value().ToString();
3328

    
3329
                if (flowDirection == "End 1 is downstream (Outlet)")
3330
                {
3331
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
3332
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
3333
                    flowDirection = "End 1 is upstream (Inlet)";
3334
                }
3335
                else
3336
                {
3337
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
3338
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
3339
                }
3340
                string oldModelItemId = connector.ModelItemID;
3341
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
3342
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
3343
                newConnector.Commit();
3344
                ZeroLengthSymbolToSymbolModelItemID.Add(newConnector.ModelItemID);
3345
                if (!string.IsNullOrEmpty(flowDirection))
3346
                    newConnector.ModelItemObject.Attributes["FlowDirection"].set_Value(flowDirection);
3347
                ReleaseCOMObjects(connector);
3348

    
3349
                foreach (var line in document.LINES.FindAll(z => z.SPPID.ModelItemId == oldModelItemId))
3350
                {
3351
                    foreach (var repId in line.SPPID.Representations)
3352
                    {
3353
                        LMConnector _connector = dataSource.GetConnector(repId);
3354
                        if (_connector != null && _connector.get_ItemStatus() == "Active")
3355
                        {
3356
                            if (line.SPPID.ModelItemId != _connector.ModelItemID)
3357
                            {
3358
                                line.SPPID.ModelItemId = _connector.ModelItemID;
3359
                                line.SPPID.Representations = GetRepresentations(line.SPPID.ModelItemId);
3360
                            }
3361
                        }
3362
                        ReleaseCOMObjects(_connector);
3363
                        _connector = null;
3364
                    }
3365
                }
3366
            }
3367

    
3368
            return newConnector;
3369
        }
3370

    
3371
        /// <summary>
3372
        /// SpecBreak Modeling 메서드
3373
        /// </summary>
3374
        /// <param name="specBreak"></param>
3375
        private void SpecBreakModeling(SpecBreak specBreak)
3376
        {
3377
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
3378
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
3379

    
3380
            if (upStreamObj != null &&
3381
                downStreamObj != null)
3382
            {
3383
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
3384
                if (upStreamObj.GetType() == typeof(Symbol) && downStreamObj.GetType() == typeof(Symbol) &&
3385
                    targetLMConnector != null &&
3386
                    !IsModelingEndBreak(upStreamObj as Symbol, downStreamObj as Symbol))
3387
                    targetLMConnector = ReModelingZeroLengthLMConnectorForSegment(targetLMConnector);
3388

    
3389
                if (targetLMConnector != null)
3390
                {
3391
                    foreach (var attribute in specBreak.ATTRIBUTES)
3392
                    {
3393
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
3394
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
3395
                        {
3396
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
3397
                            SegmentLocation location;
3398
                            double[] point = GetSegmentPoint(upStreamObj, downStreamObj, targetLMConnector, out location);
3399
                            Array array = null;
3400
                            if (point != null)
3401
                                array = new double[] { 0, point[0], point[1] };
3402
                            else
3403
                                array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
3404
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, null, null, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3405

    
3406
                            if (_LmLabelPersist != null)
3407
                            {
3408
                                _LmLabelPersist.Commit();
3409
                                specBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
3410
                                if (_LmLabelPersist.ModelItemObject != null)
3411
                                    specBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
3412
                                specBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
3413

    
3414
                                MoveDependencyObject(specBreak.SPPID.GraphicOID, location);
3415

    
3416
                                // spec break arrange
3417
                                MoveSegmentBreak(_LmLabelPersist.RepresentationObject.Id, _LmLabelPersist);
3418

    
3419
                                ReleaseCOMObjects(_LmLabelPersist);
3420
                            }
3421
                        }
3422
                    }
3423

    
3424
                    Property property = specBreak.PROPERTIES.Find(loop => loop.ATTRIBUTE == "Show");
3425
                    if (property != null && !string.IsNullOrEmpty(property.VALUE) && property.VALUE.Equals("True"))
3426
                    {
3427
                        // temp
3428
                        ReleaseCOMObjects(_placement.PIDPlaceSymbol(@"\Design\Annotation\Graphics\Break.sym", specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y, Rotation: specBreak.ANGLE));
3429
                    }
3430
                    ReleaseCOMObjects(targetLMConnector);
3431
                }
3432
                else
3433
                {
3434
                    Log.Write("Spec Break UID : " + specBreak.UID);
3435
                    Log.Write("Can't find targetLMConnector");
3436
                }
3437
            }
3438
        }
3439

    
3440
        private bool IsRhombus(LMLabelPersist labelPersist, out double x, out double y, out double radius)
3441
        {
3442
            bool result = false;
3443
            x = 0; y = 0; radius = 0;
3444

    
3445
            string oid = labelPersist.get_GraphicOID().ToString();
3446
            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[oid] as DependencyObject;
3447

    
3448
            if (dependency != null)
3449
            {
3450
                bool isLabel = false;
3451
                foreach (var attributes in dependency.AttributeSets)
3452
                {
3453
                    foreach (var attribute in attributes)
3454
                    {
3455
                        string name = attribute.Name;
3456
                        string value = attribute.GetValue().ToString();
3457
                        if (name == "DrawingItemType" && value == "LabelPersist")
3458
                        {
3459
                            isLabel = true;
3460
                            break;
3461
                        }
3462
                    }
3463
                }
3464
                if (isLabel)
3465
                {
3466
                    double minX = double.MaxValue, minY = double.MaxValue, maxX = double.MinValue, maxY = double.MinValue;
3467
                    foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
3468
                    {
3469
                        if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLine2d)
3470
                        {
3471
                            Ingr.RAD2D.Line2d line2D = drawingObject as Ingr.RAD2D.Line2d;
3472

    
3473
                            double x1, y1, x2, y2;
3474
                            line2D.GetStartPoint(out x1, out y1);
3475
                            line2D.GetEndPoint(out x2, out y2);
3476
                            double tX1 = Math.Min(x1, x2), tY1 = Math.Min(y1, y2), tX2 = Math.Max(x1, x2), tY2 = Math.Max(y1, y2);
3477
                            if (minX > tX1)
3478
                                minX = tX1;
3479
                            if (minY > tY1)
3480
                                minY = tY1;
3481
                            if (maxX < tX2)
3482
                                maxX = tX2;
3483
                            if (maxY < tY2)
3484
                                maxY = tY2;
3485
                        }
3486
                    }
3487

    
3488
                    double width = Math.Abs(maxX - minX);
3489
                    double height = Math.Abs(maxY - minY);
3490
                    double ratio = width / height * 100;
3491
                    if (ratio > 99d && ratio < 101d)
3492
                    {
3493
                        result = true;
3494
                    }
3495
                    x = (maxX + minX) / 2d;
3496
                    y = (maxY + minY) / 2d;
3497
                    radius = width / 2d;
3498
                }
3499
            }
3500

    
3501
            return result;
3502
        }
3503

    
3504
        private void MoveSegmentBreak(string connectorID, LMLabelPersist labelPersist)
3505
        {
3506
            bool bFind = false;
3507
            double x, y, radius;
3508
            if (IsRhombus(labelPersist, out x, out y, out radius))
3509
            {
3510
                List<double[]> itemPoints = new List<double[]>();
3511
                LMConnector connector = dataSource.GetConnector(connectorID);
3512
                foreach (LMLabelPersist label in connector.LabelPersists)
3513
                {
3514
                    if (!"Active".Equals(label.get_ItemStatus()))
3515
                        continue;
3516

    
3517
                    if (!label.Id.Equals(labelPersist.Id))
3518
                    {
3519
                        double centerX, centerY, temp;
3520
                        if (IsRhombus(label, out centerX, out centerY, out temp))
3521
                        {
3522
                            bFind = true;
3523
                            itemPoints.Add(new double[] { centerX, centerY });
3524
                        }
3525
                    }
3526
                }
3527
                ReleaseCOMObjects(connector);
3528

    
3529

    
3530
                if (bFind)
3531
                {
3532
                    double[] startPoint = itemPoints.First();
3533
                    itemPoints.RemoveAt(0);
3534

    
3535
                    for (int i = 0; i < 8; i++)
3536
                    {
3537
                        double pointX = 0, pointY = 0;
3538
                        switch (i)
3539
                        {
3540
                            case 0:
3541
                                pointX = startPoint[0] + radius;
3542
                                pointY = startPoint[1] + radius;
3543
                                break;
3544
                            case 1:
3545
                                pointX = startPoint[0] + radius + radius;
3546
                                pointY = startPoint[1];
3547
                                break;
3548
                            case 2:
3549
                                pointX = startPoint[0] + radius;
3550
                                pointY = startPoint[1] - radius;
3551
                                break;
3552
                            case 3:
3553
                                pointX = startPoint[0];
3554
                                pointY = startPoint[1] - radius - radius;
3555
                                break;
3556
                            case 4:
3557
                                pointX = startPoint[0] - radius;
3558
                                pointY = startPoint[1] - radius;
3559
                                break;
3560
                            case 5:
3561
                                pointX = startPoint[0] - radius - radius;
3562
                                pointY = startPoint[1];
3563
                                break;
3564
                            case 6:
3565
                                pointX = startPoint[0] - radius;
3566
                                pointY = startPoint[1] + radius;
3567
                                break;
3568
                            case 7:
3569
                                pointX = startPoint[0];
3570
                                pointY = startPoint[1] + radius + radius;
3571
                                break;
3572
                            default:
3573
                                break;
3574
                        }
3575

    
3576
                        if (!ExistSegmentByPoint(pointX, pointY))
3577
                        {
3578
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[labelPersist.get_GraphicOID().ToString()] as DependencyObject;
3579
                            if (dependency != null)
3580
                            {
3581
                                radApp.ActiveSelectSet.RemoveAll();
3582
                                radApp.ActiveSelectSet.Add(dependency);
3583
                                Ingr.RAD2D.Transform transform = dependency.GetTransform();
3584
                                transform.DefineByMove2d(pointX - x, pointY - y);
3585
                                radApp.ActiveSelectSet.Transform(transform, true);
3586
                                radApp.ActiveSelectSet.RemoveAll();
3587
                            }
3588
                            break;
3589
                        }
3590
                    }
3591

    
3592
                    bool ExistSegmentByPoint(double pointX, double pointY)
3593
                    {
3594
                        bool result = false;
3595
                        foreach (var item in itemPoints)
3596
                        {
3597
                            double distance = SPPIDUtil.CalcPointToPointdDistance(item[0], item[1], pointX, pointY);
3598
                            if (Math.Truncate(distance * 1000000000d).Equals(0))
3599
                                result = true;
3600
                        }
3601
                        return result;
3602
                    }
3603
                }
3604
            }
3605

    
3606
            if (!bFind)
3607
                MoveSegmentBestLocation(labelPersist.get_GraphicOID().ToString(), new double[] { x - radius, y - radius, x + radius, y + radius }, itemRange);
3608
        }
3609

    
3610
        LMConnectors GetConnectors()
3611
        {
3612
            LMAFilter filter = new LMAFilter();
3613
            LMACriterion criterion1 = new LMACriterion();
3614
            criterion1.SourceAttributeName = "SP_DRAWINGID";
3615
            criterion1.Operator = "=";
3616
            criterion1.set_ValueAttribute(dataSource.PIDMgr.Drawing.ID);
3617
            criterion1.Conjunctive = true;
3618
            filter.get_Criteria().Add(criterion1);
3619
            filter.ItemType = "Connector";
3620

    
3621
            LMACriterion criterion2 = new LMACriterion();
3622
            criterion2.SourceAttributeName = "ITEMSTATUS";
3623
            criterion2.Operator = "=";
3624
            criterion2.set_ValueAttribute("1");
3625
            criterion2.Conjunctive = true;
3626
            filter.get_Criteria().Add(criterion2);
3627

    
3628
            LMACriterion criterion3 = new LMACriterion();
3629
            criterion3.SourceAttributeName = "INSTOCKPILE";
3630
            criterion3.Operator = "=";
3631
            criterion3.set_ValueAttribute("1");
3632
            criterion3.Conjunctive = true;
3633
            filter.get_Criteria().Add(criterion3);
3634

    
3635
            LMConnectors items = new LMConnectors();
3636
            items.Collect(dataSource, Filter: filter);
3637

    
3638
            ReleaseCOMObjects(filter);
3639
            ReleaseCOMObjects(criterion1);
3640
            ReleaseCOMObjects(criterion2);
3641
            ReleaseCOMObjects(criterion3);
3642

    
3643
            return items;
3644
        }
3645
        LMSymbols GetSymbols()
3646
        {
3647
            LMAFilter filter = new LMAFilter();
3648
            LMACriterion criterion1 = new LMACriterion();
3649
            criterion1.SourceAttributeName = "SP_DRAWINGID";
3650
            criterion1.Operator = "=";
3651
            criterion1.set_ValueAttribute(dataSource.PIDMgr.Drawing.ID);
3652
            criterion1.Conjunctive = true;
3653
            filter.get_Criteria().Add(criterion1);
3654
            filter.ItemType = "Symbol";
3655

    
3656
            LMACriterion criterion2 = new LMACriterion();
3657
            criterion2.SourceAttributeName = "ITEMSTATUS";
3658
            criterion2.Operator = "=";
3659
            criterion2.set_ValueAttribute("1");
3660
            criterion2.Conjunctive = true;
3661
            filter.get_Criteria().Add(criterion2);
3662

    
3663
            LMACriterion criterion3 = new LMACriterion();
3664
            criterion3.SourceAttributeName = "INSTOCKPILE";
3665
            criterion3.Operator = "=";
3666
            criterion3.set_ValueAttribute("1");
3667
            criterion3.Conjunctive = true;
3668
            filter.get_Criteria().Add(criterion3);
3669

    
3670
            LMSymbols items = new LMSymbols();
3671
            items.Collect(dataSource, Filter: filter);
3672

    
3673
            ReleaseCOMObjects(filter);
3674
            ReleaseCOMObjects(criterion1);
3675
            ReleaseCOMObjects(criterion2);
3676
            ReleaseCOMObjects(criterion3);
3677

    
3678
            return items;
3679
        }
3680

    
3681
        private void SetConnectorAndSymbolRange()
3682
        {
3683
            itemRange = new List<double[]>();
3684

    
3685
            LMConnectors connectors = GetConnectors();
3686
            foreach (LMConnector connector in connectors)
3687
            {
3688
                List<double[]> vertices = GetConnectorVertices(connector);
3689
                for (int i = 0; i < vertices.Count - 1; i++)
3690
                {
3691
                    double[] point1 = vertices[i];
3692
                    double[] point2 = vertices[i + 1];
3693
                    double x1 = Math.Min(point1[0], point2[0]), y1 = Math.Min(point1[1], point2[1]), x2 = Math.Max(point1[0], point2[0]), y2 = Math.Max(point1[1], point2[1]);
3694
                    double gap = 0.0001d;
3695
                    itemRange.Add(new double[] { x1 - gap, y1 - gap, x2 + gap, y2 + gap });
3696
                }
3697
                ReleaseCOMObjects(connector);
3698
            }
3699
            ReleaseCOMObjects(connectors);
3700

    
3701
            LMSymbols symbols = GetSymbols();
3702
            foreach (LMSymbol symbol in symbols)
3703
            {
3704
                string oid = symbol.get_GraphicOID().ToString();
3705
                DrawingObjectBase drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[oid];
3706
                if (drawingObject != null)
3707
                {
3708
                    double x1, y1, x2, y2;
3709
                    drawingObject.Range(out x1, out y1, out x2, out y2);
3710
                    itemRange.Add(new double[] { x1, y1, x2, y2 });
3711
                }
3712

    
3713
                ReleaseCOMObjects(symbol);
3714
            }
3715
            ReleaseCOMObjects(symbols);
3716
        }
3717

    
3718
        private void MoveSegmentBestLocation(string oid, double[] segmentRange, List<double[]> allRanges)
3719
        {
3720
            double minValue = Math.Min(segmentRange[2] - segmentRange[0], segmentRange[3] - segmentRange[1]);
3721
            double maxValue = Math.Max(segmentRange[2] - segmentRange[0], segmentRange[3] - segmentRange[1]);
3722

    
3723
            double maxX = 0, maxY = 0;
3724
            maxX = maxValue * 10;
3725
            maxY = minValue * 10;
3726

    
3727
            double move = minValue / 10d;
3728
            double textGap = minValue / 3d;
3729
            segmentRange = new double[] { segmentRange[0] - textGap, segmentRange[1] - textGap, segmentRange[2] + textGap, segmentRange[3] + textGap };
3730

    
3731

    
3732
            List<double[]> containRanges = new List<double[]>();
3733
            double[] findRange = new double[] {
3734
            segmentRange[0] - maxX, segmentRange[1] - maxY,
3735
            segmentRange[2] + maxX, segmentRange[3] + maxY};
3736

    
3737
            foreach (var range in allRanges)
3738
                if (SPPIDUtil.IsOverlap(findRange, range))
3739
                    containRanges.Add(range);
3740

    
3741
            double movePointX = 0, movePointY = 0, distance = double.MaxValue;
3742
            for (double x = 0; x < maxX; x = x + move)
3743
                for (double y = 0; y < maxY; y = y + move)
3744
                    for (int i = 0; i < 4; i++)
3745
                    {
3746
                        double tempX = 0d, tempY = 0d;
3747
                        switch (i)
3748
                        {
3749
                            case 0:
3750
                                tempX = x;
3751
                                tempY = y;
3752
                                break;
3753
                            case 1:
3754
                                tempX = -x;
3755
                                tempY = y;
3756
                                break;
3757
                            case 2:
3758
                                tempX = -x;
3759
                                tempY = -y;
3760
                                break;
3761
                            case 3:
3762
                                tempX = x;
3763
                                tempY = -y;
3764
                                break;
3765
                            default:
3766
                                break;
3767
                        }
3768

    
3769
                        bool result = true;
3770
                        double[] movedRange = new double[] { segmentRange[0] + tempX, segmentRange[1] + tempY, segmentRange[2] + tempX, segmentRange[3] + tempY };
3771
                        foreach (double[] range in containRanges)
3772
                        {
3773
                            if (SPPIDUtil.IsOverlap(range, movedRange))
3774
                            {
3775
                                result = false;
3776
                                break;
3777
                            }
3778
                        }
3779

    
3780
                        if (result)
3781
                        {
3782
                            //double tempDistance = Utils.CalcDistance(new double[] { 0, 0, 0 }, new double[] { tempX, tempY, 0 });
3783
                            double tempDistance = SPPIDUtil.CalcPointToPointdDistance(0, 0, tempX, tempY);
3784
                            bool bChange = false;
3785
                            if (distance > tempDistance)
3786
                                bChange = true;
3787
                            else if (distance.Equals(tempDistance) && (movePointX.Equals(0d) || movePointY.Equals(0d)))
3788
                                bChange = true;
3789

    
3790
                            if (bChange)
3791
                            {
3792
                                distance = tempDistance;
3793
                                movePointX = tempX;
3794
                                movePointY = tempY;
3795
                            }
3796
                        }
3797
                    }
3798

    
3799
            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[oid] as DependencyObject;
3800
            if (dependency != null)
3801
            {
3802
                radApp.ActiveSelectSet.RemoveAll();
3803
                radApp.ActiveSelectSet.Add(dependency);
3804
                Ingr.RAD2D.Transform transform = dependency.GetTransform();
3805
                transform.DefineByMove2d(movePointX, movePointY);
3806
                radApp.ActiveSelectSet.Transform(transform, true);
3807
                radApp.ActiveSelectSet.RemoveAll();
3808
            }
3809
        }
3810

    
3811
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
3812
        {
3813
            LMConnector targetConnector = null;
3814
            Symbol targetSymbol = targetObj as Symbol;
3815
            Symbol connectedSymbol = connectedObj as Symbol;
3816
            Line targetLine = targetObj as Line;
3817
            Line connectedLine = connectedObj as Line;
3818
            if (targetSymbol != null && connectedSymbol != null)
3819
            {
3820
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
3821
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
3822

    
3823
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
3824
                {
3825
                    if (connector.get_ItemStatus() != "Active")
3826
                        continue;
3827

    
3828
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
3829
                    {
3830
                        targetConnector = connector;
3831
                        break;
3832
                    }
3833
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
3834
                    {
3835
                        targetConnector = connector;
3836
                        break;
3837
                    }
3838
                }
3839

    
3840
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
3841
                {
3842
                    if (connector.get_ItemStatus() != "Active")
3843
                        continue;
3844

    
3845
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
3846
                    {
3847
                        targetConnector = connector;
3848
                        break;
3849
                    }
3850
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
3851
                    {
3852
                        targetConnector = connector;
3853
                        break;
3854
                    }
3855
                }
3856

    
3857
                ReleaseCOMObjects(targetLMSymbol);
3858
                ReleaseCOMObjects(connectedLMSymbol);
3859
            }
3860
            else if (targetLine != null && connectedLine != null)
3861
            {
3862
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
3863
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
3864

    
3865
                if (targetModelItem != null && targetModelItem.get_ItemStatus() == "Active" && connectedModelItem != null && connectedModelItem.get_ItemStatus() == "Active")
3866
                {
3867
                    foreach (LMRepresentation rep in targetModelItem.Representations)
3868
                    {
3869
                        if (targetConnector != null)
3870
                            break;
3871

    
3872
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3873
                        {
3874
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3875

    
3876
                            if (IsConnected(_LMConnector, connectedModelItem))
3877
                                targetConnector = _LMConnector;
3878
                            else
3879
                                ReleaseCOMObjects(_LMConnector);
3880
                        }
3881
                    }
3882

    
3883
                    ReleaseCOMObjects(targetModelItem);
3884
                }
3885
            }
3886
            else
3887
            {
3888
                LMSymbol connectedLMSymbol = null;
3889
                if (connectedSymbol != null)
3890
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
3891
                else if (targetSymbol != null)
3892
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
3893
                else
3894
                {
3895

    
3896
                }
3897
                LMModelItem targetModelItem = null;
3898
                if (targetLine != null)
3899
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
3900
                else if (connectedLine != null)
3901
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
3902
                else
3903
                {
3904

    
3905
                }
3906
                if (connectedLMSymbol != null && targetModelItem != null)
3907
                {
3908
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
3909
                    {
3910
                        if (connector.get_ItemStatus() != "Active")
3911
                            continue;
3912

    
3913
                        if (IsConnected(connector, targetModelItem))
3914
                        {
3915
                            targetConnector = connector;
3916
                            break;
3917
                        }
3918
                    }
3919

    
3920
                    if (targetConnector == null)
3921
                    {
3922
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
3923
                        {
3924
                            if (connector.get_ItemStatus() != "Active")
3925
                                continue;
3926

    
3927
                            if (IsConnected(connector, targetModelItem))
3928
                            {
3929
                                targetConnector = connector;
3930
                                break;
3931
                            }
3932
                        }
3933
                    }
3934
                }
3935

    
3936
            }
3937

    
3938
            return targetConnector;
3939
        }
3940

    
3941
        private double[] GetSegmentPoint(object targetObj, object connObj, LMConnector targetConnector, out SegmentLocation location)
3942
        {
3943
            double[] result = null;
3944
            Line targetLine = targetObj as Line;
3945
            Symbol targetSymbol = targetObj as Symbol;
3946
            Line connLine = connObj as Line;
3947
            Symbol connSymbol = connObj as Symbol;
3948
            location = SegmentLocation.None;
3949
            if (Convert.ToBoolean(targetConnector.get_IsZeroLength()))
3950
            {
3951
                result = GetConnectorVertices(targetConnector)[0];
3952
                if (targetSymbol != null && connSymbol != null)
3953
                {
3954
                    SlopeType slopeType = SPPIDUtil.CalcSlope(targetSymbol.SPPID.SPPID_X, targetSymbol.SPPID.SPPID_Y, connSymbol.SPPID.SPPID_X, connSymbol.SPPID.SPPID_Y);
3955
                    result = new double[] { result[0], result[1] };
3956
                    if (slopeType == SlopeType.HORIZONTAL)
3957
                        location = SegmentLocation.Up;
3958
                    else if (slopeType == SlopeType.VERTICAL)
3959
                        location = SegmentLocation.Right;
3960
                }
3961
                else if (targetLine != null)
3962
                {
3963
                    result = new double[] { result[0], result[1] };
3964
                    if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3965
                        location = SegmentLocation.Up;
3966
                    else if (targetLine.SlopeType == SlopeType.VERTICAL)
3967
                        location = SegmentLocation.Right;
3968
                }
3969
                else if (connLine != null)
3970
                {
3971
                    result = new double[] { result[0], result[1] };
3972
                    if (connLine.SlopeType == SlopeType.HORIZONTAL)
3973
                        location = SegmentLocation.Up;
3974
                    else if (connLine.SlopeType == SlopeType.VERTICAL)
3975
                        location = SegmentLocation.Right;
3976
                }
3977
            }
3978
            else
3979
            {
3980
                if (targetObj.GetType() == typeof(Line) && connObj.GetType() == typeof(Line))
3981
                {
3982
                    Line line = connObj as Line;
3983
                    LMConnector connectedConnector = null;
3984
                    int connIndex = 0;
3985
                    LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3986
                    FindConnectedConnector(targetConnector, modelItem, ref connectedConnector, ref connIndex);
3987

    
3988
                    List<double[]> vertices = GetConnectorVertices(targetConnector);
3989

    
3990
                    ReleaseCOMObjects(modelItem);
3991
                    ReleaseCOMObjects(connectedConnector);
3992

    
3993
                    if (vertices.Count > 0)
3994
                    {
3995
                        if (connIndex == 1)
3996
                            result = vertices[0];
3997
                        else if (connIndex == 2)
3998
                            result = vertices[vertices.Count - 1];
3999

    
4000
                        if (targetLine.SlopeType == SlopeType.HORIZONTAL)
4001
                        {
4002
                            result = new double[] { result[0], result[1] };
4003
                            location = SegmentLocation.Up;
4004
                            if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X < targetLine.SPPID.END_X)
4005
                                location = location | SegmentLocation.Right;
4006
                            else if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X > targetLine.SPPID.END_X)
4007
                                location = location | SegmentLocation.Left;
4008
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X < targetLine.SPPID.END_X)
4009
                                location = location | SegmentLocation.Left;
4010
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X > targetLine.SPPID.END_X)
4011
                                location = location | SegmentLocation.Right;
4012
                        }
4013
                        else if (targetLine.SlopeType == SlopeType.VERTICAL)
4014
                        {
4015
                            result = new double[] { result[0], result[1] };
4016
                            location = SegmentLocation.Right;
4017
                            if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y < targetLine.SPPID.END_Y)
4018
                                location = location | SegmentLocation.Up;
4019
                            else if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y > targetLine.SPPID.END_Y)
4020
                                location = location | SegmentLocation.Down;
4021
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y < targetLine.SPPID.END_Y)
4022
                                location = location | SegmentLocation.Down;
4023
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y > targetLine.SPPID.END_Y)
4024
                                location = location | SegmentLocation.Up;
4025
                        }
4026

    
4027
                    }
4028
                }
4029
                else
4030
                {
4031
                    Log.Write("error in GetSegemtPoint");
4032
                }
4033
            }
4034

    
4035
            return result;
4036
        }
4037

    
4038
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
4039
        {
4040
            bool result = false;
4041

    
4042
            foreach (LMRepresentation rep in modelItem.Representations)
4043
            {
4044
                if (result)
4045
                    break;
4046

    
4047
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
4048
                {
4049
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
4050

    
4051
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
4052
                        connector.ConnectItem1SymbolObject != null &&
4053
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
4054
                    {
4055
                        result = true;
4056
                        ReleaseCOMObjects(_LMConnector);
4057
                        break;
4058
                    }
4059
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
4060
                        connector.ConnectItem2SymbolObject != null &&
4061
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
4062
                    {
4063
                        result = true;
4064
                        ReleaseCOMObjects(_LMConnector);
4065
                        break;
4066
                    }
4067
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
4068
                        connector.ConnectItem1SymbolObject != null &&
4069
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
4070
                    {
4071
                        result = true;
4072
                        ReleaseCOMObjects(_LMConnector);
4073
                        break;
4074
                    }
4075
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
4076
                        connector.ConnectItem2SymbolObject != null &&
4077
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
4078
                    {
4079
                        result = true;
4080
                        ReleaseCOMObjects(_LMConnector);
4081
                        break;
4082
                    }
4083

    
4084
                    ReleaseCOMObjects(_LMConnector);
4085
                }
4086
            }
4087

    
4088

    
4089
            return result;
4090
        }
4091

    
4092
        private void FindConnectedConnector(LMConnector connector, LMModelItem modelItem, ref LMConnector connectedConnector, ref int connectorIndex)
4093
        {
4094
            foreach (LMRepresentation rep in modelItem.Representations)
4095
            {
4096
                if (connectedConnector != null)
4097
                    break;
4098

    
4099
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
4100
                {
4101
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
4102

    
4103
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
4104
                        connector.ConnectItem1SymbolObject != null &&
4105
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
4106
                    {
4107
                        connectedConnector = _LMConnector;
4108
                        connectorIndex = 1;
4109
                        break;
4110
                    }
4111
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
4112
                        connector.ConnectItem2SymbolObject != null &&
4113
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
4114
                    {
4115
                        connectedConnector = _LMConnector;
4116
                        connectorIndex = 2;
4117
                        break;
4118
                    }
4119
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
4120
                        connector.ConnectItem1SymbolObject != null &&
4121
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
4122
                    {
4123
                        connectedConnector = _LMConnector;
4124
                        connectorIndex = 1;
4125
                        break;
4126
                    }
4127
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
4128
                        connector.ConnectItem2SymbolObject != null &&
4129
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
4130
                    {
4131
                        connectedConnector = _LMConnector;
4132
                        connectorIndex = 2;
4133
                        break;
4134
                    }
4135

    
4136
                    if (connectedConnector == null)
4137
                        ReleaseCOMObjects(_LMConnector);
4138
                }
4139
            }
4140
        }
4141

    
4142
        /// <summary>
4143
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
4144
        /// </summary>
4145
        /// <param name="modelItemID1"></param>
4146
        /// <param name="modelItemID2"></param>
4147
        private void JoinRun(string modelId1, string modelId2, ref string survivorId, bool IsSameConnector = true)
4148
        {
4149
            try
4150
            {
4151
                LMModelItem modelItem1 = dataSource.GetModelItem(modelId1);
4152
                LMConnector connector1 = GetLMConnectorFirst(modelId1);
4153
                List<double[]> vertices1 = null;
4154
                string graphicOID1 = string.Empty;
4155
                if (connector1 != null)
4156
                {
4157
                    vertices1 = GetConnectorVertices(connector1);
4158
                    graphicOID1 = connector1.get_GraphicOID();
4159
                }
4160
                _LMAItem item1 = modelItem1.AsLMAItem();
4161
                ReleaseCOMObjects(connector1);
4162
                connector1 = null;
4163

    
4164
                LMModelItem modelItem2 = dataSource.GetModelItem(modelId2);
4165
                LMConnector connector2 = GetLMConnectorFirst(modelId2);
4166
                List<double[]> vertices2 = null;
4167
                string graphicOID2 = string.Empty;
4168
                if (connector2 != null)
4169
                {
4170
                    vertices2 = GetConnectorVertices(connector2);
4171
                    graphicOID2 = connector2.get_GraphicOID();
4172
                }
4173
                _LMAItem item2 = modelItem2.AsLMAItem();
4174
                ReleaseCOMObjects(connector2);
4175
                connector2 = null;
4176

    
4177
                // item2가 item1으로 조인
4178
                _placement.PIDJoinRuns(ref item1, ref item2);
4179
                item1.Commit();
4180
                item2.Commit();
4181

    
4182
                string beforeID = string.Empty;
4183
                string afterID = string.Empty;
4184

    
4185
                if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() != "Active")
4186
                {
4187
                    beforeID = modelItem2.Id;
4188
                    afterID = modelItem1.Id;
4189
                    survivorId = afterID;
4190
                }
4191
                else if (modelItem1.get_ItemStatus() != "Active" && modelItem2.get_ItemStatus() == "Active")
4192
                {
4193
                    beforeID = modelItem1.Id;
4194
                    afterID = modelItem2.Id;
4195
                    survivorId = afterID;
4196
                }
4197
                else if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() == "Active")
4198
                {
4199
                    int model1Cnt = GetConnectorCount(modelId1);
4200
                    int model2Cnt = GetConnectorCount(modelId2);
4201
                    if (model1Cnt == 0)
4202
                    {
4203
                        beforeID = modelItem1.Id;
4204
                        afterID = modelItem2.Id;
4205
                        survivorId = afterID;
4206
                    }
4207
                    else if (model2Cnt == 0)
4208
                    {
4209
                        beforeID = modelItem2.Id;
4210
                        afterID = modelItem1.Id;
4211
                        survivorId = afterID;
4212
                    }
4213
                    else
4214
                        survivorId = null;
4215
                }
4216
                else
4217
                {
4218
                    Log.Write("잘못된 경우");
4219
                    survivorId = null;
4220
                }
4221

    
4222
                if (!string.IsNullOrEmpty(beforeID) && !string.IsNullOrEmpty(afterID))
4223
                {
4224
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, beforeID);
4225
                    foreach (var line in lines)
4226
                        line.SPPID.ModelItemId = afterID;
4227
                }
4228

    
4229
                ReleaseCOMObjects(modelItem1);
4230
                ReleaseCOMObjects(item1);
4231
                ReleaseCOMObjects(modelItem2);
4232
                ReleaseCOMObjects(item2);
4233
            }
4234
            catch (Exception ex)
4235
            {
4236
                Log.Write("Join Error");
4237
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
4238
            }
4239
        }
4240

    
4241
        private bool IsModelingEndBreak(Symbol symbol1, Symbol symbol2)
4242
        {
4243
            bool result = false;
4244
            List<EndBreak> endBreaks = document.EndBreaks.FindAll(x =>
4245
           (x.OWNER == symbol1.UID || x.OWNER == symbol2.UID) &&
4246
           (x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbol1.UID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbol2.UID));
4247

    
4248
            foreach (var item in endBreaks)
4249
            {
4250
                if (!string.IsNullOrEmpty(item.SPPID.RepresentationId))
4251
                {
4252
                    result = true;
4253
                    break;
4254
                }
4255
            }
4256

    
4257
            return result;
4258
        }
4259
        private List<string> FindOtherModelItemBySymbolWhereTypePipeRun(LMSymbol symbol, string modelId)
4260
        {
4261
            List<string> temp = new List<string>();
4262
            List<LMConnector> connectors = new List<LMConnector>();
4263
            foreach (LMConnector connector in symbol.Avoid1Connectors)
4264
            {
4265
                if (connector.get_ItemStatus() != "Active")
4266
                    continue;
4267

    
4268
                LMModelItem modelItem = connector.ModelItemObject;
4269
                LMSymbol connOtherSymbol = FindOtherConnectedSymbol(connector);
4270
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId && !temp.Contains(modelItem.Id))
4271
                    temp.Add(modelItem.Id);
4272

    
4273
                if (temp.Contains(modelItem.Id) &&
4274
                    connOtherSymbol != null &&
4275
                    connOtherSymbol.get_RepresentationType() == "Branch" &&
4276
                    Convert.ToBoolean(connector.get_IsZeroLength()))
4277
                    temp.Remove(modelItem.Id);
4278

    
4279

    
4280
                if (temp.Contains(modelItem.Id))
4281
                    connectors.Add(connector);
4282
                ReleaseCOMObjects(connOtherSymbol);
4283
                connOtherSymbol = null;
4284
                ReleaseCOMObjects(modelItem);
4285
                modelItem = null;
4286
            }
4287

    
4288
            foreach (LMConnector connector in symbol.Avoid2Connectors)
4289
            {
4290
                if (connector.get_ItemStatus() != "Active")
4291
                    continue;
4292

    
4293
                LMModelItem modelItem = connector.ModelItemObject;
4294
                LMSymbol connOtherSymbol = FindOtherConnectedSymbol(connector);
4295
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId && !temp.Contains(modelItem.Id))
4296
                    temp.Add(modelItem.Id);
4297

    
4298
                if (temp.Contains(modelItem.Id) &&
4299
                    connOtherSymbol != null &&
4300
                    connOtherSymbol.get_RepresentationType() == "Branch" &&
4301
                    Convert.ToBoolean(connector.get_IsZeroLength()))
4302
                    temp.Remove(modelItem.Id);
4303

    
4304
                if (temp.Contains(modelItem.Id))
4305
                    connectors.Add(connector);
4306
                ReleaseCOMObjects(connOtherSymbol);
4307
                connOtherSymbol = null;
4308
                ReleaseCOMObjects(modelItem);
4309
                modelItem = null;
4310
            }
4311

    
4312

    
4313
            List<string> result = new List<string>();
4314
            string originalName = GetSPPIDFileName(modelId);
4315
            foreach (var connector in connectors)
4316
            {
4317
                string fileName = GetSPPIDFileName(connector.ModelItemID);
4318
                if (originalName == fileName)
4319
                    result.Add(connector.ModelItemID);
4320
                else
4321
                {
4322
                    if (document.LINES.Find(x => x.SPPID.ModelItemId == connector.ModelItemID) == null && Convert.ToBoolean(connector.get_IsZeroLength()))
4323
                        result.Add(connector.ModelItemID);
4324
                    else
4325
                    {
4326
                        Line line1 = document.LINES.Find(x => x.SPPID.ModelItemId == modelId);
4327
                        Line line2 = document.LINES.Find(x => x.SPPID.ModelItemId == connector.ModelItemID.ToString());
4328
                        if (line1 != null && line2 != null && line1.TYPE == line2.TYPE)
4329
                            result.Add(connector.ModelItemID);
4330
                    }
4331
                }
4332
            }
4333

    
4334
            foreach (var connector in connectors)
4335
                ReleaseCOMObjects(connector);
4336

    
4337
            return result;
4338

    
4339

    
4340
            LMSymbol FindOtherConnectedSymbol(LMConnector connector)
4341
            {
4342
                LMSymbol findResult = null;
4343
                if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.Id != symbol.Id && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
4344
                    findResult = connector.ConnectItem1SymbolObject;
4345
                else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.Id != symbol.Id && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
4346
                    findResult = connector.ConnectItem2SymbolObject;
4347

    
4348
                return findResult;
4349
            }
4350
        }
4351

    
4352
        /// <summary>
4353
        /// PipeRun의 좌표를 가져오는 메서드
4354
        /// </summary>
4355
        /// <param name="modelId"></param>
4356
        /// <returns></returns>
4357
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId, bool ContainZeroLength = true)
4358
        {
4359
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
4360
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
4361

    
4362
            if (modelItem != null)
4363
            {
4364
                foreach (LMRepresentation rep in modelItem.Representations)
4365
                {
4366
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
4367
                    {
4368
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
4369
                        if (!ContainZeroLength && Convert.ToBoolean(_LMConnector.get_IsZeroLength()))
4370
                        {
4371
                            ReleaseCOMObjects(_LMConnector);
4372
                            _LMConnector = null;
4373
                            continue;
4374
                        }
4375
                        connectorVertices.Add(_LMConnector, new List<double[]>());
4376
                        dynamic OID = rep.get_GraphicOID().ToString();
4377
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
4378
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
4379
                        int verticesCount = lineStringGeometry.VertexCount;
4380
                        double[] vertices = null;
4381
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
4382
                        for (int i = 0; i < verticesCount; i++)
4383
                        {
4384
                            double x = 0;
4385
                            double y = 0;
4386
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
4387
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
4388
                        }
4389
                    }
4390
                }
4391

    
4392
                ReleaseCOMObjects(modelItem);
4393
            }
4394

    
4395
            return connectorVertices;
4396
        }
4397

    
4398
        private List<double[]> GetConnectorVertices(LMConnector connector)
4399
        {
4400
            List<double[]> vertices = new List<double[]>();
4401
            if (connector != null)
4402
            {
4403
                dynamic OID = connector.get_GraphicOID().ToString();
4404
                DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
4405
                if (drawingObject != null)
4406
                {
4407
                    Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
4408
                    int verticesCount = lineStringGeometry.VertexCount;
4409
                    double[] value = null;
4410
                    lineStringGeometry.GetVertices(ref verticesCount, ref value);
4411
                    for (int i = 0; i < verticesCount; i++)
4412
                    {
4413
                        double x = 0;
4414
                        double y = 0;
4415
                        lineStringGeometry.GetVertex(i + 1, ref x, ref y);
4416
                        vertices.Add(new double[] { x, y });
4417
                    }
4418
                }
4419
            }
4420
            return vertices;
4421
        }
4422

    
4423
        private double GetConnectorDistance(LMConnector connector)
4424
        {
4425
            double result = 0;
4426
            List<double[]> vertices = new List<double[]>();
4427
            if (connector != null)
4428
            {
4429
                dynamic OID = connector.get_GraphicOID().ToString();
4430
                DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
4431
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
4432
                int verticesCount = lineStringGeometry.VertexCount;
4433
                double[] value = null;
4434
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
4435
                for (int i = 0; i < verticesCount; i++)
4436
                {
4437
                    double x = 0;
4438
                    double y = 0;
4439
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
4440
                    vertices.Add(new double[] { x, y });
4441
                    if (vertices.Count > 1)
4442
                    {
4443
                        result += SPPIDUtil.CalcPointToPointdDistance(vertices[vertices.Count - 2][0], vertices[vertices.Count - 2][1], x, y);
4444
                    }
4445
                }
4446
            }
4447
            return result;
4448
        }
4449
        private double[] GetConnectorRange(LMConnector connector)
4450
        {
4451
            double[] result = null;
4452
            List<double[]> vertices = new List<double[]>();
4453
            if (connector != null)
4454
            {
4455
                dynamic OID = connector.get_GraphicOID().ToString();
4456
                DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
4457
                double minX = 0;
4458
                double minY = 0;
4459
                double maxX = 0;
4460
                double maxY = 0;
4461

    
4462
                drawingObject.Range(out minX, out minY, out maxX, out maxY);
4463
                result = new double[] { minX, minY, maxX, maxY };
4464
            }
4465
            return result;
4466
        }
4467
        private List<double[]> GetConnectorVertices(dynamic graphicOID)
4468
        {
4469
            List<double[]> vertices = null;
4470
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID];
4471
            if (drawingObject != null)
4472
            {
4473
                vertices = new List<double[]>();
4474
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
4475
                int verticesCount = lineStringGeometry.VertexCount;
4476
                double[] value = null;
4477
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
4478
                for (int i = 0; i < verticesCount; i++)
4479
                {
4480
                    double x = 0;
4481
                    double y = 0;
4482
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
4483
                    vertices.Add(new double[] { x, y });
4484
                }
4485
            }
4486
            return vertices;
4487
        }
4488
        /// <summary>
4489
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
4490
        /// </summary>
4491
        /// <param name="connectorVertices"></param>
4492
        /// <param name="connX"></param>
4493
        /// <param name="connY"></param>
4494
        /// <returns></returns>
4495
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
4496
        {
4497
            double length = double.MaxValue;
4498
            LMConnector targetConnector = null;
4499
            foreach (var item in connectorVertices)
4500
            {
4501
                List<double[]> points = item.Value;
4502
                for (int i = 0; i < points.Count - 1; i++)
4503
                {
4504
                    double[] point1 = points[i];
4505
                    double[] point2 = points[i + 1];
4506
                    double x1 = Math.Min(point1[0], point2[0]);
4507
                    double y1 = Math.Min(point1[1], point2[1]);
4508
                    double x2 = Math.Max(point1[0], point2[0]);
4509
                    double y2 = Math.Max(point1[1], point2[1]);
4510

    
4511
                    if ((x1 <= connX && x2 >= connX) ||
4512
                        (y1 <= connY && y2 >= connY))
4513
                    {
4514
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
4515
                        if (length >= distance)
4516
                        {
4517
                            targetConnector = item.Key;
4518
                            length = distance;
4519
                        }
4520

    
4521
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
4522
                        if (length >= distance)
4523
                        {
4524
                            targetConnector = item.Key;
4525
                            length = distance;
4526
                        }
4527
                    }
4528
                }
4529
            }
4530

    
4531
            // 못찾았을때.
4532
            length = double.MaxValue;
4533
            if (targetConnector == null)
4534
            {
4535
                foreach (var item in connectorVertices)
4536
                {
4537
                    List<double[]> points = item.Value;
4538

    
4539
                    foreach (double[] point in points)
4540
                    {
4541
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
4542
                        if (length >= distance)
4543
                        {
4544
                            targetConnector = item.Key;
4545
                            length = distance;
4546
                        }
4547
                    }
4548
                }
4549
            }
4550

    
4551
            return targetConnector;
4552
        }
4553

    
4554
        private LMConnector FindTargetLMConnectorForBranch(Line line, Line targetLine, ref double x, ref double y)
4555
        {
4556
            Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
4557
            if (vertices.Count == 0)
4558
                return null;
4559

    
4560
            double length = double.MaxValue;
4561
            LMConnector targetConnector = null;
4562
            double[] resultPoint = null;
4563
            List<double[]> targetVertices = null;
4564

    
4565
            // Vertices 포인트에 제일 가까운곳
4566
            foreach (var item in vertices)
4567
            {
4568
                List<double[]> points = item.Value;
4569
                for (int i = 0; i < points.Count; i++)
4570
                {
4571
                    double[] point = points[i];
4572
                    double tempX = point[0];
4573
                    double tempY = point[1];
4574

    
4575
                    double distance = SPPIDUtil.CalcPointToPointdDistance(tempX, tempY, x, y);
4576
                    if (length >= distance)
4577
                    {
4578
                        targetConnector = item.Key;
4579
                        length = distance;
4580
                        resultPoint = point;
4581
                        targetVertices = item.Value;
4582
                    }
4583
                }
4584
            }
4585

    
4586
            // Vertices Cross에 제일 가까운곳
4587
            foreach (var item in vertices)
4588
            {
4589
                List<double[]> points = item.Value;
4590
                for (int i = 0; i < points.Count - 1; i++)
4591
                {
4592
                    double[] point1 = points[i];
4593
                    double[] point2 = points[i + 1];
4594

    
4595
                    double maxLineX = Math.Max(point1[0], point2[0]);
4596
                    double minLineX = Math.Min(point1[0], point2[0]);
4597
                    double maxLineY = Math.Max(point1[1], point2[1]);
4598
                    double minLineY = Math.Min(point1[1], point2[1]);
4599

    
4600
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
4601

    
4602
                    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]);
4603
                    if (crossingPoint != null)
4604
                    {
4605
                        double distance = SPPIDUtil.CalcPointToPointdDistance(crossingPoint[0], crossingPoint[1], x, y);
4606
                        if (length >= distance)
4607
                        {
4608
                            if (slope == SlopeType.Slope &&
4609
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
4610
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
4611
                            {
4612
                                targetConnector = item.Key;
4613
                                length = distance;
4614
                                resultPoint = crossingPoint;
4615
                                targetVertices = item.Value;
4616
                            }
4617
                            else if (slope == SlopeType.HORIZONTAL &&
4618
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
4619
                            {
4620
                                targetConnector = item.Key;
4621
                                length = distance;
4622
                                resultPoint = crossingPoint;
4623
                                targetVertices = item.Value;
4624
                            }
4625
                            else if (slope == SlopeType.VERTICAL &&
4626
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
4627
                            {
4628
                                targetConnector = item.Key;
4629
                                length = distance;
4630
                                resultPoint = crossingPoint;
4631
                                targetVertices = item.Value;
4632
                            }
4633
                        }
4634
                    }
4635
                }
4636
            }
4637

    
4638
            foreach (var item in vertices)
4639
                if (item.Key != null && item.Key != targetConnector)
4640
                    ReleaseCOMObjects(item.Key);
4641

    
4642
            if (SPPIDUtil.IsBranchLine(line, targetLine))
4643
            {
4644
                double tempResultX = resultPoint[0];
4645
                double tempResultY = resultPoint[1];
4646
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
4647

    
4648
                GridSetting gridSetting = GridSetting.GetInstance();
4649

    
4650
                for (int i = 0; i < targetVertices.Count; i++)
4651
                {
4652
                    double[] point = targetVertices[i];
4653
                    double tempX = targetVertices[i][0];
4654
                    double tempY = targetVertices[i][1];
4655
                    SPPIDUtil.ConvertGridPoint(ref tempX, ref tempY);
4656
                    if (tempX == tempResultX && tempY == tempResultY)
4657
                    {
4658
                        if (i == 0)
4659
                        {
4660
                            LMSymbol connSymbol = targetConnector.ConnectItem1SymbolObject;
4661
                            bool containZeroLength = false;
4662
                            if (connSymbol != null)
4663
                            {
4664
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
4665
                                {
4666
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
4667
                                        containZeroLength = true;
4668
                                }
4669
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
4670
                                {
4671
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
4672
                                        containZeroLength = true;
4673
                                }
4674
                            }
4675

    
4676
                            if (connSymbol == null ||
4677
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
4678
                                containZeroLength)
4679
                            {
4680
                                bool bCalcX = false;
4681
                                bool bCalcY = false;
4682
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
4683
                                    bCalcX = true;
4684
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
4685
                                    bCalcY = true;
4686
                                else
4687
                                {
4688
                                    bCalcX = true;
4689
                                    bCalcY = true;
4690
                                }
4691

    
4692
                                if (bCalcX)
4693
                                {
4694
                                    double nextX = targetVertices[i + 1][0];
4695
                                    double newX = 0;
4696
                                    if (nextX > tempX)
4697
                                    {
4698
                                        newX = tempX + gridSetting.Length;
4699
                                        if (newX > nextX)
4700
                                            newX = (point[0] + nextX) / 2;
4701
                                    }
4702
                                    else
4703
                                    {
4704
                                        newX = tempX - gridSetting.Length;
4705
                                        if (newX < nextX)
4706
                                            newX = (point[0] + nextX) / 2;
4707
                                    }
4708
                                    resultPoint = new double[] { newX, resultPoint[1] };
4709
                                }
4710

    
4711
                                if (bCalcY)
4712
                                {
4713
                                    double nextY = targetVertices[i + 1][1];
4714
                                    double newY = 0;
4715
                                    if (nextY > tempY)
4716
                                    {
4717
                                        newY = tempY + gridSetting.Length;
4718
                                        if (newY > nextY)
4719
                                            newY = (point[1] + nextY) / 2;
4720
                                    }
4721
                                    else
4722
                                    {
4723
                                        newY = tempY - gridSetting.Length;
4724
                                        if (newY < nextY)
4725
                                            newY = (point[1] + nextY) / 2;
4726
                                    }
4727
                                    resultPoint = new double[] { resultPoint[0], newY };
4728
                                }
4729
                            }
4730
                        }
4731
                        else if (i == targetVertices.Count - 1)
4732
                        {
4733
                            LMSymbol connSymbol = targetConnector.ConnectItem2SymbolObject;
4734
                            bool containZeroLength = false;
4735
                            if (connSymbol != null)
4736
                            {
4737
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
4738
                                {
4739
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
4740
                                        containZeroLength = true;
4741
                                }
4742
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
4743
                                {
4744
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
4745
                                        containZeroLength = true;
4746
                                }
4747
                            }
4748

    
4749
                            if (connSymbol == null ||
4750
                                 (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
4751
                                containZeroLength)
4752
                            {
4753
                                bool bCalcX = false;
4754
                                bool bCalcY = false;
4755
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
4756
                                    bCalcX = true;
4757
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
4758
                                    bCalcY = true;
4759
                                else
4760
                                {
4761
                                    bCalcX = true;
4762
                                    bCalcY = true;
4763
                                }
4764

    
4765
                                if (bCalcX)
4766
                                {
4767
                                    double nextX = targetVertices[i - 1][0];
4768
                                    double newX = 0;
4769
                                    if (nextX > tempX)
4770
                                    {
4771
                                        newX = tempX + gridSetting.Length;
4772
                                        if (newX > nextX)
4773
                                            newX = (point[0] + nextX) / 2;
4774
                                    }
4775
                                    else
4776
                                    {
4777
                                        newX = tempX - gridSetting.Length;
4778
                                        if (newX < nextX)
4779
                                            newX = (point[0] + nextX) / 2;
4780
                                    }
4781
                                    resultPoint = new double[] { newX, resultPoint[1] };
4782
                                }
4783

    
4784
                                if (bCalcY)
4785
                                {
4786
                                    double nextY = targetVertices[i - 1][1];
4787
                                    double newY = 0;
4788
                                    if (nextY > tempY)
4789
                                    {
4790
                                        newY = tempY + gridSetting.Length;
4791
                                        if (newY > nextY)
4792
                                            newY = (point[1] + nextY) / 2;
4793
                                    }
4794
                                    else
4795
                                    {
4796
                                        newY = tempY - gridSetting.Length;
4797
                                        if (newY < nextY)
4798
                                            newY = (point[1] + nextY) / 2;
4799
                                    }
4800
                                    resultPoint = new double[] { resultPoint[0], newY };
4801
                                }
4802
                            }
4803
                        }
4804
                        break;
4805
                    }
4806
                }
4807
            }
4808

    
4809
            x = resultPoint[0];
4810
            y = resultPoint[1];
4811

    
4812
            return targetConnector;
4813
        }
4814

    
4815
        private LMConnector GetLMConnectorOnlyOne(string modelItemID)
4816
        {
4817
            LMConnector result = null;
4818
            List<LMConnector> connectors = new List<LMConnector>();
4819
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4820

    
4821
            if (modelItem != null)
4822
            {
4823
                foreach (LMRepresentation rep in modelItem.Representations)
4824
                {
4825
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
4826
                        connectors.Add(dataSource.GetConnector(rep.Id));
4827
                }
4828

    
4829
                ReleaseCOMObjects(modelItem);
4830
            }
4831

    
4832
            if (connectors.Count == 1)
4833
                result = connectors[0];
4834
            else
4835
                foreach (var item in connectors)
4836
                    ReleaseCOMObjects(item);
4837

    
4838
            return result;
4839
        }
4840

    
4841
        private LMConnector GetLMConnectorFirst(string modelItemID)
4842
        {
4843
            LMConnector result = null;
4844
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4845

    
4846
            if (modelItem != null)
4847
            {
4848
                foreach (LMRepresentation rep in modelItem.Representations)
4849
                {
4850
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" &&
4851
                        rep.Attributes["ItemStatus"].get_Value() == "Active")
4852
                    {
4853
                        LMConnector connector = dataSource.GetConnector(rep.Id);
4854
                        if (!Convert.ToBoolean(connector.get_IsZeroLength()))
4855
                        {
4856
                            result = connector;
4857
                            break;
4858
                        }
4859
                        else
4860
                        {
4861
                            ReleaseCOMObjects(connector);
4862
                            connector = null;
4863
                        }
4864
                    }
4865
                }
4866
                ReleaseCOMObjects(modelItem);
4867
                modelItem = null;
4868
            }
4869

    
4870
            return result;
4871
        }
4872

    
4873
        private int GetConnectorCount(string modelItemID)
4874
        {
4875
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4876
            int result = 0;
4877
            if (modelItem != null)
4878
            {
4879
                foreach (LMRepresentation rep in modelItem.Representations)
4880
                {
4881
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
4882
                        result++;
4883
                    ReleaseCOMObjects(rep);
4884
                }
4885
                ReleaseCOMObjects(modelItem);
4886
            }
4887

    
4888
            return result;
4889
        }
4890

    
4891
        public List<string> GetRepresentations(string modelItemID)
4892
        {
4893
            List<string> result = new List<string>(); ;
4894
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4895
            if (modelItem != null)
4896
            {
4897
                foreach (LMRepresentation rep in modelItem.Representations)
4898
                {
4899
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
4900
                        result.Add(rep.Id);
4901
                }
4902
                ReleaseCOMObjects(modelItem);
4903
            }
4904

    
4905
            return result;
4906
        }
4907

    
4908
        private void LineNumberModeling(LineNumber lineNumber)
4909
        {
4910
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
4911
            if (line != null)
4912
            {
4913
                double x = 0;
4914
                double y = 0;
4915
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
4916

    
4917
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
4918
                LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, x, y);
4919
                if (connectedLMConnector != null)
4920
                {
4921
                    Array points = new double[] { 0, x, y };
4922
                    lineNumber.SPPID.SPPID_X = x;
4923
                    lineNumber.SPPID.SPPID_Y = y;
4924
                    LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: false);
4925

    
4926
                    if (_LmLabelPresist != null)
4927
                    {
4928
                        _LmLabelPresist.Commit();
4929
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
4930
                        ReleaseCOMObjects(_LmLabelPresist);
4931
                    }
4932
                }
4933

    
4934
                foreach (var item in connectorVertices)
4935
                    ReleaseCOMObjects(item.Key);
4936
            }
4937
        }
4938
        private void LineNumberCorrectModeling(LineNumber lineNumber)
4939
        {
4940
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
4941
            if (line == null || line.SPPID.Vertices == null)
4942
                return;
4943

    
4944
            if (!string.IsNullOrEmpty(lineNumber.SPPID.RepresentationId))
4945
            {
4946
                LMLabelPersist removeLabel = dataSource.GetLabelPersist(lineNumber.SPPID.RepresentationId);
4947
                if (removeLabel != null)
4948
                {
4949
                    lineNumber.SPPID.SPPID_X = removeLabel.get_XCoordinate();
4950
                    lineNumber.SPPID.SPPID_Y = removeLabel.get_YCoordinate();
4951

    
4952
                    GridSetting gridSetting = GridSetting.GetInstance();
4953
                    LMConnector connector = dataSource.GetConnector(removeLabel.RepresentationID);
4954

    
4955
                    double[] labelRange = null;
4956
                    GetSPPIDSymbolRange(removeLabel, ref labelRange);
4957
                    List<double[]> vertices = GetConnectorVertices(connector);
4958

    
4959
                    double[] resultStart = null;
4960
                    double[] resultEnd = null;
4961
                    double distance = double.MaxValue;
4962
                    for (int i = 0; i < vertices.Count - 1; i++)
4963
                    {
4964
                        double[] startPoint = vertices[i];
4965
                        double[] endPoint = vertices[i + 1];
4966
                        foreach (var item in line.SPPID.Vertices)
4967
                        {
4968
                            double[] lineStartPoint = item[0];
4969
                            double[] lineEndPoint = item[item.Count - 1];
4970

    
4971
                            double tempDistance = SPPIDUtil.CalcPointToPointdDistance(startPoint[0], startPoint[1], lineStartPoint[0], lineStartPoint[1]) +
4972
                                SPPIDUtil.CalcPointToPointdDistance(endPoint[0], endPoint[1], lineEndPoint[0], lineEndPoint[1]);
4973
                            if (tempDistance < distance)
4974
                            {
4975
                                distance = tempDistance;
4976
                                resultStart = startPoint;
4977
                                resultEnd = endPoint;
4978
                            }
4979
                            tempDistance = SPPIDUtil.CalcPointToPointdDistance(startPoint[0], startPoint[1], lineEndPoint[0], lineEndPoint[1]) +
4980
                                SPPIDUtil.CalcPointToPointdDistance(endPoint[0], endPoint[1], lineStartPoint[0], lineStartPoint[1]);
4981
                            if (tempDistance < distance)
4982
                            {
4983
                                distance = tempDistance;
4984
                                resultStart = startPoint;
4985
                                resultEnd = endPoint;
4986
                            }
4987
                        }
4988
                    }
4989

    
4990
                    if (resultStart != null && resultEnd != null)
4991
                    {
4992
                        SlopeType slope = SPPIDUtil.CalcSlope(resultStart[0], resultStart[1], resultEnd[0], resultEnd[1]);
4993
                        double lineStartX = 0;
4994
                        double lineStartY = 0;
4995
                        double lineEndX = 0;
4996
                        double lineEndY = 0;
4997
                        double lineNumberX = 0;
4998
                        double lineNumberY = 0;
4999
                        SPPIDUtil.ConvertPointBystring(line.STARTPOINT, ref lineStartX, ref lineStartY);
5000
                        SPPIDUtil.ConvertPointBystring(line.ENDPOINT, ref lineEndX, ref lineEndY);
5001

    
5002
                        double lineX = (lineStartX + lineEndX) / 2;
5003
                        double lineY = (lineStartY + lineEndY) / 2;
5004
                        lineNumberX = (lineNumber.X1 + lineNumber.X2) / 2;
5005
                        lineNumberY = (lineNumber.Y1 + lineNumber.Y2) / 2;
5006

    
5007
                        double SPPIDCenterX = (resultStart[0] + resultEnd[0]) / 2;
5008
                        double SPPIDCenterY = (resultStart[1] + resultEnd[1]) / 2;
5009
                        double labelCenterX = (labelRange[0] + labelRange[2]) / 2;
5010
                        double labelCenterY = (labelRange[1] + labelRange[3]) / 2;
5011

    
5012
                        double offsetX = 0;
5013
                        double offsetY = 0;
5014
                        if (slope == SlopeType.HORIZONTAL)
5015
                        {
5016
                            // Line Number 아래
5017
                            if (lineY < lineNumberY)
5018
                            {
5019
                                offsetX = labelCenterX - SPPIDCenterX;
5020
                                offsetY = labelRange[3] - SPPIDCenterY + gridSetting.Length;
5021
                                MoveLineNumber(lineNumber, offsetX, offsetY);
5022
                            }
5023
                            // Line Number 위
5024
                            else
5025
                            {
5026
                                offsetX = labelCenterX - SPPIDCenterX;
5027
                                offsetY = labelRange[1] - SPPIDCenterY - gridSetting.Length;
5028
                                MoveLineNumber(lineNumber, offsetX, offsetY);
5029
                            }
5030
                        }
5031
                        else if (slope == SlopeType.VERTICAL)
5032
                        {
5033
                            // Line Number 오르쪽
5034
                            if (lineX < lineNumberX)
5035
                            {
5036
                                offsetX = labelRange[0] - SPPIDCenterX - gridSetting.Length;
5037
                                offsetY = labelCenterY - SPPIDCenterY;
5038
                                MoveLineNumber(lineNumber, offsetX, offsetY);
5039
                            }
5040
                            // Line Number 왼쪽
5041
                            else
5042
                            {
5043
                                offsetX = labelRange[2] - SPPIDCenterX + gridSetting.Length;
5044
                                offsetY = labelCenterY - SPPIDCenterY;
5045
                                MoveLineNumber(lineNumber, offsetX, offsetY);
5046
                            }
5047
                        }
5048

    
5049
                        if (offsetY != 0 || offsetY != 0)
5050
                        {
5051
                            if (connector.ConnectItem1SymbolObject != null &&
5052
                                connector.ConnectItem1SymbolObject.get_RepresentationType() == "OPC")
5053
                            {
5054
                                Ingr.RAD2D.Symbol2d symbol = radApp.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()];
5055

    
5056
                                double x1, y1, x2, y2, originX, originY;
5057
                                symbol.Range(out x1, out y1, out x2, out y2);
5058
                                symbol.GetOrigin(out originX, out originY);
5059
                                if (originX < lineNumber.SPPID.SPPID_X)
5060
                                    offsetX = -1 * (originX + gridSetting.Length * 30 - labelCenterX);
5061
                                else
5062
                                    offsetX = -1 * (originX - gridSetting.Length * 30 - labelCenterX);
5063
                            }
5064
                            else if (connector.ConnectItem2SymbolObject != null &&
5065
                                    connector.ConnectItem2SymbolObject.get_RepresentationType() == "OPC")
5066
                            {
5067
                                Ingr.RAD2D.Symbol2d symbol = radApp.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()];
5068

    
5069
                                double x1, y1, x2, y2, originX, originY;
5070
                                symbol.Range(out x1, out y1, out x2, out y2);
5071
                                symbol.GetOrigin(out originX, out originY);
5072
                                if (originX < lineNumber.SPPID.SPPID_X)
5073
                                    offsetX = -1 * (originX + gridSetting.Length * 30 - labelCenterX);
5074
                                else
5075
                                    offsetX = -1 * (originX - gridSetting.Length * 30 - labelCenterX);
5076
                            }
5077

    
5078
                            radApp.ActiveSelectSet.RemoveAll();
5079
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[removeLabel.get_GraphicOID().ToString()] as DependencyObject;
5080
                            if (dependency != null)
5081
                            {
5082
                                radApp.ActiveSelectSet.Add(dependency);
5083
                                Ingr.RAD2D.Transform transform = dependency.GetTransform();
5084
                                transform.DefineByMove2d(-offsetX, -offsetY);
5085
                                radApp.ActiveSelectSet.Transform(transform, true);
5086
                                radApp.ActiveSelectSet.RemoveAll();
5087
                            }
5088
                        }
5089

    
5090
                        void MoveLineNumber(LineNumber moveLineNumber, double x, double y)
5091
                        {
5092
                            moveLineNumber.SPPID.SPPID_X = moveLineNumber.SPPID.SPPID_X - x;
5093
                            moveLineNumber.SPPID.SPPID_Y = moveLineNumber.SPPID.SPPID_Y - y;
5094
                        }
5095
                    }
5096

    
5097

    
5098
                    ReleaseCOMObjects(connector);
5099
                    connector = null;
5100
                }
5101

    
5102
                ReleaseCOMObjects(removeLabel);
5103
                removeLabel = null;
5104
            }
5105
        }
5106
        /// <summary>
5107
        /// Flow Mark Modeling
5108
        /// </summary>
5109
        /// <param name="line"></param>
5110
        private void FlowMarkModeling(Line line)
5111
        {
5112
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
5113
            {
5114
                LMConnector connector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
5115
                if (connector != null)
5116
                {
5117
                    string mappingPath = _ETCSetting.FlowMarkSymbolPath;
5118
                    List<double[]> vertices = GetConnectorVertices(connector);
5119
                    vertices = vertices.FindAll(x => x[0] > 0 && x[1] > 0);
5120
                    double[] point = vertices[vertices.Count - 1];
5121
                    Array array = new double[] { 0, point[0], point[1] };
5122
                    LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mappingPath, ref array, null, null, LabeledItem: connector.AsLMRepresentation());
5123
                    if (_LMLabelPersist != null)
5124
                    {
5125
                        _LMLabelPersist.Commit();
5126
                        FlowMarkRepIds.Add(_LMLabelPersist.Id);
5127
                        ReleaseCOMObjects(_LMLabelPersist);
5128
                    }
5129
                }
5130
            }
5131
        }
5132

    
5133
        /// <summary>
5134
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
5135
        /// </summary>
5136
        /// <param name="lineNumber"></param>
5137
        private void InputLineNumberAttribute(LineNumber lineNumber, List<string> endLine)
5138
        {
5139
            lineNumber.ATTRIBUTES.Sort(SortAttribute);
5140
            int SortAttribute(BaseModel.Attribute a, BaseModel.Attribute b)
5141
            {
5142
                if (a.ATTRIBUTE == "Tag Seq No")
5143
                    return 1;
5144
                else if (b.ATTRIBUTE == "Tag Seq No")
5145
                    return -1;
5146

    
5147
                return 0;
5148
            }
5149

    
5150
            foreach (LineRun run in lineNumber.RUNS)
5151
            {
5152
                foreach (var item in run.RUNITEMS)
5153
                {
5154
                    if (item.GetType() == typeof(Line))
5155
                    {
5156
                        Line line = item as Line;
5157
                        if (line != null && !endLine.Contains(line.SPPID.ModelItemId))
5158
                        {
5159
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
5160
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
5161
                            {
5162
                                foreach (var attribute in lineNumber.ATTRIBUTES)
5163
                                {
5164
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
5165
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
5166
                                    {
5167
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
5168
                                        if (mapping.SPPIDATTRIBUTENAME == "OperFluidCode" && !string.IsNullOrEmpty(attribute.VALUE))
5169
                                        {
5170
                                            LMAAttribute _FluidSystemAttribute = _LMModelItem.Attributes["FluidSystem"];
5171
                                            if (_FluidSystemAttribute != null)
5172
                                            {
5173
                                                DataTable dt = SPPID_DB.GetFluidSystemInfo(attribute.VALUE);
5174
                                                if (dt.Rows.Count == 1)
5175
                                                {
5176
                                                    string fluidSystem = dt.Rows[0]["CODELIST_TEXT"].ToString();
5177
                                                    if (DBNull.Value.Equals(_FluidSystemAttribute.get_Value()))
5178
                                                        _FluidSystemAttribute.set_Value(fluidSystem);
5179
                                                    else if (_FluidSystemAttribute.get_Value() != fluidSystem)
5180
                                                        _FluidSystemAttribute.set_Value(fluidSystem);
5181

    
5182
                                                    if (_LMAAttribute != null)
5183
                                                    {
5184
                                                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
5185
                                                            _LMAAttribute.set_Value(attribute.VALUE);
5186
                                                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
5187
                                                            _LMAAttribute.set_Value(attribute.VALUE);
5188
                                                    }
5189
                                                }
5190
                                                if (dt != null)
5191
                                                    dt.Dispose();
5192
                                            }
5193
                                        }
5194
                                        else if (mapping.SPPIDATTRIBUTENAME.Equals("NominalDiameter") && !string.IsNullOrEmpty(attribute.VALUE) && _LMAAttribute != null)
5195
                                        {
5196
                                            DataRow[] rows = nominalDiameterTable.Select(string.Format("MetricStr = '{0}' OR InchStr = '{0}'", attribute.VALUE));
5197

    
5198
                                            if (rows.Length.Equals(1))
5199
                                            {
5200
                                                if (_ETCSetting.UnitSetting != null && _ETCSetting.UnitSetting.Equals("Metric"))
5201
                                                    attribute.VALUE = rows[0]["MetricStr"].ToString();
5202
                                                else
5203
                                                    attribute.VALUE = rows[0]["InchStr"].ToString();
5204

    
5205
                                                if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
5206
                                                    _LMAAttribute.set_Value(attribute.VALUE);
5207
                                                else if (_LMAAttribute.get_Value() != attribute.VALUE)
5208
                                                    _LMAAttribute.set_Value(attribute.VALUE);
5209
                                            }
5210
                                        }
5211
                                        else if (_LMAAttribute != null)
5212
                                        {
5213
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
5214
                                                _LMAAttribute.set_Value(attribute.VALUE);
5215
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
5216
                                                _LMAAttribute.set_Value(attribute.VALUE);
5217
                                        }
5218
                                    }
5219
                                }
5220
                                _LMModelItem.Commit();
5221
                            }
5222
                            if (_LMModelItem != null)
5223
                                ReleaseCOMObjects(_LMModelItem);
5224
                            endLine.Add(line.SPPID.ModelItemId);
5225
                        }
5226
                    }
5227
                }
5228
            }
5229
        }
5230

    
5231
        /// <summary>
5232
        /// Symbol Attribute 입력 메서드
5233
        /// </summary>
5234
        /// <param name="item"></param>
5235
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
5236
        {
5237
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
5238
            string sRep = null;
5239
            string sModelID = null;
5240
            if (targetItem.GetType() == typeof(Symbol))
5241
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
5242
            else if (targetItem.GetType() == typeof(Equipment))
5243
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
5244
            else if (targetItem.GetType() == typeof(Line))
5245
                sModelID = ((Line)targetItem).SPPID.ModelItemId;
5246

    
5247
            if (!string.IsNullOrEmpty(sRep))
5248
            {
5249
                LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
5250
                LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
5251
                LMAAttributes _Attributes = _LMModelItem.Attributes;
5252

    
5253
                foreach (var item in targetAttributes)
5254
                {
5255
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
5256
                    if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
5257
                    {
5258
                        if (!mapping.IsText)
5259
                        {
5260
                            LMAAttribute _LMAAttribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
5261
                            if (mapping.SPPIDATTRIBUTENAME.Equals("NominalDiameter") && !string.IsNullOrEmpty(item.VALUE) && _LMAAttribute != null)
5262
                            {
5263
                                DataRow[] rows = nominalDiameterTable.Select(string.Format("MetricStr = '{0}' OR InchStr = '{0}'", item.VALUE));
5264

    
5265
                                if (rows.Length.Equals(1))
5266
                                {
5267
                                    if (_ETCSetting.UnitSetting != null && _ETCSetting.UnitSetting.Equals("Metric"))
5268
                                        item.VALUE = rows[0]["MetricStr"].ToString();
5269
                                    else
5270
                                        item.VALUE = rows[0]["InchStr"].ToString();
5271

    
5272
                                    if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
5273
                                        _LMAAttribute.set_Value(item.VALUE);
5274
                                    else if (_LMAAttribute.get_Value() != item.VALUE)
5275
                                        _LMAAttribute.set_Value(item.VALUE);
5276
                                }
5277
                            }
5278
                            else if (_LMAAttribute != null)
5279
                            {
5280
                                _LMAAttribute.set_Value(item.VALUE);
5281
                                // OPC 일경우 Attribute 저장
5282
                                if (targetItem.GetType() == typeof(Symbol))
5283
                                {
5284
                                    Symbol symbol = targetItem as Symbol;
5285
                                    if (symbol.TYPE == "Piping OPC's" || symbol.TYPE == "Instrument OPC's")
5286
                                        symbol.SPPID.Attributes.Add(new string[] { mapping.SPPIDATTRIBUTENAME, item.VALUE });
5287
                                }
5288
                            }
5289
                        }
5290
                        else
5291
                            DefaultTextModeling(item.VALUE, _LMSymbol.get_XCoordinate(), _LMSymbol.get_YCoordinate());
5292
                    }
5293
                }
5294
                _LMModelItem.Commit();
5295

    
5296
                ReleaseCOMObjects(_Attributes);
5297
                ReleaseCOMObjects(_LMModelItem);
5298
                ReleaseCOMObjects(_LMSymbol);
5299
            }
5300
            else if (!string.IsNullOrEmpty(sModelID))
5301
            {
5302
                LMModelItem _LMModelItem = dataSource.GetModelItem(sModelID);
5303
                LMAAttributes _Attributes = _LMModelItem.Attributes;
5304

    
5305
                foreach (var item in targetAttributes)
5306
                {
5307
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
5308
                    if (mapping == null)
5309
                        continue;
5310
                    
5311
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
5312
                    if (mapping.SPPIDATTRIBUTENAME == "OperFluidCode" && !string.IsNullOrEmpty(item.VALUE))
5313
                    {
5314
                        LMAAttribute _FluidSystemAttribute = _LMModelItem.Attributes["FluidSystem"];
5315
                        if (_FluidSystemAttribute != null)
5316
                        {
5317
                            DataTable dt = SPPID_DB.GetFluidSystemInfo(item.VALUE);
5318
                            if (dt.Rows.Count == 1)
5319
                            {
5320
                                string fluidSystem = dt.Rows[0]["CODELIST_TEXT"].ToString();
5321
                                if (DBNull.Value.Equals(_FluidSystemAttribute.get_Value()))
5322
                                    _FluidSystemAttribute.set_Value(fluidSystem);
5323
                                else if (_FluidSystemAttribute.get_Value() != fluidSystem)
5324
                                    _FluidSystemAttribute.set_Value(fluidSystem);
5325

    
5326
                                if (_LMAAttribute != null)
5327
                                {
5328
                                    if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
5329
                                        _LMAAttribute.set_Value(item.VALUE);
5330
                                    else if (_LMAAttribute.get_Value() != item.VALUE)
5331
                                        _LMAAttribute.set_Value(item.VALUE);
5332
                                }
5333
                            }
5334
                            if (dt != null)
5335
                                dt.Dispose();
5336
                        }
5337
                    }
5338
                    else if (mapping.SPPIDATTRIBUTENAME.Equals("NominalDiameter") && !string.IsNullOrEmpty(item.VALUE) && _LMAAttribute != null)
5339
                    {
5340
                        DataRow[] rows = nominalDiameterTable.Select(string.Format("MetricStr = '{0}' OR InchStr = '{0}'", item.VALUE));
5341

    
5342
                        if (rows.Length.Equals(1))
5343
                        {
5344
                            if (_ETCSetting.UnitSetting != null && _ETCSetting.UnitSetting.Equals("Metric"))
5345
                                item.VALUE = rows[0]["MetricStr"].ToString();
5346
                            else
5347
                                item.VALUE = rows[0]["InchStr"].ToString();
5348

    
5349
                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
5350
                                _LMAAttribute.set_Value(item.VALUE);
5351
                            else if (_LMAAttribute.get_Value() != item.VALUE)
5352
                                _LMAAttribute.set_Value(item.VALUE);
5353
                        }
5354
                    }
5355
                    else if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
5356
                    {
5357
                        if (!mapping.IsText)
5358
                        {
5359
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
5360
                            if (_Attribute != null)
5361
                                _Attribute.set_Value(item.VALUE);
5362
                        }
5363
                    }
5364
                }
5365
                _LMModelItem.Commit();
5366

    
5367
                ReleaseCOMObjects(_Attributes);
5368
                ReleaseCOMObjects(_LMModelItem);
5369
            }
5370
        }
5371

    
5372
        /// <summary>
5373
        /// Input SpecBreak Attribute
5374
        /// </summary>
5375
        /// <param name="specBreak"></param>
5376
        private void InputSpecBreakAttribute(SpecBreak specBreak)
5377
        {
5378
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
5379
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
5380

    
5381
            if (upStreamObj != null &&
5382
                downStreamObj != null)
5383
            {
5384
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
5385

    
5386
                if (targetLMConnector != null)
5387
                {
5388
                    foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
5389
                    {
5390
                        string symbolPath = _LMLabelPersist.get_FileName();
5391
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
5392
                        if (mapping != null)
5393
                        {
5394
                            BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
5395
                            if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
5396
                            {
5397
                                string[] values = attribute.VALUE.Split(new char[] { ',' });
5398
                                if (values.Length == 2)
5399
                                {
5400
                                    string upStreamValue = values[0];
5401
                                    string downStreamValue = values[1];
5402

    
5403
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
5404
                                }
5405
                            }
5406
                        }
5407
                    }
5408

    
5409
                    ReleaseCOMObjects(targetLMConnector);
5410
                }
5411
            }
5412

    
5413

    
5414
            #region 내부에서만 쓰는 메서드
5415
            void InputAttributeForSpecBreak(object _upStreamObj, object _downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
5416
            {
5417
                Symbol upStreamSymbol = _upStreamObj as Symbol;
5418
                Line upStreamLine = _upStreamObj as Line;
5419
                Symbol downStreamSymbol = _downStreamObj as Symbol;
5420
                Line downStreamLine = _downStreamObj as Line;
5421
                // 둘다 Line일 경우
5422
                if (upStreamLine != null && downStreamLine != null)
5423
                {
5424
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
5425
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
5426
                }
5427
                // 둘다 Symbol일 경우
5428
                else if (upStreamSymbol != null && downStreamSymbol != null)
5429
                {
5430
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
5431
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
5432
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
5433

    
5434
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
5435
                    {
5436
                        if (connector.get_ItemStatus() != "Active")
5437
                            continue;
5438

    
5439
                        if (connector.Id != zeroLenthConnector.Id)
5440
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
5441
                    }
5442

    
5443
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
5444
                    {
5445
                        if (connector.get_ItemStatus() != "Active")
5446
                            continue;
5447

    
5448
                        if (connector.Id != zeroLenthConnector.Id)
5449
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
5450
                    }
5451

    
5452
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
5453
                    {
5454
                        if (connector.get_ItemStatus() != "Active")
5455
                            continue;
5456

    
5457
                        if (connector.Id != zeroLenthConnector.Id)
5458
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
5459
                    }
5460

    
5461
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
5462
                    {
5463
                        if (connector.get_ItemStatus() != "Active")
5464
                            continue;
5465

    
5466
                        if (connector.Id != zeroLenthConnector.Id)
5467
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
5468
                    }
5469

    
5470
                    ReleaseCOMObjects(zeroLenthConnector);
5471
                    ReleaseCOMObjects(upStreamLMSymbol);
5472
                    ReleaseCOMObjects(downStreamLMSymbol);
5473
                }
5474
                else if (upStreamSymbol != null && downStreamLine != null)
5475
                {
5476
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
5477
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
5478
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
5479

    
5480
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
5481
                    {
5482
                        if (connector.get_ItemStatus() != "Active")
5483
                            continue;
5484

    
5485
                        if (connector.Id == zeroLenthConnector.Id)
5486
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
5487
                    }
5488

    
5489
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
5490
                    {
5491
                        if (connector.get_ItemStatus() != "Active")
5492
                            continue;
5493

    
5494
                        if (connector.Id == zeroLenthConnector.Id)
5495
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
5496
                    }
5497

    
5498
                    ReleaseCOMObjects(zeroLenthConnector);
5499
                    ReleaseCOMObjects(upStreamLMSymbol);
5500
                }
5501
                else if (upStreamLine != null && downStreamSymbol != null)
5502
                {
5503
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
5504
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
5505
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
5506

    
5507
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
5508
                    {
5509
                        if (connector.get_ItemStatus() != "Active")
5510
                            continue;
5511

    
5512
                        if (connector.Id == zeroLenthConnector.Id)
5513
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
5514
                    }
5515

    
5516
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
5517
                    {
5518
                        if (connector.get_ItemStatus() != "Active")
5519
                            continue;
5520

    
5521
                        if (connector.Id == zeroLenthConnector.Id)
5522
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
5523
                    }
5524

    
5525
                    ReleaseCOMObjects(zeroLenthConnector);
5526
                    ReleaseCOMObjects(downStreamLMSymbol);
5527
                }
5528
            }
5529

    
5530
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
5531
            {
5532
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
5533
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
5534
                {
5535
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
5536
                    if (_LMAAttribute != null)
5537
                    {
5538
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
5539
                            _LMAAttribute.set_Value(value);
5540
                        else if (_LMAAttribute.get_Value() != value)
5541
                            _LMAAttribute.set_Value(value);
5542
                    }
5543

    
5544
                    _LMModelItem.Commit();
5545
                }
5546
                if (_LMModelItem != null)
5547
                    ReleaseCOMObjects(_LMModelItem);
5548
            }
5549

    
5550
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
5551
            {
5552
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
5553
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
5554
                {
5555
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
5556
                    if (_LMAAttribute != null)
5557
                    {
5558
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
5559
                            _LMAAttribute.set_Value(value);
5560
                        else if (_LMAAttribute.get_Value() != value)
5561
                            _LMAAttribute.set_Value(value);
5562
                    }
5563

    
5564
                    _LMModelItem.Commit();
5565
                }
5566
                if (_LMModelItem != null)
5567
                    ReleaseCOMObjects(_LMModelItem);
5568
            }
5569
            #endregion
5570
        }
5571

    
5572
        private void InputEndBreakAttribute(EndBreak endBreak)
5573
        {
5574
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
5575
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
5576

    
5577
            if ((ownerObj.GetType() == typeof(Symbol) && connectedItem.GetType() == typeof(Line)) ||
5578
                (ownerObj.GetType() == typeof(Line) && connectedItem.GetType() == typeof(Symbol)))
5579
            {
5580
                LMLabelPersist labelPersist = dataSource.GetLabelPersist(endBreak.SPPID.RepresentationId);
5581
                if (labelPersist != null)
5582
                {
5583
                    LMRepresentation representation = labelPersist.RepresentationObject;
5584
                    if (representation != null)
5585
                    {
5586
                        LMConnector connector = dataSource.GetConnector(representation.Id);
5587
                        LMModelItem ZeroLengthModelItem = connector.ModelItemObject;
5588
                        string modelItemID = connector.ModelItemID;
5589
                        if (Convert.ToBoolean(connector.get_IsZeroLength()))
5590
                        {
5591
                            List<string> modelItemIDs = new List<string>();
5592
                            if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
5593
                            {
5594
                                LMSymbol symbol = connector.ConnectItem1SymbolObject;
5595
                                foreach (LMConnector item in symbol.Connect1Connectors)
5596
                                {
5597
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
5598
                                        modelItemIDs.Add(item.ModelItemID);
5599
                                    ReleaseCOMObjects(item);
5600
                                }
5601
                                foreach (LMConnector item in symbol.Connect2Connectors)
5602
                                {
5603
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
5604
                                        modelItemIDs.Add(item.ModelItemID);
5605
                                    ReleaseCOMObjects(item);
5606
                                }
5607
                                ReleaseCOMObjects(symbol);
5608
                                symbol = null;
5609
                            }
5610
                            else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
5611
                            {
5612
                                LMSymbol symbol = connector.ConnectItem2SymbolObject;
5613
                                foreach (LMConnector item in symbol.Connect1Connectors)
5614
                                {
5615
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
5616
                                        modelItemIDs.Add(item.ModelItemID);
5617
                                    ReleaseCOMObjects(item);
5618
                                }
5619
                                foreach (LMConnector item in symbol.Connect2Connectors)
5620
                                {
5621
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
5622
                                        modelItemIDs.Add(item.ModelItemID);
5623
                                    ReleaseCOMObjects(item);
5624
                                }
5625
                                ReleaseCOMObjects(symbol);
5626
                                symbol = null;
5627
                            }
5628

    
5629
                            modelItemIDs = modelItemIDs.Distinct().ToList();
5630
                            if (modelItemIDs.Count == 1)
5631
                            {
5632
                                LMModelItem modelItem = dataSource.GetModelItem(modelItemIDs[0]);
5633
                                LMConnector onlyOne = GetLMConnectorOnlyOne(modelItem.Id);
5634
                                if (onlyOne != null && Convert.ToBoolean(onlyOne.get_IsZeroLength()))
5635
                                {
5636
                                    bool result = false;
5637
                                    foreach (LMLabelPersist loop in onlyOne.LabelPersists)
5638
                                    {
5639
                                        if (document.EndBreaks.Find(x => x.SPPID.RepresentationId == loop.RepresentationID) != null)
5640
                                            result = true;
5641
                                        ReleaseCOMObjects(loop);
5642
                                    }
5643

    
5644
                                    if (result)
5645
                                    {
5646
                                        object value = modelItem.Attributes["TagSequenceNo"].get_Value();
5647
                                        ZeroLengthModelItem.Attributes["TagSequenceNo"].set_Value(value);
5648
                                        //object value2 = modelItem.Attributes["SubUnitNumber"].get_Value();
5649
                                        //ZeroLengthModelItem.Attributes["SubUnitNumber"].set_Value(value2);
5650
                                        ZeroLengthModelItem.Commit();
5651
                                    }
5652
                                    else
5653
                                    {
5654
                                        List<string> loopModelItems = new List<string>();
5655
                                        if (onlyOne.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
5656
                                        {
5657
                                            LMSymbol _symbol = onlyOne.ConnectItem1SymbolObject;
5658
                                            foreach (LMConnector loop in _symbol.Connect1Connectors)
5659
                                            {
5660
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
5661
                                                    loopModelItems.Add(loop.ModelItemID);
5662
                                                ReleaseCOMObjects(loop);
5663
                                            }
5664
                                            foreach (LMConnector loop in _symbol.Connect2Connectors)
5665
                                            {
5666
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
5667
                                                    loopModelItems.Add(loop.ModelItemID);
5668
                                                ReleaseCOMObjects(loop);
5669
                                            }
5670
                                            ReleaseCOMObjects(_symbol);
5671
                                            _symbol = null;
5672
                                        }
5673
                                        else if (onlyOne.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
5674
                                        {
5675
                                            LMSymbol _symbol = onlyOne.ConnectItem2SymbolObject;
5676
                                            foreach (LMConnector loop in _symbol.Connect1Connectors)
5677
                                            {
5678
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
5679
                                                    loopModelItems.Add(loop.ModelItemID);
5680
                                                ReleaseCOMObjects(loop);
5681
                                            }
5682
                                            foreach (LMConnector loop in _symbol.Connect2Connectors)
5683
                                            {
5684
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
5685
                                                    loopModelItems.Add(loop.ModelItemID);
5686
                                                ReleaseCOMObjects(loop);
5687
                                            }
5688
                                            ReleaseCOMObjects(_symbol);
5689
                                            _symbol = null;
5690
                                        }
5691

    
5692
                                        loopModelItems = loopModelItems.Distinct().ToList();
5693
                                        if (loopModelItems.Count == 1)
5694
                                        {
5695
                                            LMModelItem loopModelItem = dataSource.GetModelItem(loopModelItems[0]);
5696
                                            object value = loopModelItem.Attributes["TagSequenceNo"].get_Value();
5697
                                            //object value2 = loopModelItem.Attributes["SubUnitNumber"].get_Value();
5698
                                            modelItem.Attributes["TagSequenceNo"].set_Value(value);
5699
                                            //modelItem.Attributes["SubUnitNumber"].set_Value(value2);
5700
                                            modelItem.Commit();
5701
                                            ZeroLengthModelItem.Attributes["TagSequenceNo"].set_Value(value);
5702
                                            //ZeroLengthModelItem.Attributes["SubUnitNumber"].set_Value(value2);
5703
                                            ZeroLengthModelItem.Commit();
5704

    
5705
                                            ReleaseCOMObjects(loopModelItem);
5706
                                            loopModelItem = null;
5707
                                        }
5708
                                    }
5709
                                }
5710
                                else
5711
                                {
5712
                                    object value = modelItem.Attributes["TagSequenceNo"].get_Value();
5713
                                    ZeroLengthModelItem.Attributes["TagSequenceNo"].set_Value(value);
5714
                                    //object value2 = modelItem.Attributes["SubUnitNumber"].get_Value();
5715
                                    //ZeroLengthModelItem.Attributes["SubUnitNumber"].set_Value(value2);
5716
                                    ZeroLengthModelItem.Commit();
5717
                                }
5718
                                ReleaseCOMObjects(modelItem);
5719
                                modelItem = null;
5720
                                ReleaseCOMObjects(onlyOne);
5721
                                onlyOne = null;
5722
                            }
5723
                        }
5724
                        ReleaseCOMObjects(connector);
5725
                        connector = null;
5726
                        ReleaseCOMObjects(ZeroLengthModelItem);
5727
                        ZeroLengthModelItem = null;
5728
                    }
5729
                    ReleaseCOMObjects(representation);
5730
                    representation = null;
5731
                }
5732
                ReleaseCOMObjects(labelPersist);
5733
                labelPersist = null;
5734
            }
5735
        }
5736

    
5737
        /// <summary>
5738
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
5739
        /// </summary>
5740
        /// <param name="text"></param>
5741
        private void NormalTextModeling(Text text)
5742
        {
5743
            LMSymbol _LMSymbol = null;
5744

    
5745
            LMItemNote _LMItemNote = null;
5746
            LMAAttribute _LMAAttribute = null;
5747

    
5748
            double x = 0;
5749
            double y = 0;
5750
            double angle = text.ANGLE;
5751
            CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
5752

    
5753
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
5754
            text.SPPID.SPPID_X = x;
5755
            text.SPPID.SPPID_Y = y;
5756

    
5757
            _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, Rotation: angle);
5758
            if (_LMSymbol != null)
5759
            {
5760
                _LMSymbol.Commit();
5761
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5762
                if (_LMItemNote != null)
5763
                {
5764
                    _LMItemNote.Commit();
5765
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5766
                    if (_LMAAttribute != null)
5767
                    {
5768
                        _LMAAttribute.set_Value(text.VALUE);
5769
                        text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5770
                        _LMItemNote.Commit();
5771

    
5772

    
5773
                        double[] range = null;
5774
                        foreach (LMLabelPersist labelPersist in _LMSymbol.LabelPersists)
5775
                        {
5776
                            double[] temp = null;
5777
                            GetSPPIDSymbolRange(labelPersist, ref temp);
5778
                            if (temp != null)
5779
                            {
5780
                                if (range == null)
5781
                                    range = temp;
5782
                                else
5783
                                {
5784
                                    range = new double[] {
5785
                                            Math.Min(range[0], temp[0]),
5786
                                            Math.Min(range[1], temp[1]),
5787
                                            Math.Max(range[2], temp[2]),
5788
                                            Math.Max(range[3], temp[3])
5789
                                        };
5790
                                }
5791
                            }
5792
                        }
5793
                        text.SPPID.Range = range;
5794

    
5795
                        if (_LMAAttribute != null)
5796
                            ReleaseCOMObjects(_LMAAttribute);
5797
                        if (_LMItemNote != null)
5798
                            ReleaseCOMObjects(_LMItemNote);
5799
                    }
5800

    
5801
                    TextCorrectModeling(text);
5802
                }
5803
            }
5804
            if (_LMSymbol != null)
5805
                ReleaseCOMObjects(_LMSymbol);
5806
        }
5807

    
5808
        private void DefaultTextModeling(string value, double x, double y)
5809
        {
5810
            LMSymbol _LMSymbol = null;
5811

    
5812
            LMItemNote _LMItemNote = null;
5813
            LMAAttribute _LMAAttribute = null;
5814

    
5815
            _LMSymbol = _placement.PIDPlaceSymbol(_ETCSetting.TextSymbolPath, x, y, Rotation: 0);
5816
            if (_LMSymbol != null)
5817
            {
5818
                _LMSymbol.Commit();
5819
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5820
                if (_LMItemNote != null)
5821
                {
5822
                    _LMItemNote.Commit();
5823
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5824
                    if (_LMAAttribute != null)
5825
                    {
5826
                        _LMAAttribute.set_Value(value);
5827
                        _LMItemNote.Commit();
5828

    
5829
                        if (_LMAAttribute != null)
5830
                            ReleaseCOMObjects(_LMAAttribute);
5831
                        if (_LMItemNote != null)
5832
                            ReleaseCOMObjects(_LMItemNote);
5833
                    }
5834
                }
5835
            }
5836
            if (_LMSymbol != null)
5837
                ReleaseCOMObjects(_LMSymbol);
5838
        }
5839

    
5840
        private void AssociationTextModeling(Text text)
5841
        {
5842
            LMSymbol _LMSymbol = null;
5843
            LMConnector connectedLMConnector = null;
5844
            object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
5845
            if (owner != null && (owner.GetType() == typeof(Symbol) || owner.GetType() == typeof(Equipment)))
5846
            {
5847
                Symbol symbol = owner as Symbol;
5848
                _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
5849
                if (_LMSymbol != null)
5850
                {
5851
                    foreach (BaseModel.Attribute attribute in symbol.ATTRIBUTES.FindAll(x => x.ASSOCITEM == text.UID))
5852
                    {
5853
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
5854
                        {
5855
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
5856

    
5857
                            if (mapping != null)
5858
                            {
5859
                                double x = 0;
5860
                                double y = 0;
5861

    
5862
                                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, mapping.Location);
5863
                                SPPIDUtil.ConvertGridPoint(ref x, ref y);
5864
                                Array array = new double[] { 0, x, y };
5865
                                text.SPPID.SPPID_X = x;
5866
                                text.SPPID.SPPID_Y = y;
5867
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
5868
                                if (_LMLabelPersist != null)
5869
                                {
5870
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
5871
                                    _LMLabelPersist.Commit();
5872
                                    ReleaseCOMObjects(_LMLabelPersist);
5873
                                }
5874
                            }
5875
                        }
5876
                    }
5877
                }
5878
            }
5879
            else if (owner != null && owner.GetType() == typeof(Line))
5880
            {
5881
                Line line = owner as Line;
5882
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
5883
                connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
5884

    
5885
                if (connectedLMConnector != null)
5886
                {
5887
                    foreach (BaseModel.Attribute attribute in line.ATTRIBUTES.FindAll(x => x.ASSOCITEM == text.UID))
5888
                    {
5889
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
5890
                        {
5891
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
5892

    
5893
                            if (mapping != null)
5894
                            {
5895
                                double x = 0;
5896
                                double y = 0;
5897
                                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, mapping.Location);
5898
                                SPPIDUtil.ConvertGridPoint(ref x, ref y);
5899
                                Array array = new double[] { 0, x, y };
5900

    
5901
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
5902
                                if (_LMLabelPersist != null)
5903
                                {
5904
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
5905
                                    _LMLabelPersist.Commit();
5906

    
5907
                                    DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_LMLabelPersist.get_GraphicOID().ToString()] as DependencyObject;
5908
                                    if (dependency != null)
5909
                                    {
5910
                                        radApp.ActiveSelectSet.RemoveAll();
5911
                                        radApp.ActiveSelectSet.Add(dependency);
5912
                                        Ingr.RAD2D.Transform transform = dependency.GetTransform();
5913
                                        transform.DefineByMove2d(x - _LMLabelPersist.get_XCoordinate(), y - _LMLabelPersist.get_YCoordinate());
5914
                                        radApp.ActiveSelectSet.Transform(transform, true);
5915
                                        radApp.ActiveSelectSet.RemoveAll();
5916
                                    }
5917

    
5918
                                    ReleaseCOMObjects(_LMLabelPersist);
5919
                                }
5920
                            }
5921
                        }
5922
                    }
5923
                }
5924
            }
5925
            if (_LMSymbol != null)
5926
                ReleaseCOMObjects(_LMSymbol);
5927
        }
5928

    
5929
        private void TextCorrectModeling(Text text)
5930
        {
5931
            if (text.SPPID.Range == null)
5932
                return;
5933

    
5934
            bool needRemodeling = false;
5935
            bool loop = true;
5936
            GridSetting gridSetting = GridSetting.GetInstance();
5937
            while (loop)
5938
            {
5939
                loop = false;
5940
                foreach (var overlapText in document.TEXTINFOS)
5941
                {
5942
                    if (overlapText.ASSOCIATION || overlapText == text || overlapText.SPPID.Range == null)
5943
                        continue;
5944

    
5945
                    if (SPPIDUtil.IsOverlap(overlapText.SPPID.Range, text.SPPID.Range))
5946
                    {
5947
                        double percentX = 0;
5948
                        double percentY = 0;
5949
                        if (overlapText.X1 <= text.X2 && overlapText.X2 >= text.X1)
5950
                        {
5951
                            double gapX = Math.Min(overlapText.X2, text.X2) - Math.Max(overlapText.X1, text.X1);
5952
                            percentX = Math.Max(gapX / (overlapText.X2 - overlapText.X1), gapX / (text.X2 - text.X1));
5953
                        }
5954
                        if (overlapText.Y1 <= text.Y2 && overlapText.Y2 >= text.Y1)
5955
                        {
5956
                            double gapY = Math.Min(overlapText.Y2, text.Y2) - Math.Max(overlapText.Y1, text.Y1);
5957
                            percentY = Math.Max(gapY / (overlapText.Y2 - overlapText.Y1), gapY / (text.Y2 - text.Y1));
5958
                        }
5959

    
5960
                        double tempX = 0;
5961
                        double tempY = 0;
5962
                        bool overlapX = false;
5963
                        bool overlapY = false;
5964
                        SPPIDUtil.CalcOverlap(text.SPPID.Range, overlapText.SPPID.Range, ref tempX, ref tempY, ref overlapX, ref overlapY);
5965
                        if (percentX >= percentY)
5966
                        {
5967
                            int count = Convert.ToInt32(tempY / gridSetting.Length) + 1;
5968
                            double move = gridSetting.Length * count;
5969
                            text.SPPID.SPPID_Y = text.SPPID.SPPID_Y - move;
5970
                            text.SPPID.Range = new double[] { text.SPPID.Range[0], text.SPPID.Range[1] - move, text.SPPID.Range[2], text.SPPID.Range[3] - move };
5971
                            needRemodeling = true;
5972
                            loop = true;
5973
                        }
5974
                        else
5975
                        {
5976
                            int count = Convert.ToInt32(tempX / gridSetting.Length) + 1;
5977
                            double move = gridSetting.Length * count;
5978
                            text.SPPID.SPPID_X = text.SPPID.SPPID_X + move;
5979
                            text.SPPID.Range = new double[] { text.SPPID.Range[0] + move, text.SPPID.Range[1], text.SPPID.Range[2] + move, text.SPPID.Range[3] };
5980
                            needRemodeling = true;
5981
                            loop = true;
5982
                        }
5983
                    }
5984
                }
5985
            }
5986

    
5987

    
5988
            if (needRemodeling)
5989
            {
5990
                LMSymbol symbol = dataSource.GetSymbol(text.SPPID.RepresentationId);
5991
                _placement.PIDRemovePlacement(symbol.AsLMRepresentation());
5992
                text.SPPID.RepresentationId = null;
5993

    
5994
                LMItemNote _LMItemNote = null;
5995
                LMAAttribute _LMAAttribute = null;
5996
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, text.SPPID.SPPID_X, text.SPPID.SPPID_Y, Rotation: text.ANGLE);
5997
                if (_LMSymbol != null)
5998
                {
5999
                    _LMSymbol.Commit();
6000
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
6001
                    if (_LMItemNote != null)
6002
                    {
6003
                        _LMItemNote.Commit();
6004
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
6005
                        if (_LMAAttribute != null)
6006
                        {
6007
                            _LMAAttribute.set_Value(text.VALUE);
6008
                            text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
6009
                            _LMItemNote.Commit();
6010

    
6011
                            ReleaseCOMObjects(_LMAAttribute);
6012
                            ReleaseCOMObjects(_LMItemNote);
6013
                        }
6014
                    }
6015
                }
6016

    
6017
                ReleaseCOMObjects(symbol);
6018
                symbol = null;
6019
                ReleaseCOMObjects(_LMItemNote);
6020
                _LMItemNote = null;
6021
                ReleaseCOMObjects(_LMAAttribute);
6022
                _LMAAttribute = null;
6023
                ReleaseCOMObjects(_LMSymbol);
6024
                _LMSymbol = null;
6025
            }
6026
        }
6027

    
6028
        private void AssociationTextCorrectModeling(Text text, List<Text> endTexts)
6029
        {
6030
            if (!string.IsNullOrEmpty(text.SPPID.RepresentationId))
6031
            {
6032
                List<Text> allTexts = new List<Text>();
6033
                LMLabelPersist targetLabel = dataSource.GetLabelPersist(text.SPPID.RepresentationId);
6034
                LMRepresentation representation = targetLabel.RepresentationObject;
6035
                Symbol symbol = document.SYMBOLS.Find(x => x.SPPID.RepresentationId == representation.Id);
6036
                if (targetLabel.RepresentationObject != null && symbol != null)
6037
                {
6038
                    double[] symbolRange = null;
6039
                    GetSPPIDSymbolRange(symbol, ref symbolRange, true, true);
6040
                    if (symbolRange != null)
6041
                    {
6042
                        foreach (LMLabelPersist labelPersist in representation.LabelPersists)
6043
                        {
6044
                            Text findText = document.TEXTINFOS.Find(x => x.SPPID.RepresentationId == labelPersist.AsLMRepresentation().Id && x.ASSOCIATION);
6045
                            if (findText != null)
6046
                            {
6047
                                double[] range = null;
6048
                                GetSPPIDSymbolRange(labelPersist, ref range);
6049
                                findText.SPPID.Range = range;
6050
                                allTexts.Add(findText);
6051
                            }
6052

    
6053
                            ReleaseCOMObjects(labelPersist);
6054
                        }
6055

    
6056
                        if (allTexts.Count > 0)
6057
                        {
6058
                            #region Sort Text By Y
6059
                            allTexts.Sort(SortTextByY);
6060
                            int SortTextByY(Text a, Text b)
6061
                            {
6062
                                return b.SPPID.Range[3].CompareTo(a.SPPID.Range[3]);
6063
                            }
6064
                            #endregion
6065

    
6066
                            #region 정렬하기전 방향
6067
                            List<Text> left = new List<Text>();
6068
                            List<Text> down = new List<Text>();
6069
                            List<Text> right = new List<Text>();
6070
                            List<Text> up = new List<Text>();
6071
                            List<List<Text>> sortTexts = new List<List<Text>>() { left, down, right, up };
6072
                            foreach (var loopText in allTexts)
6073
                            {
6074
                                double textCenterX = (loopText.X1 + loopText.X2) / 2;
6075
                                double textCenterY = (loopText.Y1 + loopText.Y2) / 2;
6076
                                double originX = 0;
6077
                                double originY = 0;
6078
                                SPPIDUtil.ConvertPointBystring(symbol.ORIGINALPOINT, ref originX, ref originY);
6079
                                double angle = SPPIDUtil.CalcAngle(textCenterX, textCenterY, originX, originY);
6080

    
6081
                                if (angle < 45)
6082
                                {
6083
                                    // Text 오른쪽
6084
                                    if (textCenterX > originX)
6085
                                        right.Add(loopText);
6086
                                    // Text 왼쪽
6087
                                    else
6088
                                        left.Add(loopText);
6089
                                }
6090
                                else
6091
                                {
6092
                                    // Text 아래쪽
6093
                                    if (textCenterY > originY)
6094
                                        down.Add(loopText);
6095
                                    // Text 위쪽
6096
                                    else
6097
                                        up.Add(loopText);
6098
                                }
6099
                            }
6100

    
6101
                            #endregion
6102

    
6103
                            foreach (var texts in sortTexts)
6104
                            {
6105
                                if (texts.Count == 0)
6106
                                    continue;
6107

    
6108
                                #region 첫번째 Text로 기준 맞춤
6109
                                for (int i = 0; i < texts.Count; i++)
6110
                                {
6111
                                    if (i != 0)
6112
                                    {
6113
                                        Text currentText = texts[i];
6114
                                        Text prevText = texts[i - 1];
6115
                                        double minY = prevText.SPPID.Range[1];
6116
                                        double centerPrevX = (prevText.SPPID.Range[0] + prevText.SPPID.Range[2]) / 2;
6117
                                        double centerX = (currentText.SPPID.Range[0] + currentText.SPPID.Range[2]) / 2;
6118
                                        double _gapX = centerX - centerPrevX;
6119
                                        double _gapY = currentText.SPPID.Range[3] - minY;
6120
                                        MoveText(currentText, _gapX, _gapY);
6121
                                    }
6122
                                }
6123
                                List<double> rangeMinX = texts.Select(loopX => loopX.SPPID.Range[0]).ToList();
6124
                                List<double> rangeMinY = texts.Select(loopX => loopX.SPPID.Range[1]).ToList();
6125
                                List<double> rangeMaxX = texts.Select(loopX => loopX.SPPID.Range[2]).ToList();
6126
                                List<double> rangeMaxY = texts.Select(loopX => loopX.SPPID.Range[3]).ToList();
6127
                                rangeMinX.Sort();
6128
                                rangeMinY.Sort();
6129
                                rangeMaxX.Sort();
6130
                                rangeMaxY.Sort();
6131
                                double allTextCenterX = (rangeMinX[0] + rangeMaxX[rangeMaxX.Count - 1]) / 2;
6132
                                double allTextCenterY = (rangeMinY[0] + rangeMaxY[rangeMaxY.Count - 1]) / 2;
6133
                                #endregion
6134
                                #region 정렬
6135
                                Text correctBySymbol = texts[0];
6136
                                double textCenterX = (correctBySymbol.X1 + correctBySymbol.X2) / 2;
6137
                                double textCenterY = (correctBySymbol.Y1 + correctBySymbol.Y2) / 2;
6138
                                double originX = 0;
6139
                                double originY = 0;
6140
                                SPPIDUtil.ConvertPointBystring(symbol.ORIGINALPOINT, ref originX, ref originY);
6141
                                double angle = SPPIDUtil.CalcAngle(textCenterX, textCenterY, originX, originY);
6142
                                double symbolCenterX = (symbolRange[0] + symbolRange[2]) / 2;
6143
                                double symbolCenterY = (symbolRange[1] + symbolRange[3]) / 2;
6144

    
6145
                                double gapX = 0;
6146
                                double gapY = 0;
6147
                                if (angle < 45)
6148
                                {
6149
                                    // Text 오른쪽
6150
                                    if (textCenterX > originX)
6151
                                    {
6152
                                        gapX = rangeMinX[0] - symbolRange[2];
6153
                                        gapY = allTextCenterY - symbolCenterY;
6154
                                    }
6155
                                    // Text 왼쪽
6156
                                    else
6157
                                    {
6158
                                        gapX = rangeMaxX[rangeMaxX.Count - 1] - symbolRange[0];
6159
                                        gapY = allTextCenterY - symbolCenterY;
6160
                                    }
6161
                                }
6162
                                else
6163
                                {
6164
                                    // Text 아래쪽
6165
                                    if (textCenterY > originY)
6166
                                    {
6167
                                        gapX = allTextCenterX - symbolCenterX;
6168
                                        gapY = rangeMaxY[rangeMaxY.Count - 1] - symbolRange[1];
6169
                                    }
6170
                                    // Text 위쪽
6171
                                    else
6172
                                    {
6173
                                        gapX = allTextCenterX - symbolCenterX;
6174
                                        gapY = rangeMinY[0] - symbolRange[3];
6175
                                    }
6176
                                }
6177

    
6178
                                foreach (var item in texts)
6179
                                {
6180
                                    MoveText(item, gapX, gapY);
6181
                                    RemodelingAssociationText(item);
6182
                                }
6183
                                #endregion
6184
                            }
6185
                        }
6186
                    }
6187
                }
6188

    
6189
                void MoveText(Text moveText, double x, double y)
6190
                {
6191
                    moveText.SPPID.SPPID_X = moveText.SPPID.SPPID_X - x;
6192
                    moveText.SPPID.SPPID_Y = moveText.SPPID.SPPID_Y - y;
6193
                    moveText.SPPID.Range = new double[] {
6194
                        moveText.SPPID.Range[0] - x,
6195
                        moveText.SPPID.Range[1]- y,
6196
                        moveText.SPPID.Range[2]- x,
6197
                        moveText.SPPID.Range[3]- y
6198
                    };
6199
                }
6200

    
6201
                endTexts.AddRange(allTexts);
6202

    
6203
                ReleaseCOMObjects(targetLabel);
6204
                targetLabel = null;
6205
                ReleaseCOMObjects(representation);
6206
                representation = null;
6207
            }
6208
        }
6209

    
6210
        private void RemodelingAssociationText(Text text)
6211
        {
6212
            LMLabelPersist removeLabel = dataSource.GetLabelPersist(text.SPPID.RepresentationId);
6213
            _placement.PIDRemovePlacement(removeLabel.AsLMRepresentation());
6214
            removeLabel.Commit();
6215
            ReleaseCOMObjects(removeLabel);
6216
            removeLabel = null;
6217

    
6218
            object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
6219
            if (owner != null && owner.GetType() == typeof(Symbol))
6220
            {
6221
                Symbol symbol = owner as Symbol;
6222
                _LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
6223
                if (_LMSymbol != null)
6224
                {
6225
                    BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
6226
                    if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
6227
                    {
6228
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
6229

    
6230
                        if (mapping != null)
6231
                        {
6232
                            double x = 0;
6233
                            double y = 0;
6234

    
6235
                            Array array = new double[] { 0, text.SPPID.SPPID_X, text.SPPID.SPPID_Y };
6236
                            LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
6237
                            if (_LMLabelPersist != null)
6238
                            {
6239
                                text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
6240
                                _LMLabelPersist.Commit();
6241
                            }
6242
                            ReleaseCOMObjects(_LMLabelPersist);
6243
                            _LMLabelPersist = null;
6244
                        }
6245
                    }
6246
                }
6247
                ReleaseCOMObjects(_LMSymbol);
6248
                _LMSymbol = null;
6249
            }
6250
        }
6251

    
6252
        /// <summary>
6253
        /// Note Modeling
6254
        /// </summary>
6255
        /// <param name="note"></param>
6256
        private void NoteModeling(Note note, List<Note> correctList)
6257
        {
6258
            LMSymbol _LMSymbol = null;
6259
            LMItemNote _LMItemNote = null;
6260
            LMAAttribute _LMAAttribute = null;
6261

    
6262
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
6263
            {
6264
                double x = 0;
6265
                double y = 0;
6266

    
6267
                CalcLabelLocation(ref x, ref y, note.SPPID.ORIGINAL_X, note.SPPID.ORIGINAL_Y, note.SPPIDLabelLocation, _ETCSetting.NoteLocation);
6268
                SPPIDUtil.ConvertGridPoint(ref x, ref y);
6269
                note.SPPID.SPPID_X = x;
6270
                note.SPPID.SPPID_Y = y;
6271

    
6272
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
6273
                if (_LMSymbol != null)
6274
                {
6275
                    _LMSymbol.Commit();
6276
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
6277
                    if (_LMItemNote != null)
6278
                    {
6279
                        _LMItemNote.Commit();
6280
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
6281
                        if (_LMAAttribute != null)
6282
                        {
6283
                            _LMAAttribute.set_Value(note.VALUE);
6284
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
6285

    
6286
                            double[] range = null;
6287
                            foreach (LMLabelPersist labelPersist in _LMSymbol.LabelPersists)
6288
                            {
6289
                                double[] temp = null;
6290
                                GetSPPIDSymbolRange(labelPersist, ref temp);
6291
                                if (temp != null)
6292
                                {
6293
                                    if (range == null)
6294
                                        range = temp;
6295
                                    else
6296
                                    {
6297
                                        range = new double[] {
6298
                                            Math.Min(range[0], temp[0]),
6299
                                            Math.Min(range[1], temp[1]),
6300
                                            Math.Max(range[2], temp[2]),
6301
                                            Math.Max(range[3], temp[3])
6302
                                        };
6303
                                    }
6304
                                }
6305
                            }
6306
                            if (range != null)
6307
                                correctList.Add(note);
6308
                            note.SPPID.Range = range;
6309

    
6310

    
6311
                            _LMItemNote.Commit();
6312
                        }
6313
                    }
6314
                }
6315
            }
6316

    
6317
            if (_LMAAttribute != null)
6318
                ReleaseCOMObjects(_LMAAttribute);
6319
            if (_LMItemNote != null)
6320
                ReleaseCOMObjects(_LMItemNote);
6321
            if (_LMSymbol != null)
6322
                ReleaseCOMObjects(_LMSymbol);
6323
        }
6324

    
6325
        private void NoteCorrectModeling(Note note, List<Note> endList)
6326
        {
6327
            bool needRemodeling = false;
6328
            bool loop = true;
6329
            GridSetting gridSetting = GridSetting.GetInstance();
6330
            while (loop)
6331
            {
6332
                loop = false;
6333
                foreach (var overlap in endList)
6334
                {
6335
                    if (SPPIDUtil.IsOverlap(overlap.SPPID.Range, note.SPPID.Range))
6336
                    {
6337
                        double tempX = 0;
6338
                        double tempY = 0;
6339
                        bool overlapX = false;
6340
                        bool overlapY = false;
6341
                        SPPIDUtil.CalcOverlap(note.SPPID.Range, overlap.SPPID.Range, ref tempX, ref tempY, ref overlapX, ref overlapY);
6342
                        double angle = SPPIDUtil.CalcAngle(note.LOCATION_X, note.LOCATION_Y, overlap.LOCATION_X, overlap.LOCATION_Y);
6343
                        if (overlapY && angle >= 45)
6344
                        {
6345
                            int count = Convert.ToInt32(tempY / gridSetting.Length) + 1;
6346
                            double move = gridSetting.Length * count;
6347
                            note.SPPID.SPPID_Y = note.SPPID.SPPID_Y - move;
6348
                            note.SPPID.Range = new double[] { note.SPPID.Range[0], note.SPPID.Range[1] - move, note.SPPID.Range[2], note.SPPID.Range[3] - move };
6349
                            needRemodeling = true;
6350
                            loop = true;
6351
                        }
6352
                        if (overlapX && angle <= 45)
6353
                        {
6354
                            int count = Convert.ToInt32(tempX / gridSetting.Length) + 1;
6355
                            double move = gridSetting.Length * count;
6356
                            note.SPPID.SPPID_X = note.SPPID.SPPID_X + move;
6357
                            note.SPPID.Range = new double[] { note.SPPID.Range[0] + move, note.SPPID.Range[1], note.SPPID.Range[2] + move, note.SPPID.Range[3] };
6358
                            needRemodeling = true;
6359
                            loop = true;
6360
                        }
6361
                    }
6362
                }
6363
            }
6364

    
6365

    
6366
            if (needRemodeling)
6367
            {
6368
                LMSymbol symbol = dataSource.GetSymbol(note.SPPID.RepresentationId);
6369
                _placement.PIDRemovePlacement(symbol.AsLMRepresentation());
6370
                note.SPPID.RepresentationId = null;
6371

    
6372
                LMItemNote _LMItemNote = null;
6373
                LMAAttribute _LMAAttribute = null;
6374
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, note.SPPID.SPPID_X, note.SPPID.SPPID_Y, Rotation: note.ANGLE);
6375
                if (_LMSymbol != null)
6376
                {
6377
                    _LMSymbol.Commit();
6378
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
6379
                    if (_LMItemNote != null)
6380
                    {
6381
                        _LMItemNote.Commit();
6382
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
6383
                        if (_LMAAttribute != null)
6384
                        {
6385
                            _LMAAttribute.set_Value(note.VALUE);
6386
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
6387
                            _LMItemNote.Commit();
6388

    
6389
                            ReleaseCOMObjects(_LMAAttribute);
6390
                            ReleaseCOMObjects(_LMItemNote);
6391
                        }
6392
                    }
6393
                }
6394

    
6395
                ReleaseCOMObjects(symbol);
6396
                symbol = null;
6397
                ReleaseCOMObjects(_LMItemNote);
6398
                _LMItemNote = null;
6399
                ReleaseCOMObjects(_LMAAttribute);
6400
                _LMAAttribute = null;
6401
                ReleaseCOMObjects(_LMSymbol);
6402
                _LMSymbol = null;
6403
            }
6404

    
6405
            endList.Add(note);
6406
        }
6407

    
6408
        private void JoinRunBySameType(string modelItemId, ref string survivorId)
6409
        {
6410
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
6411
            if (modelItem != null)
6412
            {
6413
                foreach (LMRepresentation rep in modelItem.Representations)
6414
                {
6415
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
6416
                    {
6417
                        LMConnector connector = dataSource.GetConnector(rep.Id);
6418
                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
6419
                        {
6420
                            LMSymbol symbol = connector.ConnectItem1SymbolObject;
6421
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
6422
                            if (modelItemIds.Count == 1)
6423
                            {
6424
                                string joinModelItemId = modelItemIds[0];
6425
                                JoinRun(joinModelItemId, modelItemId, ref survivorId, false);
6426
                                if (survivorId != null)
6427
                                    break;
6428
                            }
6429
                        }
6430
                        if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
6431
                        {
6432
                            LMSymbol symbol = connector.ConnectItem2SymbolObject;
6433
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
6434
                            if (modelItemIds.Count == 1)
6435
                            {
6436
                                string joinModelItemId = modelItemIds[0];
6437
                                JoinRun(joinModelItemId, modelItemId, ref survivorId, false);
6438
                                if (survivorId != null)
6439
                                    break;
6440
                            }
6441
                        }
6442
                    }
6443
                }
6444
            }
6445
        }
6446

    
6447
        /// <summary>
6448
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
6449
        /// </summary>
6450
        /// <param name="x"></param>
6451
        /// <param name="y"></param>
6452
        /// <param name="originX"></param>
6453
        /// <param name="originY"></param>
6454
        /// <param name="SPPIDLabelLocation"></param>
6455
        /// <param name="location"></param>
6456
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
6457
        {
6458
            if (location == Location.None)
6459
            {
6460
                x = originX;
6461
                y = originY;
6462
            }
6463
            else
6464
            {
6465
                if (location.HasFlag(Location.Center))
6466
                {
6467
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
6468
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
6469
                }
6470

    
6471
                if (location.HasFlag(Location.Left))
6472
                    x = SPPIDLabelLocation.X1;
6473
                else if (location.HasFlag(Location.Right))
6474
                    x = SPPIDLabelLocation.X2;
6475

    
6476
                if (location.HasFlag(Location.Down))
6477
                    y = SPPIDLabelLocation.Y1;
6478
                else if (location.HasFlag(Location.Up))
6479
                    y = SPPIDLabelLocation.Y2;
6480
            }
6481
        }
6482

    
6483
        /// <summary>
6484
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
6485
        /// 1. Angle Valve
6486
        /// 2. 3개로 이루어진 Symbol Group
6487
        /// </summary>
6488
        /// <returns></returns>
6489
        private List<Symbol> GetPrioritySymbol()
6490
        {
6491
            DataTable symbolTable = document.SymbolTable;
6492
            // List에 순서대로 쌓는다.
6493
            List<Symbol> symbols = new List<Symbol>();
6494

    
6495
            // Angle Valve 부터
6496
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
6497
            {
6498
                if (!symbols.Contains(symbol))
6499
                {
6500
                    double originX = 0;
6501
                    double originY = 0;
6502

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

    
6507
                    SlopeType slopeType1 = SlopeType.None;
6508
                    SlopeType slopeType2 = SlopeType.None;
6509
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
6510
                    {
6511
                        double connectorX = 0;
6512
                        double connectorY = 0;
6513
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
6514
                        if (slopeType1 == SlopeType.None)
6515
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
6516
                        else
6517
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
6518
                    }
6519

    
6520
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
6521
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
6522
                        symbols.Add(symbol);
6523
                }
6524
            }
6525

    
6526
            List<Symbol> tempSymbols = new List<Symbol>();
6527
            // Conn 갯수 기준
6528
            foreach (var item in document.SYMBOLS)
6529
            {
6530
                if (!symbols.Contains(item))
6531
                    tempSymbols.Add(item);
6532
            }
6533
            tempSymbols.Sort(SPPIDUtil.SortSymbolPriority);
6534
            symbols.AddRange(tempSymbols);
6535

    
6536
            return symbols;
6537
        }
6538

    
6539
        private void SetPriorityLine(List<Line> lines)
6540
        {
6541
            lines.Sort(SortLinePriority);
6542

    
6543
            int SortLinePriority(Line a, Line b)
6544
            {
6545
                // Branch 없는것부터
6546
                int branchRetval = CompareBranchLine(a, b);
6547
                if (branchRetval != 0)
6548
                {
6549
                    return branchRetval;
6550
                }
6551
                else
6552
                {
6553
                    // Symbol 연결 갯수
6554
                    int connSymbolRetval = CompareConnSymbol(a, b);
6555
                    if (connSymbolRetval != 0)
6556
                    {
6557
                        return connSymbolRetval;
6558
                    }
6559
                    else
6560
                    {
6561
                        // 아이템 연결 갯수(심볼, Line이면서 Not Branch)
6562
                        int connItemRetval = CompareConnItem(a, b);
6563
                        if (connItemRetval != 0)
6564
                        {
6565
                            return connItemRetval;
6566
                        }
6567
                        else
6568
                        {
6569
                            // ConnectedItem이 없는것
6570
                            int noneConnRetval = CompareNoneConn(a, b);
6571
                            if (noneConnRetval != 0)
6572
                            {
6573
                                return noneConnRetval;
6574
                            }
6575
                            else
6576
                            {
6577

    
6578
                            }
6579
                        }
6580
                    }
6581
                }
6582

    
6583
                return 0;
6584
            }
6585

    
6586
            int CompareNotSegmentLine(Line a, Line b)
6587
            {
6588
                List<Connector> connectorsA = a.CONNECTORS
6589
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
6590
                    .ToList();
6591

    
6592
                List<Connector> connectorsB = b.CONNECTORS
6593
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
6594
                    .ToList();
6595

    
6596
                // 오름차순
6597
                return connectorsB.Count.CompareTo(connectorsA.Count);
6598
            }
6599

    
6600
            int CompareConnSymbol(Line a, Line b)
6601
            {
6602
                List<Connector> connectorsA = a.CONNECTORS
6603
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
6604
                    .ToList();
6605

    
6606
                List<Connector> connectorsB = b.CONNECTORS
6607
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
6608
                    .ToList();
6609

    
6610
                // 오름차순
6611
                return connectorsB.Count.CompareTo(connectorsA.Count);
6612
            }
6613

    
6614
            int CompareConnItem(Line a, Line b)
6615
            {
6616
                List<Connector> connectorsA = a.CONNECTORS
6617
                    .Where(conn => conn.ConnectedObject != null &&
6618
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
6619
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
6620
                    .ToList();
6621

    
6622
                List<Connector> connectorsB = b.CONNECTORS
6623
                    .Where(conn => conn.ConnectedObject != null &&
6624
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
6625
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
6626
                    .ToList();
6627

    
6628
                // 오름차순
6629
                return connectorsB.Count.CompareTo(connectorsA.Count);
6630
            }
6631

    
6632
            int CompareBranchLine(Line a, Line b)
6633
            {
6634
                List<Connector> connectorsA = a.CONNECTORS
6635
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
6636
                    .ToList();
6637
                List<Connector> connectorsB = b.CONNECTORS
6638
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
6639
                    .ToList();
6640

    
6641
                // 내림차순
6642
                return connectorsA.Count.CompareTo(connectorsB.Count);
6643
            }
6644

    
6645
            int CompareNoneConn(Line a, Line b)
6646
            {
6647
                List<Connector> connectorsA = a.CONNECTORS
6648
                    .Where(conn => conn.ConnectedObject == null)
6649
                    .ToList();
6650

    
6651
                List<Connector> connectorsB = b.CONNECTORS
6652
                    .Where(conn => conn.ConnectedObject == null)
6653
                    .ToList();
6654

    
6655
                // 오름차순
6656
                return connectorsB.Count.CompareTo(connectorsA.Count);
6657
            }
6658
        }
6659

    
6660
        private void SortText(List<Text> texts)
6661
        {
6662
            texts.Sort(Sort);
6663

    
6664
            int Sort(Text a, Text b)
6665
            {
6666
                int yRetval = CompareY(a, b);
6667
                if (yRetval != 0)
6668
                {
6669
                    return yRetval;
6670
                }
6671
                else
6672
                {
6673
                    return CompareX(a, b);
6674
                }
6675
            }
6676

    
6677
            int CompareY(Text a, Text b)
6678
            {
6679
                return a.LOCATION_Y.CompareTo(b.LOCATION_Y);
6680
            }
6681

    
6682
            int CompareX(Text a, Text b)
6683
            {
6684
                return a.LOCATION_X.CompareTo(b.LOCATION_X);
6685
            }
6686
        }
6687
        private void SortNote(List<Note> notes)
6688
        {
6689
            notes.Sort(Sort);
6690

    
6691
            int Sort(Note a, Note b)
6692
            {
6693
                int yRetval = CompareY(a, b);
6694
                if (yRetval != 0)
6695
                {
6696
                    return yRetval;
6697
                }
6698
                else
6699
                {
6700
                    return CompareX(a, b);
6701
                }
6702
            }
6703

    
6704
            int CompareY(Note a, Note b)
6705
            {
6706
                return a.LOCATION_Y.CompareTo(b.LOCATION_Y);
6707
            }
6708

    
6709
            int CompareX(Note a, Note b)
6710
            {
6711
                return a.LOCATION_X.CompareTo(b.LOCATION_X);
6712
            }
6713
        }
6714

    
6715
        private void SortBranchLines()
6716
        {
6717
            BranchLines.Sort(SortBranchLine);
6718
            int SortBranchLine(Line a, Line b)
6719
            {
6720
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
6721
                 x.ConnectedObject.GetType() == typeof(Line) &&
6722
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
6723
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
6724

    
6725
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
6726
                 x.ConnectedObject.GetType() == typeof(Line) &&
6727
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
6728
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
6729

    
6730
                // 내림차순
6731
                return countA.CompareTo(countB);
6732
            }
6733
        }
6734

    
6735
        
6736

    
6737
        private string GetSPPIDFileName(LMModelItem modelItem)
6738
        {
6739
            string symbolPath = null;
6740
            foreach (LMRepresentation rep in modelItem.Representations)
6741
            {
6742
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
6743
                {
6744
                    symbolPath = rep.get_FileName();
6745
                    break;
6746
                }
6747
            }
6748
            return symbolPath;
6749
        }
6750

    
6751
        private string GetSPPIDFileName(string modelItemId)
6752
        {
6753
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
6754
            string symbolPath = null;
6755
            foreach (LMRepresentation rep in modelItem.Representations)
6756
            {
6757
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
6758
                {
6759
                    symbolPath = rep.get_FileName();
6760
                    break;
6761
                }
6762
            }
6763
            ReleaseCOMObjects(modelItem);
6764
            return symbolPath;
6765
        }
6766

    
6767
        /// <summary>
6768
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
6769
        /// </summary>
6770
        /// <param name="graphicOID"></param>
6771
        /// <param name="milliseconds"></param>
6772
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
6773
        {
6774
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
6775
            {
6776
                double minX = 0;
6777
                double minY = 0;
6778
                double maxX = 0;
6779
                double maxY = 0;
6780
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
6781
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
6782

    
6783
                Thread.Sleep(milliseconds);
6784
            }
6785
        }
6786

    
6787
        /// <summary>
6788
        /// ComObject를 Release
6789
        /// </summary>
6790
        /// <param name="objVars"></param>
6791
        public void ReleaseCOMObjects(params object[] objVars)
6792
        {
6793
            if (objVars != null)
6794
            {
6795
                int intNewRefCount = 0;
6796
                foreach (object obj in objVars)
6797
                {
6798
                    if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
6799
                        intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
6800
                }
6801
            }
6802
        }
6803

    
6804
        /// IDisposable 구현
6805
        ~AutoModeling()
6806
        {
6807
            this.Dispose(false);
6808
        }
6809

    
6810
        private bool disposed;
6811
        public void Dispose()
6812
        {
6813
            this.Dispose(true);
6814
            GC.SuppressFinalize(this);
6815
        }
6816

    
6817
        protected virtual void Dispose(bool disposing)
6818
        {
6819
            if (this.disposed) return;
6820
            if (disposing)
6821
            {
6822
                // IDisposable 인터페이스를 구현하는 멤버들을 여기서 정리합니다.
6823
            }
6824
            // .NET Framework에 의하여 관리되지 않는 외부 리소스들을 여기서 정리합니다.
6825
            this.disposed = true;
6826
        }
6827
    }
6828
}
클립보드 이미지 추가 (최대 크기: 500 MB)