프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ af4868b8

이력 | 보기 | 이력해설 | 다운로드 (69 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using Llama;
7
using Plaice;
8
using Ingr.RAD2D.Interop.RAD2D;
9
using Ingr.RAD2D.Internal;
10
using Ingr.RAD2D.Helper;
11
using Converter.BaseModel;
12
using Converter.SPPID.Model;
13
using Converter.SPPID.Properties;
14
using Converter.SPPID.Util;
15
using Converter.SPPID.DB;
16
using Ingr.RAD2D.MacroControls.CmdCtrl;
17
using Ingr.RAD2D;
18
using System.Windows;
19
using System.Threading;
20
using System.Drawing;
21
using Microsoft.VisualBasic;
22
using Newtonsoft.Json;
23

    
24
namespace Converter.SPPID
25
{
26
    public class AutoModeling
27
    {
28
        Placement _placement;
29
        LMADataSource dataSource;
30
        Ingr.RAD2D.Application radApp;
31
        SPPID_Document document;
32
        ETCSetting _ETCSetting;
33

    
34
        List<Tuple<string, Line, Line>> BranchLines = new List<Tuple<string, Line, Line>>();
35
        public AutoModeling(SPPID_Document document, Ingr.RAD2D.Application radApp)
36
        {
37
            this.document = document;
38
            this.radApp = radApp;
39
            this._ETCSetting = ETCSetting.GetInstance();
40
        }
41

    
42
        public void Run()
43
        {
44
            _placement = new Placement();
45
            dataSource = _placement.PIDDataSource;
46

    
47
            //dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
48
            //dynamic newDrawing = application.Drawings.Add(document.Unit, document.Template, document.DrawingNumber, document.DrawingName);
49
            //application.ActiveWindow.Fit();
50
            //Thread.Sleep(100);
51
            //application.ActiveWindow.Zoom = 60;
52
            //Thread.Sleep(100);
53

    
54
            try
55
            {
56
                // Equipment Modeling
57
                foreach (Equipment equipment in document.Equipments)
58
                    EquipmentModeling(equipment);
59
                
60
                // LineRun Symbol Modeling
61
                foreach (LineNumber lineNumber in document.LINENUMBERS)
62
                    foreach (LineRun run in lineNumber.RUNS)
63
                        SymbolModelingByRun(run);
64
                
65
                // TrimLineRun Symbol Modeling
66
                foreach (TrimLine trimLine in document.TRIMLINES)
67
                    foreach (LineRun run in trimLine.RUNS)
68
                        SymbolModelingByRun(run);
69
                
70
                // LineRun Line Modeling
71
                foreach (LineNumber lineNumber in document.LINENUMBERS)
72
                    foreach (LineRun run in lineNumber.RUNS)
73
                        LineModelingByRun(run);
74
                
75
                // TrimLineRun Line Modeling
76
                foreach (TrimLine trimLine in document.TRIMLINES)
77
                    foreach (LineRun run in trimLine.RUNS)
78
                        LineModelingByRun(run);
79
                
80
                // Branch Line Modeling
81
                foreach (var item in BranchLines)
82
                    BranchLineModeling(item);
83
                
84
                // EndBreak Modeling
85
                foreach (var item in document.EndBreaks)
86
                    EndBreakModeling(item);
87
                
88
                // LineNumber Modeling
89
                foreach (var item in document.LINENUMBERS)
90
                    LineNumberModeling(item);
91

    
92
                // LineRun Line Join
93
                foreach (LineNumber lineNumber in document.LINENUMBERS)
94
                    foreach (LineRun run in lineNumber.RUNS)
95
                        JoinRunLine(run);
96
                
97
                // TrimLineRun Line Join
98
                foreach (TrimLine trimLine in document.TRIMLINES)
99
                    foreach (LineRun run in trimLine.RUNS)
100
                        JoinRunLine(run);
101

    
102
                // Note Modeling
103
                foreach (var item in document.NOTES)
104
                    NoteModeling(item);
105
                
106
                // Text Modeling
107
                foreach (var item in document.TEXTINFOS)
108
                    TextModeling(item);
109

    
110
                // Input LineNumber Attribute
111
                foreach (var item in document.LINENUMBERS)
112
                    InputLineNumberAttribute(item);
113

    
114
                // Input Symbol Attribute
115
                foreach (var item in document.SYMBOLS)
116
                    InputSymbolAttribute(item);
117

    
118
                #region 임시 Label
119
                foreach (var item in document.SYMBOLS)
120
                {
121
                    if (item.SPPID.MAPPINGNAME.Contains("Labels - "))
122
                    {
123
                        SPPIDSymbolInfo info = item.SPPID;
124
                        Array points = new double[] { 0, item.SPPID.ORIGINAL_X, item.SPPID.ORIGINAL_Y };
125
                        string symbolUID = item.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL").VALUE;
126
                        object objectItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
127
                        if (objectItem != null)
128
                        {
129

    
130
                            string sRep = null;
131
                            if (objectItem.GetType() == typeof(Symbol))
132
                                sRep = ((Symbol)objectItem).SPPID.RepresentationId;
133
                            else if (objectItem.GetType() == typeof(Equipment))
134
                                sRep = ((Equipment)objectItem).SPPID.RepresentationId;
135

    
136
                            if (!string.IsNullOrEmpty(sRep))
137
                            {
138
                                LMSymbol _TargetItem = dataSource.GetSymbol(sRep);
139
                                LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(item.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: _TargetItem.AsLMRepresentation(), IsLeaderVisible: true);
140
                                LMModelItem _LMModelItem = _TargetItem.ModelItemObject;
141
                                LMAAttributes _Attributes = _LMModelItem.Attributes;
142

    
143
                                foreach (var attribute in item.ATTRIBUTES)
144
                                {
145
                                    if (!string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
146
                                    {
147
                                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
148
                                        if (mapping != null)
149
                                        {
150
                                            LMAAttribute _LMAAttribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
151
                                            if (mapping.SPPIDATTRIBUTENAME.Contains("Description"))
152
                                            {
153
                                                if (!DBNull.Value.Equals(_LMAAttribute.get_Value()) && !string.IsNullOrEmpty(_LMAAttribute.get_Value()))
154
                                                {
155
                                                    string value = _LMAAttribute.get_Value() + "\n" + attribute.VALUE;
156
                                                    _LMAAttribute.set_Value(value);
157
                                                }
158
                                                else
159
                                                {
160
                                                    _LMAAttribute.set_Value(attribute.VALUE);
161
                                                }
162
                                            }
163
                                            else if (_LMAAttribute != null)
164
                                                _LMAAttribute.set_Value(attribute.VALUE);
165
                                        }
166
                                    }
167
                                }
168

    
169
                                
170

    
171
                                _LMModelItem.Commit();
172
                                _LMLabelPresist.Commit();
173
                                ReleaseCOMObjects(_TargetItem);
174
                                ReleaseCOMObjects(_LMLabelPresist);
175
                                ReleaseCOMObjects(_LMModelItem);
176
                                ReleaseCOMObjects(_Attributes);
177
                            }
178
                        }
179
                    }
180
                }
181
                #endregion
182
            }
183
            catch (Exception ex)
184
            {
185
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
186
            }
187
            finally
188
            {
189
                ReleaseCOMObjects(dataSource);
190
                ReleaseCOMObjects(_placement);
191
            }
192

    
193
            //System.Windows.Forms.MessageBox.Show("end");
194
        }
195

    
196
        private void LineModelingByRun(LineRun run)
197
        {
198
            Line prevLine = null;
199
            List<Line> lines = new List<Line>();
200
            foreach (var item in run.RUNITEMS)
201
            {
202
                // Line일 경우
203
                if (item.GetType() == typeof(Line))
204
                {
205
                    Line line = item as Line;
206
                    if (prevLine == null)
207
                        lines.Add(line);
208
                    else if (prevLine != null)
209
                    {
210
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
211
                            lines.Add(line);
212
                        else
213
                        {
214
                            if (lines.Count > 0)
215
                            {
216
                                LineModeling(lines);
217
                                lines.Clear();
218
                            }
219
                            lines.Add(line);
220
                        }
221
                    }
222

    
223
                    prevLine = line;
224
                }
225
                // Symbol 일 경우
226
                else if (item.GetType() == typeof(Symbol))
227
                {
228
                    if (lines.Count > 0)
229
                    {
230
                        LineModeling(lines);
231
                        lines.Clear();
232
                    }
233
                }
234
            }
235

    
236
            if (lines.Count > 0)
237
                LineModeling(lines);
238
        }
239

    
240
        private void SymbolModelingByRun(LineRun run)
241
        {
242
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
243
            if (run.RUNITEMS.Count > 0)
244
            {
245
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
246
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
247

    
248
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
249
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
250
            }
251

    
252
            Symbol prevSymbol = null;
253
            Symbol targetSymbol = null;
254
            foreach (var item in run.RUNITEMS)
255
            {
256
                if (item.GetType() == typeof(Symbol))
257
                {
258
                    Symbol symbol = item as Symbol;
259
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
260
                    prevSymbol = symbol;
261
                    targetSymbol = symbol;
262
                }
263
                else
264
                {
265
                    targetSymbol = null;
266
                }
267
            }
268
        }
269

    
270
        private void SymbolModelingByRunStart(Symbol symbol, LineRun run)
271
        {
272
            foreach (var connector in symbol.CONNECTORS)
273
            {
274
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
275
                if (targetItem != null &&
276
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
277
                    !IsSameLineNumber(symbol, targetItem))
278
                {
279
                    SymbolModeling(symbol, targetItem as Symbol, null);
280
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
281
                    {
282
                        object item = run.RUNITEMS[i];
283
                        if (item.GetType() == typeof(Symbol))
284
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol, null);
285
                        else
286
                            break;
287
                    }
288
                    break;
289
                }
290
            }
291

    
292

    
293
        }
294

    
295
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
296
        {
297
            foreach (var connector in symbol.CONNECTORS)
298
            {
299
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
300
                if (targetItem != null &&
301
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
302
                    !IsSameLineNumber(symbol, targetItem))
303
                {
304
                    SymbolModeling(symbol, targetItem as Symbol, null);
305
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
306
                    {
307
                        object item = run.RUNITEMS[i];
308
                        if (item.GetType() == typeof(Symbol))
309
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol, null);
310
                        else
311
                            break;
312
                    }
313
                    break;
314
                }
315
            }
316
        }
317

    
318
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
319
        {
320
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
321
                return;
322

    
323
            LMSymbol _LMSymbol = null;
324

    
325
            string mappingPath = symbol.SPPID.MAPPINGNAME;
326
            double x = symbol.SPPID.ORIGINAL_X;
327
            double y = symbol.SPPID.ORIGINAL_Y;
328
            int mirror = 0;
329
            double angle = symbol.ANGLE;
330

    
331
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
332
            {
333
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
334

    
335
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
336
                double x1 = 0;
337
                double x2 = 0;
338
                double y1 = 0;
339
                double y2 = 0;
340
                symbol2d.Range(out x1, out y1, out x2, out y2);
341

    
342
                if (y2 < y)
343
                    y = y2;
344
                else if (y1 > y)
345
                    y = y1;
346

    
347
                if (x2 < x)
348
                    x = x2;
349
                else if (x1 > x)
350
                    x = x1;
351

    
352
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
353
                ReleaseCOMObjects(_TargetItem);
354
            }
355
            else if (prevSymbol != null)
356
            {
357
                LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
358
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
359
                double prevX = _PrevSymbol.get_XCoordinate();
360
                double prevY = _PrevSymbol.get_YCoordinate();
361
                if (slopeType == SlopeType.HORIZONTAL)
362
                    y = prevY;
363
                else if (slopeType == SlopeType.VERTICAL)
364
                    x = prevX;
365
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
366
            }
367
            else
368
            {
369
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
370
            }
371

    
372

    
373
            if (_LMSymbol != null)
374
            {
375
                _LMSymbol.Commit();
376
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
377

    
378
                foreach (var item in symbol.ChildSymbols)
379
                    CreateChildSymbol(item, _LMSymbol);
380
            }
381

    
382
            ReleaseCOMObjects(_LMSymbol);
383
        }
384

    
385
        private void EquipmentModeling(Equipment equipment)
386
        {
387
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
388
                return;
389

    
390
            LMSymbol _LMSymbol = null;
391
            LMSymbol targetItem = null;
392
            string mappingPath = equipment.SPPID.MAPPINGNAME;
393
            double x = equipment.SPPID.ORIGINAL_X;
394
            double y = equipment.SPPID.ORIGINAL_Y;
395
            int mirror = 0;
396
            double angle = equipment.ANGLE;
397

    
398
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
399
            if (connector != null)
400
            {
401
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
402
                if (connEquipment != null)
403
                {
404
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
405
                        EquipmentModeling(connEquipment);
406

    
407
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
408
                    {
409
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
410
                        if (targetItem != null)
411
                        {
412
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
413
                        }
414
                        else
415
                        {
416
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
417
                        }
418
                    }
419
                    else
420
                    {
421
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
422
                    }
423
                }
424
                else
425
                {
426
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
427
                }
428
            }
429
            else
430
            {
431
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
432
            }
433

    
434
            if (_LMSymbol != null)
435
            {
436
                _LMSymbol.Commit();
437
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
438
                ReleaseCOMObjects(_LMSymbol);
439
            }
440

    
441
            if (targetItem != null)
442
            {
443
                ReleaseCOMObjects(targetItem);
444
            }
445
            
446
            //if (equipment.CONNECTORS)
447
            //{
448

    
449
            //}
450

    
451
            //if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
452
            //{
453
            //    LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
454
            //    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
455
            //    ReleaseCOMObjects(_TargetItem);
456
            //}
457
            //else if (prevSymbol != null)
458
            //{
459
            //    LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
460
            //    SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
461
            //    double prevX = _PrevSymbol.get_XCoordinate();
462
            //    double prevY = _PrevSymbol.get_YCoordinate();
463
            //    if (slopeType == SlopeType.HORIZONTAL)
464
            //        y = prevY;
465
            //    else if (slopeType == SlopeType.VERTICAL)
466
            //        x = prevX;
467
            //    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
468
            //}
469
            //else
470
            //{
471
            //    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
472
            //}
473

    
474

    
475
            //if (_LMSymbol != null)
476
            //{
477
            //    _LMSymbol.Commit();
478
            //    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
479

    
480
            //    foreach (var item in symbol.ChildSymbols)
481
            //        CreateChildSymbol(item, _LMSymbol);
482
            //}
483

    
484
            ReleaseCOMObjects(_LMSymbol);
485
        }
486

    
487
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
488
        {
489
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
490
            double x1 = 0;
491
            double x2 = 0;
492
            double y1 = 0;
493
            double y2 = 0;
494
            symbol2d.Range(out x1, out y1, out x2, out y2);
495

    
496
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
497
            if (_LMSymbol != null)
498
            {
499
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
500
                foreach (var item in childSymbol.ChildSymbols)
501
                    CreateChildSymbol(item, _LMSymbol);
502
            }
503
            
504

    
505
            ReleaseCOMObjects(_LMSymbol);
506
        }
507

    
508
        private bool IsSameLineNumber(object item, object targetItem)
509
        {
510
            foreach (var lineNumber in document.LINENUMBERS)
511
            {
512
                foreach (var run in lineNumber.RUNS)
513
                {
514
                    foreach (var runItem in run.RUNITEMS)
515
                    {
516
                        if (runItem == item)
517
                        {
518
                            foreach (var findItem in run.RUNITEMS)
519
                            {
520
                                if (findItem == targetItem)
521
                                {
522
                                    return true;
523
                                }
524
                            }
525

    
526
                            return false;
527

    
528
                        }
529
                    }
530
                }
531
            }
532

    
533
            return false;
534
        }
535

    
536
        private void LineModeling(List<Line> lines)
537
        {
538
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
539
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
540
            LMSymbol _LMSymbol1 = null;
541
            LMSymbol _LMSymbol2 = null;
542
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
543
            LMConnector targetConnector1 = null;
544
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
545
            LMConnector targetConnector2 = null;
546

    
547
            Line startBranchLine = null;
548
            Line endBranchLine = null;
549

    
550
            for (int i = 0; i < lines.Count; i++)
551
            {
552
                Line line = lines[i];
553
                if (i == 0 || i + 1 != lines.Count)
554
                {
555
                    // 시작점에 연결된 Symbol 찾기
556
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
557
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
558
                    {
559
                        _LMSymbol1 = GetTargetSymbol(connItem as Symbol, line);
560
                        if (_LMSymbol1 != null)
561
                            placeRunInputs.AddSymbolTarget(_LMSymbol1, line.SPPID.START_X, line.SPPID.START_Y);
562
                        else
563
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
564
                    }
565
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
566
                    {
567
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
568
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
569

    
570
                        if (targetConnector1 != null)
571
                            placeRunInputs.AddConnectorTarget(targetConnector1, line.SPPID.START_X, line.SPPID.START_Y);
572
                        else
573
                        {
574
                            startBranchLine = connItem as Line;
575
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
576
                        }
577
                    }
578
                    else
579
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
580
                }
581

    
582
                if (i + 1 == lines.Count)
583
                {
584
                    // 끝점에 연결된 Symbol 찾기
585
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
586

    
587
                    if (i != 0)
588
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
589

    
590
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
591
                    {
592
                        _LMSymbol2 = GetTargetSymbol(connItem as Symbol, line);
593
                        if (_LMSymbol2 != null)
594
                            placeRunInputs.AddSymbolTarget(_LMSymbol2, line.SPPID.END_X, line.SPPID.END_Y);
595
                        else
596
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
597
                            
598
                    }
599
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
600
                    {
601
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
602
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
603

    
604
                        if (targetConnector2 != null)
605
                            placeRunInputs.AddConnectorTarget(targetConnector2, line.SPPID.END_X, line.SPPID.END_Y);
606
                        else
607
                        {
608
                            endBranchLine = connItem as Line;
609
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
610
                        }
611
                    }
612
                    else
613
                    {
614
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
615
                    }
616
                }
617
            }
618

    
619
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
620

    
621
            if (_lMConnector != null)
622
            {
623
                foreach (var line in lines)
624
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
625
                _lMConnector.Commit();
626
                if (startBranchLine != null || endBranchLine != null)
627
                {
628
                    BranchLines.Add(new Tuple<string, Line, Line>(_lMConnector.ModelItemID, startBranchLine, endBranchLine));
629
                }
630
            }
631

    
632

    
633
            if (_LMSymbol1 != null)
634
                ReleaseCOMObjects(_LMSymbol1);
635
            if (_LMSymbol2 != null)
636
                ReleaseCOMObjects(_LMSymbol2);
637
            if (targetConnector1 != null)
638
                ReleaseCOMObjects(targetConnector1);
639
            if (targetConnector2 != null)
640
                ReleaseCOMObjects(targetConnector2);
641
            foreach (var item in connectorVertices1)
642
                ReleaseCOMObjects(item.Key);
643
            foreach (var item in connectorVertices2)
644
                ReleaseCOMObjects(item.Key);
645

    
646
            ReleaseCOMObjects(_lMConnector);
647
            ReleaseCOMObjects(placeRunInputs);
648
            ReleaseCOMObjects(_LMAItem);
649
        }
650

    
651
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
652
        {
653
            LMSymbol _LMSymbol = null;
654
            foreach (var connector in symbol.CONNECTORS)
655
            {
656
                if (connector.CONNECTEDITEM == line.UID)
657
                {
658
                    if (connector.Index == 0)
659
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
660
                    else
661
                    {
662
                        ChildSymbol child = null;
663
                        foreach (var childSymbol in symbol.ChildSymbols)
664
                        {
665
                            if (childSymbol.Connectors.Contains(connector))
666
                                child = childSymbol;
667
                            else
668
                                child = GetChildSymbolByConnector(childSymbol, connector);
669

    
670
                            if (child != null)
671
                                break;
672
                        }
673

    
674
                        if (child != null)
675
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
676
                    }
677

    
678
                    break;  
679
                }
680
            }
681

    
682
            return _LMSymbol;
683
        }
684

    
685
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
686
        {
687
            foreach (var childSymbol in item.ChildSymbols)
688
            {
689
                if (childSymbol.Connectors.Contains(connector))
690
                    return childSymbol;
691
                else
692
                    return GetChildSymbolByConnector(childSymbol, connector);
693
            }
694

    
695
            return null;
696
        }
697

    
698
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
699
        {
700
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
701
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
702

    
703
            LMConnector _StartConnector = null;
704
            LMConnector _EndConnector = null;
705
            double lengthStart = double.MaxValue;
706
            double lengthEnd = double.MaxValue;
707
            List<double[]> startPoints = new List<double[]>();
708
            List<double[]> endPoints = new List<double[]>();
709

    
710
            foreach (var item in connectorVertices)
711
            {
712
                foreach (var point in item.Value)
713
                {
714
                    // Start Point가 Branch
715
                    if (branch.Item2 != null)
716
                    {
717
                        Line targetLine = branch.Item2;
718
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
719
                        if (lengthStart > distance)
720
                        {
721
                            _StartConnector = item.Key;
722
                            lengthStart = distance;
723
                            startPoints = item.Value;
724
                        }
725
                    }
726
                    // End Point가 Branch
727
                    if (branch.Item3 != null)
728
                    {
729
                        Line targetLine = branch.Item3;
730
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
731
                        if (lengthEnd > distance)
732
                        {
733
                            _EndConnector = item.Key;
734
                            lengthEnd = distance;
735
                            endPoints = item.Value;
736
                        }
737
                    }
738
                }
739
            }
740
            #region Branch가 양쪽 전부일 때
741
            if (_StartConnector != null && _StartConnector == _EndConnector)
742
            {
743
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
744

    
745
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
746
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
747

    
748
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
749
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
750
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
751
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
752
                   startPoints[startPoints.Count - 1][0],
753
                   startPoints[startPoints.Count - 1][1],
754
                   startPoints[startPoints.Count - 2][0],
755
                   startPoints[startPoints.Count - 2][1]);
756

    
757
                for (int i = 0; i < startPoints.Count; i++)
758
                {
759
                    double[] point = startPoints[i];
760
                    if (i == 0)
761
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
762
                    else if (i == startPoints.Count - 1)
763
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
764
                    else
765
                        placeRunInputs.AddPoint(point[0], point[1]);
766
                }
767

    
768
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
769
                if (_LMConnector != null)
770
                {
771
                    _LMConnector.Commit();
772
                    foreach (var item in lines)
773
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
774
                }
775

    
776
                foreach (var item in startConnectorVertices)
777
                    ReleaseCOMObjects(item.Key);
778
                foreach (var item in endConnectorVertices)
779
                    ReleaseCOMObjects(item.Key);
780
                ReleaseCOMObjects(placeRunInputs);
781
                ReleaseCOMObjects(_LMAItem);
782
                ReleaseCOMObjects(_LMConnector);
783
            }
784
            #endregion
785
            #region 양쪽이 다른 Branch 
786
            else
787
            {
788
                // Branch 시작 Connector
789
                if (_StartConnector != null)
790
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
791

    
792
                // Branch 끝 Connector
793
                if (_EndConnector != null)
794
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
795
            }
796
            #endregion
797

    
798
            if (_StartConnector != null)
799
                ReleaseCOMObjects(_StartConnector);
800
            if (_EndConnector != null)
801
                ReleaseCOMObjects(_EndConnector);
802
            foreach (var item in connectorVertices)
803
                ReleaseCOMObjects(item.Key);
804
        }
805

    
806
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
807
        {
808
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
809
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
810
            LMConnector _SameRunTargetConnector = null;
811
            LMSymbol _SameRunTargetSymbol = null;
812
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
813
            LMConnector _BranchTargetConnector = null;
814
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
815

    
816
            // 같은 Line Run의 Connector 찾기
817
            foreach (var item in connectorVertices)
818
            {
819
                if (item.Key == _Connector)
820
                    continue;
821

    
822
                if (IsStart &&
823
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
824
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
825
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
826
                {
827
                    _SameRunTargetConnector = item.Key;
828
                    break;
829
                }
830
                else if (!IsStart &&
831
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
832
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
833
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
834
                {
835
                    _SameRunTargetConnector = item.Key;
836
                    break;
837
                }
838
            }
839

    
840
            // Branch 반대편이 Symbol
841
            if (_SameRunTargetConnector == null)
842
            {
843
                foreach (var line in lines)
844
                {
845
                    foreach (var connector in line.CONNECTORS)
846
                    {
847
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
848
                        if (symbol != null)
849
                        {
850
                            _SameRunTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
851
                            break;
852
                        }
853
                    }
854
                }
855
            }
856

    
857
            // 기존 Connector 제거
858
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
859
            
860
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
861
            if (IsStart)
862
            {
863
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
864
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
865
            }
866
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
867
            else
868
            {
869
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
870
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
871
                    points[points.Count - 1][0],
872
                    points[points.Count - 1][1],
873
                    points[points.Count - 2][0],
874
                    points[points.Count - 2][1]);
875
            }
876

    
877
            for (int i = 0; i < points.Count; i++)
878
            {
879
                double[] point = points[i];
880
                if (i == 0)
881
                {
882
                    if (IsStart)
883
                    {
884
                        placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
885
                    }
886
                    else
887
                    {
888
                        if (_SameRunTargetConnector != null)
889
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
890
                        else if (_SameRunTargetSymbol != null)
891
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
892
                        else
893
                            placeRunInputs.AddPoint(point[0], point[1]);
894
                    }
895
                }
896
                else if (i == points.Count - 1)
897
                {
898
                    if (IsStart)
899
                    {
900
                        if (_SameRunTargetConnector != null)
901
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
902
                        else if (_SameRunTargetSymbol != null)
903
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
904
                        else
905
                            placeRunInputs.AddPoint(point[0], point[1]);
906
                    }
907
                    else
908
                    {
909
                        if (_BranchTargetConnector != null)
910
                        {
911
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
912
                        }
913
                    }
914
                }
915
                else
916
                    placeRunInputs.AddPoint(point[0], point[1]);
917
            }
918
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
919
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
920

    
921
            if (_LMConnector != null)
922
            {
923
                if (_SameRunTargetConnector != null)
924
                {
925
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
926
                }
927
                else
928
                {
929
                    foreach (var item in lines)
930
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
931
                }
932

    
933
                _LMConnector.Commit();
934
                ReleaseCOMObjects(_LMConnector);
935
            }
936

    
937
            ReleaseCOMObjects(placeRunInputs);
938
            ReleaseCOMObjects(_LMAItem);
939
            if (_BranchTargetConnector != null)
940
                ReleaseCOMObjects(_BranchTargetConnector);
941
            if (_SameRunTargetConnector != null)
942
                ReleaseCOMObjects(_SameRunTargetConnector);
943
            if (_SameRunTargetSymbol != null)
944
                ReleaseCOMObjects(_SameRunTargetSymbol);
945
            foreach (var item in connectorVertices)
946
                ReleaseCOMObjects(item.Key);
947
            foreach (var item in branchConnectorVertices)
948
                ReleaseCOMObjects(item.Key);
949
        }
950

    
951
        private void EndBreakModeling(EndBreak endBreak)
952
        {
953
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
954
            LMConnector targetLMConnector = null;
955
            if (ownerObj !=null && ownerObj.GetType() == typeof(Line))
956
            {
957
                Line ownerLine = ownerObj as Line;
958
                LMLabelPersist _LmLabelPersist = null;
959
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(ownerLine.SPPID.ModelItemId);
960

    
961
                targetLMConnector = FindTargetLMConnectorByPoint(connectorVertices, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
962
                
963
                if (targetLMConnector != null)
964
                {
965
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
966
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
967
                }
968

    
969
                if (_LmLabelPersist != null)
970
                {
971
                    _LmLabelPersist.Commit();
972
                    ReleaseCOMObjects(_LmLabelPersist);
973
                }
974
                else
975
                    RetryEndBreakModeling(endBreak, targetLMConnector);
976

    
977
                foreach (var item in connectorVertices)
978
                    ReleaseCOMObjects(item.Key);
979

    
980
            }
981
            else if (ownerObj != null && ownerObj.GetType() == typeof(Symbol))
982
            {
983
                Symbol ownerSymbol = ownerObj as Symbol;
984
                LMSymbol _LMSymbol = dataSource.GetSymbol(ownerSymbol.SPPID.RepresentationId);
985

    
986
                targetLMConnector = null;
987
                double distance = double.MaxValue;
988

    
989
                foreach (LMConnector connector in _LMSymbol.Avoid1Connectors)
990
                {
991
                    if (connector.get_ItemStatus() == "Active")
992
                    {
993
                        dynamic OID = connector.get_GraphicOID();
994
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
995
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
996
                        int verticesCount = lineStringGeometry.VertexCount;
997
                        double[] vertices = null;
998
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
999
                        for (int i = 0; i < verticesCount; i++)
1000
                        {
1001
                            double x = 0;
1002
                            double y = 0;
1003
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1004

    
1005
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1006
                            if (result < distance)
1007
                            {
1008
                                targetLMConnector = connector;
1009
                                distance = result;
1010
                            }
1011
                        }
1012
                    }
1013
                }
1014

    
1015
                foreach (LMConnector connector in _LMSymbol.Avoid2Connectors)
1016
                {
1017
                    if (connector.get_ItemStatus() == "Active")
1018
                    {
1019
                        dynamic OID = connector.get_GraphicOID();
1020
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1021
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1022
                        int verticesCount = lineStringGeometry.VertexCount;
1023
                        double[] vertices = null;
1024
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1025
                        for (int i = 0; i < verticesCount; i++)
1026
                        {
1027
                            double x = 0;
1028
                            double y = 0;
1029
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1030

    
1031
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1032
                            if (result < distance)
1033
                            {
1034
                                targetLMConnector = connector;
1035
                                distance = result;
1036
                            }
1037
                        }
1038
                    }
1039
                }
1040

    
1041
                if (targetLMConnector != null)
1042
                {
1043
                    LMLabelPersist _LmLabelPersist = null;
1044
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1045
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1046
                    if (_LmLabelPersist != null)
1047
                    {
1048
                        _LmLabelPersist.Commit();
1049
                        ReleaseCOMObjects(_LmLabelPersist);
1050
                    }
1051
                    else
1052
                        RetryEndBreakModeling(endBreak, targetLMConnector);
1053
                }
1054
                
1055
                ReleaseCOMObjects(_LMSymbol);
1056
            }
1057
        }
1058

    
1059
        private void RetryEndBreakModeling(EndBreak endBreak, LMConnector targetLMConnector)
1060
        {
1061
            bool isZeroLength = Convert.ToBoolean(targetLMConnector.get_IsZeroLength());
1062
            Array array = null;
1063
            LMLabelPersist _LMLabelPersist = null;
1064
            LMConnector _LMConnector = null;
1065
            dynamic OID = targetLMConnector.get_GraphicOID();
1066
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1067
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1068
            int verticesCount = lineStringGeometry.VertexCount;
1069
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1070
            _LMAItem _LMAItem = _placement.PIDCreateItem(@"\Piping\Routing\Process Lines\Primary Piping.sym");
1071

    
1072
            if (isZeroLength)
1073
            {
1074
                double[] vertices = null;
1075
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1076
                double x = 0;
1077
                double y = 0;
1078
                lineStringGeometry.GetVertex(1, ref x, ref y);
1079

    
1080
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, x, y);
1081
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, x, y);
1082

    
1083
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1084
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1085

    
1086
                array = new double[] { 0, x, y };
1087
                _LMLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: _LMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1088
            }
1089
            else
1090
            {
1091
                List<double[]> vertices = new List<double[]>();
1092
                for (int i = 1; i <= verticesCount; i++)
1093
                {
1094
                    double x = 0;
1095
                    double y = 0;
1096
                    lineStringGeometry.GetVertex(i, ref x, ref y);
1097
                    vertices.Add(new double[] { x, y });
1098
                }
1099

    
1100
                for (int i = 0; i < vertices.Count; i++)
1101
                {
1102
                    double[] points = vertices[i];
1103
                    if (i == 0)
1104
                    {
1105
                        if (targetLMConnector.ConnectItem1SymbolObject != null)
1106
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1107
                        else
1108
                            placeRunInputs.AddPoint(points[0], points[1]);
1109
                    }
1110
                    else if (i == vertices.Count - 1)
1111
                    {
1112
                        if (targetLMConnector.ConnectItem2SymbolObject != null)
1113
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1114
                        else
1115
                            placeRunInputs.AddPoint(points[0], points[1]);
1116
                    }
1117
                    else
1118
                        placeRunInputs.AddPoint(points[0], points[1]);
1119
                }
1120

    
1121
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, targetLMConnector.ModelItemID);
1122
                
1123
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1124
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1125

    
1126
                foreach (var line in lines)
1127
                    line.SPPID.ModelItemId = _LMConnector.ModelItemID;
1128

    
1129
                array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1130
                _LMLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: _LMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1131
            }
1132

    
1133

    
1134
            if (_LMLabelPersist != null)
1135
            {
1136
                _LMLabelPersist.Commit();
1137
                ReleaseCOMObjects(_LMLabelPersist);
1138
            }
1139
            else
1140
            {
1141
                
1142
            }
1143

    
1144
            ReleaseCOMObjects(_LMConnector);
1145
            ReleaseCOMObjects(placeRunInputs);
1146
            ReleaseCOMObjects(_LMAItem);
1147
        }
