프로젝트

일반

사용자정보

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

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ aa3c9d74

이력 | 보기 | 이력해설 | 다운로드 (69.9 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
            //dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
47
            //dynamic newDrawing = application.Drawings.Add(document.Unit, document.Template, document.DrawingNumber, document.DrawingName);
48
            //application.ActiveWindow.Fit();
49
            //Thread.Sleep(100);
50
            //application.ActiveWindow.Zoom = 60;
51
            //Thread.Sleep(100);
52

    
53

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

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

    
105
                // Note Modeling
106
                foreach (var item in document.NOTES)
107
                    NoteModeling(item);
108
                
109
                // Text Modeling
110
                foreach (var item in document.TEXTINFOS)
111
                    TextModeling(item);
112

    
113
                // Input LineNumber Attribute
114
                foreach (var item in document.LINENUMBERS)
115
                    InputLineNumberAttribute(item);
116

    
117
                // Input Symbol Attribute
118
                foreach (var item in document.SYMBOLS)
119
                    InputSymbolAttribute(item);
120

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

    
133
                            string sRep = null;
134
                            if (objectItem.GetType() == typeof(Symbol))
135
                                sRep = ((Symbol)objectItem).SPPID.RepresentationId;
136
                            else if (objectItem.GetType() == typeof(Equipment))
137
                                sRep = ((Equipment)objectItem).SPPID.RepresentationId;
138

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

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

    
172
                                
173

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

    
196
            //System.Windows.Forms.MessageBox.Show("end");
197
        }
198

    
199
        public void Test()
200
        {
201
            _placement = new Placement();
202
            dataSource = _placement.PIDDataSource;
203

    
204
            DependencyObject drawingObject = radApp.ActiveDocument.SelectSet[0] as DependencyObject;
205
            if (drawingObject != null)
206
            {
207
                string modelitemID = drawingObject.AttributeSets[0][5].GetValue().ToString();
208
                radApp.ActiveDocument.SelectSet.RemoveAll();
209
                LMPipeRun run = dataSource.GetPipeRun(modelitemID);
210
                _LMAItem item = run.AsLMAItem();
211
                string modelitemID2 = item.Id;
212
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
213
                string afterModelItemID = item.Id;
214
                if (modelitemID2 != afterModelItemID)
215
                {
216

    
217
                }
218
            }
219
        }
220

    
221
        private void LineModelingByRun(LineRun run)
222
        {
223
            Line prevLine = null;
224
            List<Line> lines = new List<Line>();
225
            foreach (var item in run.RUNITEMS)
226
            {
227
                // Line일 경우
228
                if (item.GetType() == typeof(Line))
229
                {
230
                    Line line = item as Line;
231
                    if (prevLine == null)
232
                        lines.Add(line);
233
                    else if (prevLine != null)
234
                    {
235
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
236
                            lines.Add(line);
237
                        else
238
                        {
239
                            if (lines.Count > 0)
240
                            {
241
                                LineModeling(lines);
242
                                lines.Clear();
243
                            }
244
                            lines.Add(line);
245
                        }
246
                    }
247

    
248
                    prevLine = line;
249
                }
250
                // Symbol 일 경우
251
                else if (item.GetType() == typeof(Symbol))
252
                {
253
                    if (lines.Count > 0)
254
                    {
255
                        LineModeling(lines);
256
                        lines.Clear();
257
                    }
258
                }
259
            }
260

    
261
            if (lines.Count > 0)
262
                LineModeling(lines);
263
        }
264

    
265
        private void SymbolModelingByRun(LineRun run)
266
        {
267
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
268
            if (run.RUNITEMS.Count > 0)
269
            {
270
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
271
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
272

    
273
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
274
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
275
            }
276

    
277
            Symbol prevSymbol = null;
278
            Symbol targetSymbol = null;
279
            foreach (var item in run.RUNITEMS)
280
            {
281
                if (item.GetType() == typeof(Symbol))
282
                {
283
                    Symbol symbol = item as Symbol;
284
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
285
                    prevSymbol = symbol;
286
                    targetSymbol = symbol;
287
                }
288
                else
289
                {
290
                    targetSymbol = null;
291
                }
292
            }
293
        }
294

    
295
        private void SymbolModelingByRunStart(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 = 1; i < run.RUNITEMS.Count; 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
        }
319

    
320
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
321
        {
322
            foreach (var connector in symbol.CONNECTORS)
323
            {
324
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
325
                if (targetItem != null &&
326
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
327
                    !IsSameLineNumber(symbol, targetItem))
328
                {
329
                    SymbolModeling(symbol, targetItem as Symbol, null);
330
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
331
                    {
332
                        object item = run.RUNITEMS[i];
333
                        if (item.GetType() == typeof(Symbol))
334
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol, null);
335
                        else
336
                            break;
337
                    }
338
                    break;
339
                }
340
            }
341
        }
342

    
343
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
344
        {
345
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
346
                return;
347

    
348
            LMSymbol _LMSymbol = null;
349

    
350
            string mappingPath = symbol.SPPID.MAPPINGNAME;
351
            double x = symbol.SPPID.ORIGINAL_X;
352
            double y = symbol.SPPID.ORIGINAL_Y;
353
            int mirror = 0;
354
            double angle = symbol.ANGLE;
355

    
356
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
357
            {
358
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
359

    
360
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
361
                double x1 = 0;
362
                double x2 = 0;
363
                double y1 = 0;
364
                double y2 = 0;
365
                symbol2d.Range(out x1, out y1, out x2, out y2);
366

    
367
                if (y2 < y)
368
                    y = y2;
369
                else if (y1 > y)
370
                    y = y1;
371

    
372
                if (x2 < x)
373
                    x = x2;
374
                else if (x1 > x)
375
                    x = x1;
376

    
377
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
378
                ReleaseCOMObjects(_TargetItem);
379
            }
380
            else if (prevSymbol != null)
381
            {
382
                LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
383
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
384
                double prevX = _PrevSymbol.get_XCoordinate();
385
                double prevY = _PrevSymbol.get_YCoordinate();
386
                if (slopeType == SlopeType.HORIZONTAL)
387
                    y = prevY;
388
                else if (slopeType == SlopeType.VERTICAL)
389
                    x = prevX;
390
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
391
            }
392
            else
393
            {
394
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
395
            }
396

    
397

    
398
            if (_LMSymbol != null)
399
            {
400
                _LMSymbol.Commit();
401
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
402

    
403
                foreach (var item in symbol.ChildSymbols)
404
                    CreateChildSymbol(item, _LMSymbol);
405
            }
406

    
407
            ReleaseCOMObjects(_LMSymbol);
408
        }
409

    
410
        private void EquipmentModeling(Equipment equipment)
411
        {
412
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
413
                return;
414

    
415
            LMSymbol _LMSymbol = null;
416
            LMSymbol targetItem = null;
417
            string mappingPath = equipment.SPPID.MAPPINGNAME;
418
            double x = equipment.SPPID.ORIGINAL_X;
419
            double y = equipment.SPPID.ORIGINAL_Y;
420
            int mirror = 0;
421
            double angle = equipment.ANGLE;
422

    
423
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
424
            if (connector != null)
425
            {
426
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
427
                if (connEquipment != null)
428
                {
429
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
430
                        EquipmentModeling(connEquipment);
431

    
432
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
433
                    {
434
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
435
                        if (targetItem != null)
436
                        {
437
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
438
                        }
439
                        else
440
                        {
441
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
442
                        }
443
                    }
444
                    else
445
                    {
446
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
447
                    }
448
                }
449
                else
450
                {
451
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
452
                }
453
            }
454
            else
455
            {
456
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
457
            }
458

    
459
            if (_LMSymbol != null)
460
            {
461
                _LMSymbol.Commit();
462
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
463
                ReleaseCOMObjects(_LMSymbol);
464
            }
465

    
466
            if (targetItem != null)
467
            {
468
                ReleaseCOMObjects(targetItem);
469
            }
470
            
471
            //if (equipment.CONNECTORS)
472
            //{
473

    
474
            //}
475

    
476
            //if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
477
            //{
478
            //    LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
479
            //    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
480
            //    ReleaseCOMObjects(_TargetItem);
481
            //}
482
            //else if (prevSymbol != null)
483
            //{
484
            //    LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
485
            //    SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
486
            //    double prevX = _PrevSymbol.get_XCoordinate();
487
            //    double prevY = _PrevSymbol.get_YCoordinate();
488
            //    if (slopeType == SlopeType.HORIZONTAL)
489
            //        y = prevY;
490
            //    else if (slopeType == SlopeType.VERTICAL)
491
            //        x = prevX;
492
            //    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
493
            //}
494
            //else
495
            //{
496
            //    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
497
            //}
498

    
499

    
500
            //if (_LMSymbol != null)
501
            //{
502
            //    _LMSymbol.Commit();
503
            //    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
504

    
505
            //    foreach (var item in symbol.ChildSymbols)
506
            //        CreateChildSymbol(item, _LMSymbol);
507
            //}
508

    
509
            ReleaseCOMObjects(_LMSymbol);
510
        }
511

    
512
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
513
        {
514
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
515
            double x1 = 0;
516
            double x2 = 0;
517
            double y1 = 0;
518
            double y2 = 0;
519
            symbol2d.Range(out x1, out y1, out x2, out y2);
520

    
521
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
522
            if (_LMSymbol != null)
523
            {
524
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
525
                foreach (var item in childSymbol.ChildSymbols)
526
                    CreateChildSymbol(item, _LMSymbol);
527
            }
528
            
529

    
530
            ReleaseCOMObjects(_LMSymbol);
531
        }
532

    
533
        private bool IsSameLineNumber(object item, object targetItem)
534
        {
535
            foreach (var lineNumber in document.LINENUMBERS)
536
            {
537
                foreach (var run in lineNumber.RUNS)
538
                {
539
                    foreach (var runItem in run.RUNITEMS)
540
                    {
541
                        if (runItem == item)
542
                        {
543
                            foreach (var findItem in run.RUNITEMS)
544
                            {
545
                                if (findItem == targetItem)
546
                                {
547
                                    return true;
548
                                }
549
                            }
550

    
551
                            return false;
552

    
553
                        }
554
                    }
555
                }
556
            }
557

    
558
            return false;
559
        }