1148

    
1149
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
1150
        {
1151
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
1152
            _LMAItem item1 = modelItem1.AsLMAItem();
1153
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
1154
            _LMAItem item2 = modelItem2.AsLMAItem();
1155
            
1156
            // item2가 item1으로 조인
1157
            try
1158
            {
1159
                _placement.PIDJoinRuns(ref item1, ref item2);
1160
                item1.Commit();
1161
                item2.Commit();
1162
            }
1163
            catch (Exception ex)
1164
            {
1165
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1166
            }
1167
            finally
1168
            {
1169
                ReleaseCOMObjects(modelItem1);
1170
                ReleaseCOMObjects(item1);
1171
                ReleaseCOMObjects(modelItem2);
1172
                ReleaseCOMObjects(item2);
1173
            }
1174
        }
1175

    
1176
        private void AutoJoinPipeRun(string modelItemId)
1177
        {
1178
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
1179
            _LMAItem item = modelItem.AsLMAItem();
1180
            try
1181
            {
1182
                string modelitemID = item.Id;
1183
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
1184
                string afterModelItemID = item.Id;
1185
                if (modelitemID != afterModelItemID)
1186
                {
1187
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
1188
                    foreach (var line in lines)
1189
                        line.SPPID.ModelItemId = afterModelItemID;
1190
                }
1191
                item.Commit();
1192
            }
1193
            catch (Exception ex)
1194
            {
1195
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1196
            }
1197
            finally
1198
            {
1199
                ReleaseCOMObjects(modelItem);
1200
                ReleaseCOMObjects(item);
1201
            }
1202
        }
1203

    
1204
        private void JoinRunLine(LineRun run)
1205
        {
1206
            string modelItemId = string.Empty;
1207
            foreach (var item in run.RUNITEMS)
1208
            {
1209
                if (item.GetType() == typeof(Line))
1210
                {
1211
                    Line line = item as Line;
1212
                    if (modelItemId != line.SPPID.ModelItemId)
1213
                    {
1214
                        AutoJoinPipeRun(line.SPPID.ModelItemId);
1215
                        modelItemId = line.SPPID.ModelItemId;
1216
                    }
1217
                }
1218
            }
1219
        }
1220

    
1221
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
1222
        {
1223
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
1224
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
1225

    
1226
            if (modelItem != null)
1227
            {
1228
                foreach (LMRepresentation rep in modelItem.Representations)
1229
                {
1230
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1231
                    {
1232
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1233
                        connectorVertices.Add(_LMConnector, new List<double[]>());
1234
                        dynamic OID = rep.get_GraphicOID();
1235
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1236
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1237
                        int verticesCount = lineStringGeometry.VertexCount;
1238
                        double[] vertices = null;
1239
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1240
                        for (int i = 0; i < verticesCount; i++)
1241
                        {
1242
                            double x = 0;
1243
                            double y = 0;
1244
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1245
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
1246
                        }
1247
                    }
1248
                }
1249

    
1250
                ReleaseCOMObjects(modelItem);
1251
            }
1252

    
1253
            return connectorVertices;
1254
        }