560

    
561
        private void LineModeling(List<Line> lines)
562
        {
563
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
564
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
565
            LMSymbol _LMSymbol1 = null;
566
            LMSymbol _LMSymbol2 = null;
567
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
568
            LMConnector targetConnector1 = null;
569
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
570
            LMConnector targetConnector2 = null;
571

    
572
            Line startBranchLine = null;
573
            Line endBranchLine = null;
574

    
575
            for (int i = 0; i < lines.Count; i++)
576
            {
577
                Line line = lines[i];
578
                if (i == 0 || i + 1 != lines.Count)
579
                {
580
                    // 시작점에 연결된 Symbol 찾기
581
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
582
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
583
                    {
584
                        _LMSymbol1 = GetTargetSymbol(connItem as Symbol, line);
585
                        if (_LMSymbol1 != null)
586
                            placeRunInputs.AddSymbolTarget(_LMSymbol1, line.SPPID.START_X, line.SPPID.START_Y);
587
                        else
588
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
589
                    }
590
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
591
                    {
592
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
593
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
594

    
595
                        if (targetConnector1 != null)
596
                            placeRunInputs.AddConnectorTarget(targetConnector1, line.SPPID.START_X, line.SPPID.START_Y);
597
                        else
598
                        {
599
                            startBranchLine = connItem as Line;
600
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
601
                        }
602
                    }
603
                    else
604
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
605
                }
606

    
607
                if (i + 1 == lines.Count)
608
                {
609
                    // 끝점에 연결된 Symbol 찾기
610
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
611

    
612
                    if (i != 0)
613
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
614

    
615
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
616
                    {
617
                        _LMSymbol2 = GetTargetSymbol(connItem as Symbol, line);
618
                        if (_LMSymbol2 != null)
619
                            placeRunInputs.AddSymbolTarget(_LMSymbol2, line.SPPID.END_X, line.SPPID.END_Y);
620
                        else
621
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
622
                            
623
                    }
624
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
625
                    {
626
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
627
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
628

    
629
                        if (targetConnector2 != null)
630
                            placeRunInputs.AddConnectorTarget(targetConnector2, line.SPPID.END_X, line.SPPID.END_Y);
631
                        else
632
                        {
633
                            endBranchLine = connItem as Line;
634
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
635
                        }
636
                    }
637
                    else
638
                    {
639
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
640
                    }
641
                }
642
            }
643

    
644
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
645

    
646
            if (_lMConnector != null)
647
            {
648
                foreach (var line in lines)
649
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
650
                _lMConnector.Commit();
651
                if (startBranchLine != null || endBranchLine != null)
652
                {
653
                    BranchLines.Add(new Tuple<string, Line, Line>(_lMConnector.ModelItemID, startBranchLine, endBranchLine));
654
                }
655
            }
656

    
657

    
658
            if (_LMSymbol1 != null)
659
                ReleaseCOMObjects(_LMSymbol1);
660
            if (_LMSymbol2 != null)
661
                ReleaseCOMObjects(_LMSymbol2);
662
            if (targetConnector1 != null)
663
                ReleaseCOMObjects(targetConnector1);
664
            if (targetConnector2 != null)
665
                ReleaseCOMObjects(targetConnector2);
666
            foreach (var item in connectorVertices1)
667
                ReleaseCOMObjects(item.Key);
668
            foreach (var item in connectorVertices2)
669
                ReleaseCOMObjects(item.Key);
670

    
671
            ReleaseCOMObjects(_lMConnector);
672
            ReleaseCOMObjects(placeRunInputs);
673
            ReleaseCOMObjects(_LMAItem);
674
        }
675

    
676
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
677
        {
678
            LMSymbol _LMSymbol = null;
679
            foreach (var connector in symbol.CONNECTORS)
680
            {
681
                if (connector.CONNECTEDITEM == line.UID)
682
                {
683
                    if (connector.Index == 0)
684
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
685
                    else
686
                    {
687
                        ChildSymbol child = null;
688
                        foreach (var childSymbol in symbol.ChildSymbols)
689
                        {
690
                            if (childSymbol.Connectors.Contains(connector))
691
                                child = childSymbol;
692
                            else
693
                                child = GetChildSymbolByConnector(childSymbol, connector);
694

    
695
                            if (child != null)
696
                                break;
697
                        }
698

    
699
                        if (child != null)
700
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
701
                    }
702

    
703
                    break;  
704
                }
705
            }
706

    
707
            return _LMSymbol;
708
        }
709

    
710
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
711
        {
712
            foreach (var childSymbol in item.ChildSymbols)
713
            {
714
                if (childSymbol.Connectors.Contains(connector))
715
                    return childSymbol;
716
                else
717
                    return GetChildSymbolByConnector(childSymbol, connector);
718
            }
719

    
720
            return null;
721
        }
722

    
723
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
724
        {
725
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
726
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
727

    
728
            LMConnector _StartConnector = null;
729
            LMConnector _EndConnector = null;
730
            double lengthStart = double.MaxValue;
731
            double lengthEnd = double.MaxValue;
732
            List<double[]> startPoints = new List<double[]>();
733
            List<double[]> endPoints = new List<double[]>();
734

    
735
            foreach (var item in connectorVertices)
736
            {
737
                foreach (var point in item.Value)
738
                {
739
                    // Start Point가 Branch
740
                    if (branch.Item2 != null)
741
                    {
742
                        Line targetLine = branch.Item2;
743
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
744
                        if (lengthStart > distance)
745
                        {
746
                            _StartConnector = item.Key;
747
                            lengthStart = distance;
748
                            startPoints = item.Value;
749
                        }
750
                    }
751
                    // End Point가 Branch
752
                    if (branch.Item3 != null)
753
                    {
754
                        Line targetLine = branch.Item3;
755
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
756
                        if (lengthEnd > distance)
757
                        {
758
                            _EndConnector = item.Key;
759
                            lengthEnd = distance;
760
                            endPoints = item.Value;
761
                        }
762
                    }
763
                }
764
            }
765
            #region Branch가 양쪽 전부일 때
766
            if (_StartConnector != null && _StartConnector == _EndConnector)
767
            {
768
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
769

    
770
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
771
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
772

    
773
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
774
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
775
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
776
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
777
                   startPoints[startPoints.Count - 1][0],
778
                   startPoints[startPoints.Count - 1][1],
779
                   startPoints[startPoints.Count - 2][0],
780
                   startPoints[startPoints.Count - 2][1]);
781

    
782
                for (int i = 0; i < startPoints.Count; i++)
783
                {
784
                    double[] point = startPoints[i];
785
                    if (i == 0)
786
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
787
                    else if (i == startPoints.Count - 1)
788
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
789
                    else
790
                        placeRunInputs.AddPoint(point[0], point[1]);
791
                }
792

    
793
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
794
                if (_LMConnector != null)
795
                {
796
                    _LMConnector.Commit();
797
                    foreach (var item in lines)
798
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
799
                }
800

    
801
                foreach (var item in startConnectorVertices)
802
                    ReleaseCOMObjects(item.Key);
803
                foreach (var item in endConnectorVertices)
804
                    ReleaseCOMObjects(item.Key);
805
                ReleaseCOMObjects(placeRunInputs);
806
                ReleaseCOMObjects(_LMAItem);
807
                ReleaseCOMObjects(_LMConnector);
808
            }
809
            #endregion
810
            #region 양쪽이 다른 Branch 
811
            else
812
            {
813
                // Branch 시작 Connector
814
                if (_StartConnector != null)
815
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
816

    
817
                // Branch 끝 Connector
818
                if (_EndConnector != null)
819
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
820
            }
821
            #endregion
822

    
823
            if (_StartConnector != null)
824
                ReleaseCOMObjects(_StartConnector);
825
            if (_EndConnector != null)
826
                ReleaseCOMObjects(_EndConnector);
827
            foreach (var item in connectorVertices)
828
                ReleaseCOMObjects(item.Key);
829
        }