1255

    
1256
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
1257
        {
1258
            double length = double.MaxValue;
1259
            LMConnector targetConnector = null;
1260
            foreach (var item in connectorVertices)
1261
            {
1262
                List<double[]> points = item.Value;
1263
                for (int i = 0; i < points.Count - 1; i++)
1264
                {
1265
                    double[] point1 = points[i];
1266
                    double[] point2 = points[i + 1];
1267

    
1268
                    double maxLineX = Math.Max(point1[0], point2[0]);
1269
                    double minLineX = Math.Min(point1[0], point2[0]);
1270
                    double maxLineY = Math.Max(point1[1], point2[1]);
1271
                    double minLineY = Math.Min(point1[1], point2[1]);
1272

    
1273
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
1274

    
1275
                    // 두직선의 교차점
1276
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
1277
                    if (crossingPoint != null)
1278
                    {
1279
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
1280
                        if (length >= distance)
1281
                        {
1282
                            if (slope == SlopeType.Slope &&
1283
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
1284
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1285
                            {
1286
                                targetConnector = item.Key;
1287
                                length = distance;
1288
                            }
1289
                            else if (slope == SlopeType.HORIZONTAL &&
1290
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
1291
                            {
1292
                                targetConnector = item.Key;
1293
                                length = distance;
1294
                            }
1295
                            else if (slope == SlopeType.VERTICAL &&
1296
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1297
                            {
1298
                                targetConnector = item.Key;
1299
                                length = distance;
1300
                            }
1301
                        }
1302
                    }
1303
                }
1304
            }
1305

    
1306
            return targetConnector;
1307
        }
1308

    
1309
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1310
        {
1311
            double length = double.MaxValue;
1312
            LMConnector targetConnector = null;
1313
            foreach (var item in connectorVertices)
1314
            {
1315
                List<double[]> points = item.Value;
1316

    
1317
                foreach (double[] point in points)
1318
                {
1319
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1320
                    if (length >= distance)
1321
                    {
1322
                        targetConnector = item.Key;
1323
                        length = distance;
1324
                    }
1325
                }
1326
            }
1327

    
1328
            return targetConnector;
1329
        }
1330

    
1331
        private void LineNumberModeling(LineNumber lineNumber)
1332
        {
1333
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
1334
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
1335
            LMConnector connectedLMConnector = FindTargetLMConnectorByPoint(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
1336
            if (connectedLMConnector != null)
1337
            {
1338
                double x = 0;
1339
                double y = 0;
1340
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
1341

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

    
1346
                foreach (var item in connectorVertices)
1347
                    ReleaseCOMObjects(item.Key);
1348
                if (_LmLabelPresist != null)
1349
                {
1350
                    lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
1351
                    ReleaseCOMObjects(_LmLabelPresist);
1352
                }
1353
            }
1354
        }
1355

    
1356
        private void InputLineNumberAttribute(LineNumber lineNumber)
1357
        {
1358
            foreach (var run in lineNumber.RUNS)
1359
            {
1360
                foreach (var item in run.RUNITEMS)
1361
                {
1362
                    if (item.GetType() == typeof(Line))
1363
                    {
1364
                        Line line = item as Line;
1365
                        LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1366
                        if (_LMModelItem.get_ItemStatus() == "Active")
1367
                        {
1368
                            foreach (var attribute in lineNumber.ATTRIBUTES)
1369
                            {
1370
                                LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1371
                                if (mapping != null)
1372
                                {
1373
                                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1374
                                    if (_LMAAttribute != null)
1375
                                    {
1376
                                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1377
                                            _LMAAttribute.set_Value(attribute.VALUE);
1378
                                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
1379
                                            _LMAAttribute.set_Value(attribute.VALUE);
1380
                                    }
1381
                                }
1382
                            }
1383
                            _LMModelItem.Commit();
1384
                            break;
1385
                        }
1386
                        ReleaseCOMObjects(_LMModelItem);
1387
                    }
1388
                }
1389
            }
1390
        }
1391

    
1392
        private void InputSymbolAttribute(Symbol symbol)
1393
        {
1394
            try
1395
            {
1396
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1397
                {
1398
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1399
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1400
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
1401

    
1402
                    foreach (var item in symbol.PROPERTIES)
1403
                    {
1404

    
1405

    
1406
                    }
1407

    
1408
                    foreach (var item in symbol.ATTRIBUTES)
1409
                    {
1410
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
1411
                        if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
1412
                        {
1413
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
1414
                            if (_Attribute != null)
1415
                            {
1416
                                // 임시
1417
                                if (item.ATTRIBUTETYPE == "String")
1418
                                {
1419
                                    if (!string.IsNullOrEmpty(item.VALUE))
1420
                                    {
1421
                                        if (!DBNull.Value.Equals(_Attribute.get_Value()) && !string.IsNullOrEmpty(_Attribute.get_Value()))
1422
                                        {
1423
                                            string value = _Attribute.get_Value() + "\n" + item.VALUE;
1424
                                            _Attribute.set_Value(value);
1425
                                        }
1426
                                        else
1427
                                        {
1428
                                            _Attribute.set_Value(item.VALUE);
1429
                                        }
1430
                                    }
1431
                                }
1432
                                else
1433
                                {
1434
                                    _Attribute.set_Value(item.VALUE);
1435
                                }
1436
                            }
1437
                        }
1438
                    }
1439

    
1440
                    foreach (var item in symbol.ASSOCIATIONS)
1441
                    {
1442

    
1443
                    }
1444

    
1445
                    ReleaseCOMObjects(_LMSymbol);
1446
                    ReleaseCOMObjects(_Attributes);
1447
                    ReleaseCOMObjects(_LMModelItem);
1448
                }
1449
            }
1450
            catch (Exception ex)
1451
            {
1452
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1453
            }
1454
        }
1455

    
1456
        private void TextModeling(Text text)
1457
        {
1458
            LMSymbol _LMSymbol = null;
1459
            try
1460
            {
1461
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
1462
                if (text.ASSOCIATION)
1463
                {
1464
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
1465
                    if (owner.GetType() == typeof(Symbol))
1466
                    {
1467
                        Symbol symbol = owner as Symbol;
1468
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1469
                        if (_LMSymbol != null)
1470
                        {
1471
                            Association association = symbol.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
1472
                            List<BaseModel.Attribute> attributes = symbol.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
1473
                            AttributeMapping mapping = null;
1474
                            foreach (var attribute in attributes)
1475
                            {
1476
                                if (string.IsNullOrEmpty(attribute.VALUE) || attribute.VALUE == "None")
1477
                                    continue;
1478

    
1479
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
1480
                                if (mapping != null)
1481
                                    break;  
1482
                            }
1483

    
1484
                            if (mapping != null)
1485
                            {
1486
                                double x = 0;
1487
                                double y = 0;
1488

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

    
1492
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: true);
1493
                                if (_LMLabelPersist!=null)
1494
                                {
1495
                                    _LMLabelPersist.Commit();
1496
                                    ReleaseCOMObjects(_LMLabelPersist);
1497
                                }
1498
                            }
1499
                        }
1500
                    }
1501
                    else if (owner.GetType() == typeof(Line))
1502
                    {
1503

    
1504
                    }
1505
                }
1506
                else
1507
                {
1508
                    LMItemNote _LMItemNote = null;
1509
                    LMAAttribute _LMAAttribute = null;
1510

    
1511
                    double x = 0;
1512
                    double y = 0;
1513

    
1514
                    CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
1515

    
1516
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y);
1517
                    _LMSymbol.Commit();
1518
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1519
                    _LMItemNote.Commit();
1520
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1521
                    _LMAAttribute.set_Value(text.VALUE);
1522
                    _LMItemNote.Commit();
1523

    
1524
                    if (_LMAAttribute != null)
1525
                        ReleaseCOMObjects(_LMAAttribute);
1526
                    if (_LMItemNote != null)
1527
                        ReleaseCOMObjects(_LMItemNote);
1528
                }
1529
            }
1530
            catch (Exception ex)
1531
            {
1532

    
1533
            }
1534
            finally
1535
            {
1536
                if (_LMSymbol != null)
1537
                    ReleaseCOMObjects(_LMSymbol);
1538
            }
1539
        }
1540

    
1541
        private void NoteModeling(Note note)
1542
        {
1543
            LMSymbol _LMSymbol = null;
1544
            LMItemNote _LMItemNote = null;
1545
            LMAAttribute _LMAAttribute = null;
1546

    
1547
            try
1548
            {
1549
                double x = 0;
1550
                double y = 0;
1551

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

    
1554
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
1555
                _LMSymbol.Commit();
1556
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1557
                _LMItemNote.Commit();
1558
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1559
                _LMAAttribute.set_Value(note.VALUE);
1560
                _LMItemNote.Commit();
1561
            }
1562
            catch (Exception ex)
1563
            {
1564

    
1565
            }
1566
            finally
1567
            {
1568
                if (_LMAAttribute != null)
1569
                    ReleaseCOMObjects(_LMAAttribute);
1570
                if (_LMItemNote != null)
1571
                    ReleaseCOMObjects(_LMItemNote);
1572
                if (_LMSymbol != null)
1573
                    ReleaseCOMObjects(_LMSymbol);
1574
            }
1575
            
1576
        }
1577

    
1578
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
1579
        {
1580
            if (location == Location.None)
1581
            {
1582
                x = originX;
1583
                y = originY;
1584
            }
1585
            else
1586
            {
1587
                if (location.HasFlag(Location.Center))
1588
                {
1589
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
1590
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
1591
                }
1592

    
1593
                if (location.HasFlag(Location.Left))
1594
                    x = SPPIDLabelLocation.X1;
1595
                else if (location.HasFlag(Location.Right))
1596
                    x = SPPIDLabelLocation.X2;
1597

    
1598
                if (location.HasFlag(Location.Down))
1599
                    y = SPPIDLabelLocation.Y1;
1600
                else if (location.HasFlag(Location.Up))
1601
                    y = SPPIDLabelLocation.Y2;
1602
            }
1603
        }
1604

    
1605
        public void ReleaseCOMObjects(params object[] objVars)
1606
        {
1607
            int intNewRefCount = 0;
1608
            foreach (object obj in objVars)
1609
            {
1610
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
1611
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
1612
            }
1613
        }
1614
    }
1615
}