830

    
831
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
832
        {
833
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
834
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
835
            LMConnector _SameRunTargetConnector = null;
836
            LMSymbol _SameRunTargetSymbol = null;
837
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
838
            LMConnector _BranchTargetConnector = null;
839
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
840

    
841
            // 같은 Line Run의 Connector 찾기
842
            foreach (var item in connectorVertices)
843
            {
844
                if (item.Key == _Connector)
845
                    continue;
846

    
847
                if (IsStart &&
848
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
849
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
850
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
851
                {
852
                    _SameRunTargetConnector = item.Key;
853
                    break;
854
                }
855
                else if (!IsStart &&
856
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
857
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
858
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
859
                {
860
                    _SameRunTargetConnector = item.Key;
861
                    break;
862
                }
863
            }
864

    
865
            // Branch 반대편이 Symbol
866
            if (_SameRunTargetConnector == null)
867
            {
868
                foreach (var line in lines)
869
                {
870
                    foreach (var connector in line.CONNECTORS)
871
                    {
872
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
873
                        if (symbol != null)
874
                        {
875
                            _SameRunTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
876
                            break;
877
                        }
878
                    }
879
                }
880
            }
881

    
882
            // 기존 Connector 제거
883
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
884
            
885
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
886
            if (IsStart)
887
            {
888
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
889
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
890
            }
891
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
892
            else
893
            {
894
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
895
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
896
                    points[points.Count - 1][0],
897
                    points[points.Count - 1][1],
898
                    points[points.Count - 2][0],
899
                    points[points.Count - 2][1]);
900
            }
901

    
902
            for (int i = 0; i < points.Count; i++)
903
            {
904
                double[] point = points[i];
905
                if (i == 0)
906
                {
907
                    if (IsStart)
908
                    {
909
                        placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
910
                    }
911
                    else
912
                    {
913
                        if (_SameRunTargetConnector != null)
914
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
915
                        else if (_SameRunTargetSymbol != null)
916
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
917
                        else
918
                            placeRunInputs.AddPoint(point[0], point[1]);
919
                    }
920
                }
921
                else if (i == points.Count - 1)
922
                {
923
                    if (IsStart)
924
                    {
925
                        if (_SameRunTargetConnector != null)
926
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
927
                        else if (_SameRunTargetSymbol != null)
928
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
929
                        else
930
                            placeRunInputs.AddPoint(point[0], point[1]);
931
                    }
932
                    else
933
                    {
934
                        if (_BranchTargetConnector != null)
935
                        {
936
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
937
                        }
938
                    }
939
                }
940
                else
941
                    placeRunInputs.AddPoint(point[0], point[1]);
942
            }
943
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
944
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
945

    
946
            if (_LMConnector != null)
947
            {
948
                if (_SameRunTargetConnector != null)
949
                {
950
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
951
                }
952
                else
953
                {
954
                    foreach (var item in lines)
955
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
956
                }
957

    
958
                _LMConnector.Commit();
959
                ReleaseCOMObjects(_LMConnector);
960
            }
961

    
962
            ReleaseCOMObjects(placeRunInputs);
963
            ReleaseCOMObjects(_LMAItem);
964
            if (_BranchTargetConnector != null)
965
                ReleaseCOMObjects(_BranchTargetConnector);
966
            if (_SameRunTargetConnector != null)
967
                ReleaseCOMObjects(_SameRunTargetConnector);
968
            if (_SameRunTargetSymbol != null)
969
                ReleaseCOMObjects(_SameRunTargetSymbol);
970
            foreach (var item in connectorVertices)
971
                ReleaseCOMObjects(item.Key);
972
            foreach (var item in branchConnectorVertices)
973
                ReleaseCOMObjects(item.Key);
974
        }
975

    
976
        private void EndBreakModeling(EndBreak endBreak)
977
        {
978
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
979
            LMConnector targetLMConnector = null;
980
            if (ownerObj !=null && ownerObj.GetType() == typeof(Line))
981
            {
982
                Line ownerLine = ownerObj as Line;
983
                LMLabelPersist _LmLabelPersist = null;
984
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(ownerLine.SPPID.ModelItemId);
985

    
986
                targetLMConnector = FindTargetLMConnectorByPoint(connectorVertices, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
987
                
988
                if (targetLMConnector != null)
989
                {
990
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
991
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
992
                }
993

    
994
                if (_LmLabelPersist != null)
995
                {
996
                    _LmLabelPersist.Commit();
997
                    ReleaseCOMObjects(_LmLabelPersist);
998
                }
999
                else
1000
                    RetryEndBreakModeling(endBreak, targetLMConnector);
1001

    
1002
                foreach (var item in connectorVertices)
1003
                    ReleaseCOMObjects(item.Key);
1004

    
1005
            }
1006
            else if (ownerObj != null && ownerObj.GetType() == typeof(Symbol))
1007
            {
1008
                Symbol ownerSymbol = ownerObj as Symbol;
1009
                LMSymbol _LMSymbol = dataSource.GetSymbol(ownerSymbol.SPPID.RepresentationId);
1010

    
1011
                targetLMConnector = null;
1012
                double distance = double.MaxValue;
1013

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

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

    
1040
                foreach (LMConnector connector in _LMSymbol.Avoid2Connectors)
1041
                {
1042
                    if (connector.get_ItemStatus() == "Active")
1043
                    {
1044
                        dynamic OID = connector.get_GraphicOID();
1045
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1046
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1047
                        int verticesCount = lineStringGeometry.VertexCount;
1048
                        double[] vertices = null;
1049
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1050
                        for (int i = 0; i < verticesCount; i++)
1051
                        {
1052
                            double x = 0;
1053
                            double y = 0;
1054
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1055

    
1056
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1057
                            if (result < distance)
1058
                            {
1059
                                targetLMConnector = connector;
1060
                                distance = result;
1061
                            }
1062
                        }
1063
                    }
1064
                }
1065

    
1066
                if (targetLMConnector != null)
1067
                {
1068
                    LMLabelPersist _LmLabelPersist = null;
1069
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1070
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1071
                    if (_LmLabelPersist != null)
1072
                    {
1073
                        _LmLabelPersist.Commit();
1074
                        ReleaseCOMObjects(_LmLabelPersist);
1075
                    }
1076
                    else
1077
                        RetryEndBreakModeling(endBreak, targetLMConnector);
1078
                }
1079
                
1080
                ReleaseCOMObjects(_LMSymbol);
1081
            }
1082
        }
1083

    
1084
        private void RetryEndBreakModeling(EndBreak endBreak, LMConnector targetLMConnector)
1085
        {
1086
            bool isZeroLength = Convert.ToBoolean(targetLMConnector.get_IsZeroLength());
1087
            Array array = null;
1088
            LMLabelPersist _LMLabelPersist = null;
1089
            LMConnector _LMConnector = null;
1090
            dynamic OID = targetLMConnector.get_GraphicOID();
1091
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1092
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1093
            int verticesCount = lineStringGeometry.VertexCount;
1094
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1095
            _LMAItem _LMAItem = _placement.PIDCreateItem(@"\Piping\Routing\Process Lines\Primary Piping.sym");
1096

    
1097
            if (isZeroLength)
1098
            {
1099
                double[] vertices = null;
1100
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1101
                double x = 0;
1102
                double y = 0;
1103
                lineStringGeometry.GetVertex(1, ref x, ref y);
1104

    
1105
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, x, y);
1106
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, x, y);
1107

    
1108
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1109
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1110

    
1111
                array = new double[] { 0, x, y };
1112
                _LMLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: _LMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1113
            }
1114
            else
1115
            {
1116
                List<double[]> vertices = new List<double[]>();
1117
                for (int i = 1; i <= verticesCount; i++)
1118
                {
1119
                    double x = 0;
1120
                    double y = 0;
1121
                    lineStringGeometry.GetVertex(i, ref x, ref y);
1122
                    vertices.Add(new double[] { x, y });
1123
                }
1124

    
1125
                for (int i = 0; i < vertices.Count; i++)
1126
                {
1127
                    double[] points = vertices[i];
1128
                    if (i == 0)
1129
                    {
1130
                        if (targetLMConnector.ConnectItem1SymbolObject != null)
1131
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1132
                        else
1133
                            placeRunInputs.AddPoint(points[0], points[1]);
1134
                    }
1135
                    else if (i == vertices.Count - 1)
1136
                    {
1137
                        if (targetLMConnector.ConnectItem2SymbolObject != null)
1138
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1139
                        else
1140
                            placeRunInputs.AddPoint(points[0], points[1]);
1141
                    }
1142
                    else
1143
                        placeRunInputs.AddPoint(points[0], points[1]);
1144
                }
1145

    
1146
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, targetLMConnector.ModelItemID);
1147
                
1148
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1149
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1150

    
1151
                foreach (var line in lines)
1152
                    line.SPPID.ModelItemId = _LMConnector.ModelItemID;
1153

    
1154
                array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1155
                _LMLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: _LMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1156
            }
1157

    
1158

    
1159
            if (_LMLabelPersist != null)
1160
            {
1161
                _LMLabelPersist.Commit();
1162
                ReleaseCOMObjects(_LMLabelPersist);
1163
            }
1164
            else
1165
            {
1166
                
1167
            }
1168

    
1169
            ReleaseCOMObjects(_LMConnector);
1170
            ReleaseCOMObjects(placeRunInputs);
1171
            ReleaseCOMObjects(_LMAItem);
1172
        }
1173

    
1174
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
1175
        {
1176
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
1177
            _LMAItem item1 = modelItem1.AsLMAItem();
1178
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
1179
            _LMAItem item2 = modelItem2.AsLMAItem();
1180
            
1181
            // item2가 item1으로 조인
1182
            try
1183
            {
1184
                _placement.PIDJoinRuns(ref item1, ref item2);
1185
                item1.Commit();
1186
                item2.Commit();
1187
            }
1188
            catch (Exception ex)
1189
            {
1190
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1191
            }
1192
            finally
1193
            {
1194
                ReleaseCOMObjects(modelItem1);
1195
                ReleaseCOMObjects(item1);
1196
                ReleaseCOMObjects(modelItem2);
1197
                ReleaseCOMObjects(item2);
1198
            }
1199
        }
1200

    
1201
        private void AutoJoinPipeRun(string modelItemId)
1202
        {
1203
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
1204
            _LMAItem item = modelItem.AsLMAItem();
1205
            try
1206
            {
1207
                string modelitemID = item.Id;
1208
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
1209
                string afterModelItemID = item.Id;
1210
                if (modelitemID != afterModelItemID)
1211
                {
1212
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
1213
                    foreach (var line in lines)
1214
                        line.SPPID.ModelItemId = afterModelItemID;
1215
                }
1216
                item.Commit();
1217
            }
1218
            catch (Exception ex)
1219
            {
1220
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1221
            }
1222
            finally
1223
            {
1224
                ReleaseCOMObjects(modelItem);
1225
                ReleaseCOMObjects(item);
1226
            }
1227
        }
1228

    
1229
        private void JoinRunLine(LineRun run)
1230
        {
1231
            string modelItemId = string.Empty;
1232
            foreach (var item in run.RUNITEMS)
1233
            {
1234
                if (item.GetType() == typeof(Line))
1235
                {
1236
                    Line line = item as Line;
1237
                    if (modelItemId != line.SPPID.ModelItemId)
1238
                    {
1239
                        AutoJoinPipeRun(line.SPPID.ModelItemId);
1240
                        modelItemId = line.SPPID.ModelItemId;
1241
                    }
1242
                }
1243
            }
1244
        }
1245

    
1246
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
1247
        {
1248
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
1249
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
1250

    
1251
            if (modelItem != null)
1252
            {
1253
                foreach (LMRepresentation rep in modelItem.Representations)
1254
                {
1255
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1256
                    {
1257
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1258
                        connectorVertices.Add(_LMConnector, new List<double[]>());
1259
                        dynamic OID = rep.get_GraphicOID();
1260
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1261
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1262
                        int verticesCount = lineStringGeometry.VertexCount;
1263
                        double[] vertices = null;
1264
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1265
                        for (int i = 0; i < verticesCount; i++)
1266
                        {
1267
                            double x = 0;
1268
                            double y = 0;
1269
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1270
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
1271
                        }
1272
                    }
1273
                }
1274

    
1275
                ReleaseCOMObjects(modelItem);
1276
            }
1277

    
1278
            return connectorVertices;
1279
        }
1280

    
1281
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
1282
        {
1283
            double length = double.MaxValue;
1284
            LMConnector targetConnector = null;
1285
            foreach (var item in connectorVertices)
1286
            {
1287
                List<double[]> points = item.Value;
1288
                for (int i = 0; i < points.Count - 1; i++)
1289
                {
1290
                    double[] point1 = points[i];
1291
                    double[] point2 = points[i + 1];
1292

    
1293
                    double maxLineX = Math.Max(point1[0], point2[0]);
1294
                    double minLineX = Math.Min(point1[0], point2[0]);
1295
                    double maxLineY = Math.Max(point1[1], point2[1]);
1296
                    double minLineY = Math.Min(point1[1], point2[1]);
1297

    
1298
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
1299

    
1300
                    // 두직선의 교차점
1301
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
1302
                    if (crossingPoint != null)
1303
                    {
1304
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
1305
                        if (length >= distance)
1306
                        {
1307
                            if (slope == SlopeType.Slope &&
1308
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
1309
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1310
                            {
1311
                                targetConnector = item.Key;
1312
                                length = distance;
1313
                            }
1314
                            else if (slope == SlopeType.HORIZONTAL &&
1315
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
1316
                            {
1317
                                targetConnector = item.Key;
1318
                                length = distance;
1319
                            }
1320
                            else if (slope == SlopeType.VERTICAL &&
1321
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1322
                            {
1323
                                targetConnector = item.Key;
1324
                                length = distance;
1325
                            }
1326
                        }
1327
                    }
1328
                }
1329
            }
1330

    
1331
            return targetConnector;
1332
        }
1333

    
1334
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1335
        {
1336
            double length = double.MaxValue;
1337
            LMConnector targetConnector = null;
1338
            foreach (var item in connectorVertices)
1339
            {
1340
                List<double[]> points = item.Value;
1341

    
1342
                foreach (double[] point in points)
1343
                {
1344
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1345
                    if (length >= distance)
1346
                    {
1347
                        targetConnector = item.Key;
1348
                        length = distance;
1349
                    }
1350
                }
1351
            }
1352

    
1353
            return targetConnector;
1354
        }
1355

    
1356
        private void LineNumberModeling(LineNumber lineNumber)
1357
        {
1358
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
1359
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
1360
            LMConnector connectedLMConnector = FindTargetLMConnectorByPoint(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
1361
            if (connectedLMConnector != null)
1362
            {
1363
                double x = 0;
1364
                double y = 0;
1365
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
1366

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

    
1371
                foreach (var item in connectorVertices)
1372
                    ReleaseCOMObjects(item.Key);
1373
                if (_LmLabelPresist != null)
1374
                {
1375
                    lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
1376
                    ReleaseCOMObjects(_LmLabelPresist);
1377
                }
1378
            }
1379
        }
1380

    
1381
        private void InputLineNumberAttribute(LineNumber lineNumber)
1382
        {
1383
            foreach (var run in lineNumber.RUNS)
1384
            {
1385
                foreach (var item in run.RUNITEMS)
1386
                {
1387
                    if (item.GetType() == typeof(Line))
1388
                    {
1389
                        Line line = item as Line;
1390
                        LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1391
                        if (_LMModelItem.get_ItemStatus() == "Active")
1392
                        {
1393
                            foreach (var attribute in lineNumber.ATTRIBUTES)
1394
                            {
1395
                                LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1396
                                if (mapping != null)
1397
                                {
1398
                                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1399
                                    if (_LMAAttribute != null)
1400
                                    {
1401
                                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1402
                                            _LMAAttribute.set_Value(attribute.VALUE);
1403
                                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
1404
                                            _LMAAttribute.set_Value(attribute.VALUE);
1405
                                    }
1406
                                }
1407
                            }
1408
                            _LMModelItem.Commit();
1409
                            break;
1410
                        }
1411
                        ReleaseCOMObjects(_LMModelItem);
1412
                    }
1413
                }
1414
            }
1415
        }
1416

    
1417
        private void InputSymbolAttribute(Symbol symbol)
1418
        {
1419
            try
1420
            {
1421
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1422
                {
1423
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1424
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1425
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
1426

    
1427
                    foreach (var item in symbol.PROPERTIES)
1428
                    {
1429

    
1430

    
1431
                    }
1432

    
1433
                    foreach (var item in symbol.ATTRIBUTES)
1434
                    {
1435
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
1436
                        if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
1437
                        {
1438
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
1439
                            if (_Attribute != null)
1440
                            {
1441
                                // 임시
1442
                                if (item.ATTRIBUTETYPE == "String")
1443
                                {
1444
                                    if (!string.IsNullOrEmpty(item.VALUE))
1445
                                    {
1446
                                        if (!DBNull.Value.Equals(_Attribute.get_Value()) && !string.IsNullOrEmpty(_Attribute.get_Value()))
1447
                                        {
1448
                                            string value = _Attribute.get_Value() + "\n" + item.VALUE;
1449
                                            _Attribute.set_Value(value);
1450
                                        }
1451
                                        else
1452
                                        {
1453
                                            _Attribute.set_Value(item.VALUE);
1454
                                        }
1455
                                    }
1456
                                }
1457
                                else
1458
                                {
1459
                                    _Attribute.set_Value(item.VALUE);
1460
                                }
1461
                            }
1462
                        }
1463
                    }
1464

    
1465
                    foreach (var item in symbol.ASSOCIATIONS)
1466
                    {
1467

    
1468
                    }
1469

    
1470
                    ReleaseCOMObjects(_LMSymbol);
1471
                    ReleaseCOMObjects(_Attributes);
1472
                    ReleaseCOMObjects(_LMModelItem);
1473
                }
1474
            }
1475
            catch (Exception ex)
1476
            {
1477
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1478
            }
1479
        }
1480

    
1481
        private void TextModeling(Text text)
1482
        {
1483
            LMSymbol _LMSymbol = null;
1484
            try
1485
            {
1486
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
1487
                if (text.ASSOCIATION)
1488
                {
1489
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
1490
                    if (owner.GetType() == typeof(Symbol))
1491
                    {
1492
                        Symbol symbol = owner as Symbol;
1493
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1494
                        if (_LMSymbol != null)
1495
                        {
1496
                            Association association = symbol.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
1497
                            List<BaseModel.Attribute> attributes = symbol.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
1498
                            AttributeMapping mapping = null;
1499
                            foreach (var attribute in attributes)
1500
                            {
1501
                                if (string.IsNullOrEmpty(attribute.VALUE) || attribute.VALUE == "None")
1502
                                    continue;
1503

    
1504
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
1505
                                if (mapping != null)
1506
                                    break;  
1507
                            }
1508

    
1509
                            if (mapping != 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, mapping.Location);
1515
                                Array array = new double[] { 0, x, y };
1516

    
1517
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: true);
1518
                                if (_LMLabelPersist!=null)
1519
                                {
1520
                                    _LMLabelPersist.Commit();
1521
                                    ReleaseCOMObjects(_LMLabelPersist);
1522
                                }
1523
                            }
1524
                        }
1525
                    }
1526
                    else if (owner.GetType() == typeof(Line))
1527
                    {
1528

    
1529
                    }
1530
                }
1531
                else
1532
                {
1533
                    LMItemNote _LMItemNote = null;
1534
                    LMAAttribute _LMAAttribute = null;
1535

    
1536
                    double x = 0;
1537
                    double y = 0;
1538

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

    
1541
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y);
1542
                    _LMSymbol.Commit();
1543
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1544
                    _LMItemNote.Commit();
1545
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1546
                    _LMAAttribute.set_Value(text.VALUE);
1547
                    _LMItemNote.Commit();
1548

    
1549
                    if (_LMAAttribute != null)
1550
                        ReleaseCOMObjects(_LMAAttribute);
1551
                    if (_LMItemNote != null)
1552
                        ReleaseCOMObjects(_LMItemNote);
1553
                }
1554
            }
1555
            catch (Exception ex)
1556
            {
1557

    
1558
            }
1559
            finally
1560
            {
1561
                if (_LMSymbol != null)
1562
                    ReleaseCOMObjects(_LMSymbol);
1563
            }
1564
        }
1565

    
1566
        private void NoteModeling(Note note)
1567
        {
1568
            LMSymbol _LMSymbol = null;
1569
            LMItemNote _LMItemNote = null;
1570
            LMAAttribute _LMAAttribute = null;
1571

    
1572
            try
1573
            {
1574
                double x = 0;
1575
                double y = 0;
1576

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

    
1579
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
1580
                _LMSymbol.Commit();
1581
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1582
                _LMItemNote.Commit();
1583
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1584
                _LMAAttribute.set_Value(note.VALUE);
1585
                _LMItemNote.Commit();
1586
            }
1587
            catch (Exception ex)
1588
            {
1589

    
1590
            }
1591
            finally
1592
            {
1593
                if (_LMAAttribute != null)
1594
                    ReleaseCOMObjects(_LMAAttribute);
1595
                if (_LMItemNote != null)
1596
                    ReleaseCOMObjects(_LMItemNote);
1597
                if (_LMSymbol != null)
1598
                    ReleaseCOMObjects(_LMSymbol);
1599
            }
1600
            
1601
        }
1602

    
1603
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
1604
        {
1605
            if (location == Location.None)
1606
            {
1607
                x = originX;
1608
                y = originY;
1609
            }
1610
            else
1611
            {
1612
                if (location.HasFlag(Location.Center))
1613
                {
1614
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
1615
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
1616
                }
1617

    
1618
                if (location.HasFlag(Location.Left))
1619
                    x = SPPIDLabelLocation.X1;
1620
                else if (location.HasFlag(Location.Right))
1621
                    x = SPPIDLabelLocation.X2;
1622

    
1623
                if (location.HasFlag(Location.Down))
1624
                    y = SPPIDLabelLocation.Y1;
1625
                else if (location.HasFlag(Location.Up))
1626
                    y = SPPIDLabelLocation.Y2;
1627
            }
1628
        }
1629

    
1630
        public void ReleaseCOMObjects(params object[] objVars)
1631
        {
1632
            int intNewRefCount = 0;
1633
            foreach (object obj in objVars)
1634
            {
1635
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
1636
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
1637
            }
1638
        }
1639
    }
1640
}
클립보드 이미지 추가 (최대 크기: 500 MB)