프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ b9e9f4c8

이력 | 보기 | 이력해설 | 다운로드 (68.1 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
            if (ownerObj !=null && ownerObj.GetType() == typeof(Line))
980
            {
981
                Line ownerLine = ownerObj as Line;
982
                LMLabelPersist _LmLabelPersist = null;
983
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(ownerLine.SPPID.ModelItemId);
984

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

    
993
                if (_LmLabelPersist != null)
994
                {
995
                    _LmLabelPersist.Commit();
996
                    ReleaseCOMObjects(_LmLabelPersist);
997
                }
998
                else
999
                {
1000
                    
1001
                }
1002

    
1003
                foreach (var item in connectorVertices)
1004
                    ReleaseCOMObjects(item.Key);
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
                LMConnector 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
                    {
1078
                        bool isZeroLength = Convert.ToBoolean(targetLMConnector.get_IsZeroLength());
1079
                        if (isZeroLength)
1080
                        {
1081
                            dynamic OID = targetLMConnector.get_GraphicOID();
1082
                            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1083
                            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1084
                            int verticesCount = lineStringGeometry.VertexCount;
1085
                            double[] vertices = null;
1086
                            lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1087
                            List<double[]> list = new List<double[]>();
1088
                            for (int i = 0; i < verticesCount; i++)
1089
                            {
1090
                                double x = 0;
1091
                                double y = 0;
1092
                                lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1093
                                list.Add(new double[] { x, y });
1094
                            }
1095

    
1096
                            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1097
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, list[0][0], list[0][1]);
1098
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, list[0][0], list[0][1]);
1099
                            _LMAItem _LMAItem = _placement.PIDCreateItem(@"\Piping\Routing\Process Lines\Primary Piping.sym");
1100

    
1101
                            _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1102
                            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1103

    
1104
                            array = new double[] { 0, list[0][0], list[0][1] };
1105
                            _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: _lMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1106
                            if (_LmLabelPersist != null)
1107
                            {
1108
                                _LmLabelPersist.Commit();
1109
                            }
1110
                            else
1111
                            {
1112

    
1113
                            }
1114
                        }
1115
                    }
1116
                }
1117
                
1118
                ReleaseCOMObjects(_LMSymbol);
1119
            }
1120
        }
1121

    
1122
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
1123
        {
1124
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
1125
            _LMAItem item1 = modelItem1.AsLMAItem();
1126
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
1127
            _LMAItem item2 = modelItem2.AsLMAItem();
1128
            
1129
            // item2가 item1으로 조인
1130
            try
1131
            {
1132
                _placement.PIDJoinRuns(ref item1, ref item2);
1133
                item1.Commit();
1134
                item2.Commit();
1135
            }
1136
            catch (Exception ex)
1137
            {
1138
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1139
            }
1140
            finally
1141
            {
1142
                ReleaseCOMObjects(modelItem1);
1143
                ReleaseCOMObjects(item1);
1144
                ReleaseCOMObjects(modelItem2);
1145
                ReleaseCOMObjects(item2);
1146
            }
1147
        }
1148

    
1149
        private void AutoJoinPipeRun(string modelItemId)
1150
        {
1151
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
1152
            _LMAItem item = modelItem.AsLMAItem();
1153
            try
1154
            {
1155
                string modelitemID = item.Id;
1156
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
1157
                string afterModelItemID = item.Id;
1158
                if (modelitemID != afterModelItemID)
1159
                {
1160
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
1161
                    foreach (var line in lines)
1162
                        line.SPPID.ModelItemId = afterModelItemID;
1163
                }
1164
                item.Commit();
1165
            }
1166
            catch (Exception ex)
1167
            {
1168
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1169
            }
1170
            finally
1171
            {
1172
                ReleaseCOMObjects(modelItem);
1173
                ReleaseCOMObjects(item);
1174
            }
1175
        }
1176

    
1177
        private void JoinRunLine(LineRun run)
1178
        {
1179
            string modelItemId = string.Empty;
1180
            foreach (var item in run.RUNITEMS)
1181
            {
1182
                if (item.GetType() == typeof(Line))
1183
                {
1184
                    Line line = item as Line;
1185
                    if (modelItemId != line.SPPID.ModelItemId)
1186
                    {
1187
                        AutoJoinPipeRun(line.SPPID.ModelItemId);
1188
                        modelItemId = line.SPPID.ModelItemId;
1189
                    }
1190
                }
1191
            }
1192
        }
1193

    
1194
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
1195
        {
1196
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
1197
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
1198

    
1199
            if (modelItem != null)
1200
            {
1201
                foreach (LMRepresentation rep in modelItem.Representations)
1202
                {
1203
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1204
                    {
1205
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1206
                        connectorVertices.Add(_LMConnector, new List<double[]>());
1207
                        dynamic OID = rep.get_GraphicOID();
1208
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1209
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1210
                        int verticesCount = lineStringGeometry.VertexCount;
1211
                        double[] vertices = null;
1212
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1213
                        for (int i = 0; i < verticesCount; i++)
1214
                        {
1215
                            double x = 0;
1216
                            double y = 0;
1217
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1218
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
1219
                        }
1220
                    }
1221
                }
1222

    
1223
                ReleaseCOMObjects(modelItem);
1224
            }
1225

    
1226
            return connectorVertices;
1227
        }
1228

    
1229
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
1230
        {
1231
            double length = double.MaxValue;
1232
            LMConnector targetConnector = null;
1233
            foreach (var item in connectorVertices)
1234
            {
1235
                List<double[]> points = item.Value;
1236
                for (int i = 0; i < points.Count - 1; i++)
1237
                {
1238
                    double[] point1 = points[i];
1239
                    double[] point2 = points[i + 1];
1240

    
1241
                    double maxLineX = Math.Max(point1[0], point2[0]);
1242
                    double minLineX = Math.Min(point1[0], point2[0]);
1243
                    double maxLineY = Math.Max(point1[1], point2[1]);
1244
                    double minLineY = Math.Min(point1[1], point2[1]);
1245

    
1246
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
1247

    
1248
                    // 두직선의 교차점
1249
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
1250
                    if (crossingPoint != null)
1251
                    {
1252
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
1253
                        if (length >= distance)
1254
                        {
1255
                            if (slope == SlopeType.Slope &&
1256
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
1257
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1258
                            {
1259
                                targetConnector = item.Key;
1260
                                length = distance;
1261
                            }
1262
                            else if (slope == SlopeType.HORIZONTAL &&
1263
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
1264
                            {
1265
                                targetConnector = item.Key;
1266
                                length = distance;
1267
                            }
1268
                            else if (slope == SlopeType.VERTICAL &&
1269
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1270
                            {
1271
                                targetConnector = item.Key;
1272
                                length = distance;
1273
                            }
1274
                        }
1275
                    }
1276
                }
1277
            }
1278

    
1279
            return targetConnector;
1280
        }
1281

    
1282
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1283
        {
1284
            double length = double.MaxValue;
1285
            LMConnector targetConnector = null;
1286
            foreach (var item in connectorVertices)
1287
            {
1288
                List<double[]> points = item.Value;
1289

    
1290
                foreach (double[] point in points)
1291
                {
1292
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1293
                    if (length >= distance)
1294
                    {
1295
                        targetConnector = item.Key;
1296
                        length = distance;
1297
                    }
1298
                }
1299
            }
1300

    
1301
            return targetConnector;
1302
        }
1303

    
1304
        private void LineNumberModeling(LineNumber lineNumber)
1305
        {
1306
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
1307
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
1308
            LMConnector connectedLMConnector = FindTargetLMConnectorByPoint(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
1309
            if (connectedLMConnector != null)
1310
            {
1311
                double x = 0;
1312
                double y = 0;
1313
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
1314

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

    
1319
                foreach (var item in connectorVertices)
1320
                    ReleaseCOMObjects(item.Key);
1321
                if (_LmLabelPresist != null)
1322
                {
1323
                    lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
1324
                    ReleaseCOMObjects(_LmLabelPresist);
1325
                }
1326
            }
1327
        }
1328

    
1329
        private void InputLineNumberAttribute(LineNumber lineNumber)
1330
        {
1331
            foreach (var run in lineNumber.RUNS)
1332
            {
1333
                foreach (var item in run.RUNITEMS)
1334
                {
1335
                    if (item.GetType() == typeof(Line))
1336
                    {
1337
                        Line line = item as Line;
1338
                        LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1339
                        if (_LMModelItem.get_ItemStatus() == "Active")
1340
                        {
1341
                            foreach (var attribute in lineNumber.ATTRIBUTES)
1342
                            {
1343
                                LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1344
                                if (mapping != null)
1345
                                {
1346
                                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1347
                                    if (_LMAAttribute != null)
1348
                                    {
1349
                                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1350
                                            _LMAAttribute.set_Value(attribute.VALUE);
1351
                                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
1352
                                            _LMAAttribute.set_Value(attribute.VALUE);
1353
                                    }
1354
                                }
1355
                            }
1356
                            _LMModelItem.Commit();
1357
                            break;
1358
                        }
1359
                        ReleaseCOMObjects(_LMModelItem);
1360
                    }
1361
                }
1362
            }
1363
        }
1364

    
1365
        private void InputSymbolAttribute(Symbol symbol)
1366
        {
1367
            try
1368
            {
1369
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1370
                {
1371
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1372
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1373
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
1374

    
1375
                    foreach (var item in symbol.PROPERTIES)
1376
                    {
1377

    
1378

    
1379
                    }
1380

    
1381
                    foreach (var item in symbol.ATTRIBUTES)
1382
                    {
1383
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
1384
                        if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
1385
                        {
1386
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
1387
                            if (_Attribute != null)
1388
                            {
1389
                                // 임시
1390
                                if (item.ATTRIBUTETYPE == "String")
1391
                                {
1392
                                    if (!string.IsNullOrEmpty(item.VALUE))
1393
                                    {
1394
                                        if (!DBNull.Value.Equals(_Attribute.get_Value()) && !string.IsNullOrEmpty(_Attribute.get_Value()))
1395
                                        {
1396
                                            string value = _Attribute.get_Value() + "\n" + item.VALUE;
1397
                                            _Attribute.set_Value(value);
1398
                                        }
1399
                                        else
1400
                                        {
1401
                                            _Attribute.set_Value(item.VALUE);
1402
                                        }
1403
                                    }
1404
                                }
1405
                                else
1406
                                {
1407
                                    _Attribute.set_Value(item.VALUE);
1408
                                }
1409
                            }
1410
                        }
1411
                    }
1412

    
1413
                    foreach (var item in symbol.ASSOCIATIONS)
1414
                    {
1415

    
1416
                    }
1417

    
1418
                    ReleaseCOMObjects(_LMSymbol);
1419
                    ReleaseCOMObjects(_Attributes);
1420
                    ReleaseCOMObjects(_LMModelItem);
1421
                }
1422
            }
1423
            catch (Exception ex)
1424
            {
1425
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1426
            }
1427
        }
1428

    
1429
        private void TextModeling(Text text)
1430
        {
1431
            LMSymbol _LMSymbol = null;
1432
            try
1433
            {
1434
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
1435
                if (text.ASSOCIATION)
1436
                {
1437
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
1438
                    if (owner.GetType() == typeof(Symbol))
1439
                    {
1440
                        Symbol symbol = owner as Symbol;
1441
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1442
                        if (_LMSymbol != null)
1443
                        {
1444
                            Association association = symbol.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
1445
                            List<BaseModel.Attribute> attributes = symbol.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
1446
                            AttributeMapping mapping = null;
1447
                            foreach (var attribute in attributes)
1448
                            {
1449
                                if (string.IsNullOrEmpty(attribute.VALUE) || attribute.VALUE == "None")
1450
                                    continue;
1451

    
1452
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
1453
                                if (mapping != null)
1454
                                    break;  
1455
                            }
1456

    
1457
                            if (mapping != null)
1458
                            {
1459
                                double x = 0;
1460
                                double y = 0;
1461

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

    
1465
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: true);
1466
                                if (_LMLabelPersist!=null)
1467
                                {
1468
                                    _LMLabelPersist.Commit();
1469
                                    ReleaseCOMObjects(_LMLabelPersist);
1470
                                }
1471
                            }
1472
                        }
1473
                    }
1474
                    else if (owner.GetType() == typeof(Line))
1475
                    {
1476

    
1477
                    }
1478
                }
1479
                else
1480
                {
1481
                    LMItemNote _LMItemNote = null;
1482
                    LMAAttribute _LMAAttribute = null;
1483

    
1484
                    double x = 0;
1485
                    double y = 0;
1486

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

    
1489
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y);
1490
                    _LMSymbol.Commit();
1491
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1492
                    _LMItemNote.Commit();
1493
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1494
                    _LMAAttribute.set_Value(text.VALUE);
1495
                    _LMItemNote.Commit();
1496

    
1497
                    if (_LMAAttribute != null)
1498
                        ReleaseCOMObjects(_LMAAttribute);
1499
                    if (_LMItemNote != null)
1500
                        ReleaseCOMObjects(_LMItemNote);
1501
                }
1502
            }
1503
            catch (Exception ex)
1504
            {
1505

    
1506
            }
1507
            finally
1508
            {
1509
                if (_LMSymbol != null)
1510
                    ReleaseCOMObjects(_LMSymbol);
1511
            }
1512
        }
1513

    
1514
        private void NoteModeling(Note note)
1515
        {
1516
            LMSymbol _LMSymbol = null;
1517
            LMItemNote _LMItemNote = null;
1518
            LMAAttribute _LMAAttribute = null;
1519

    
1520
            try
1521
            {
1522
                double x = 0;
1523
                double y = 0;
1524

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

    
1527
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
1528
                _LMSymbol.Commit();
1529
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1530
                _LMItemNote.Commit();
1531
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1532
                _LMAAttribute.set_Value(note.VALUE);
1533
                _LMItemNote.Commit();
1534
            }
1535
            catch (Exception ex)
1536
            {
1537

    
1538
            }
1539
            finally
1540
            {
1541
                if (_LMAAttribute != null)
1542
                    ReleaseCOMObjects(_LMAAttribute);
1543
                if (_LMItemNote != null)
1544
                    ReleaseCOMObjects(_LMItemNote);
1545
                if (_LMSymbol != null)
1546
                    ReleaseCOMObjects(_LMSymbol);
1547
            }
1548
            
1549
        }
1550

    
1551
        public void ReleaseCOMObjects(params object[] objVars)
1552
        {
1553
            int intNewRefCount = 0;
1554
            foreach (object obj in objVars)
1555
            {
1556
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
1557
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
1558
            }
1559
        }
1560

    
1561
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
1562
        {
1563
            if (location == Location.None)
1564
            {
1565
                x = originX;
1566
                y = originY;
1567
            }
1568
            else
1569
            {
1570
                if (location.HasFlag(Location.Center))
1571
                {
1572
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
1573
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
1574
                }
1575

    
1576
                if (location.HasFlag(Location.Left))
1577
                    x = SPPIDLabelLocation.X1;
1578
                else if (location.HasFlag(Location.Right))
1579
                    x = SPPIDLabelLocation.X2;
1580

    
1581
                if (location.HasFlag(Location.Down))
1582
                    y = SPPIDLabelLocation.Y1;
1583
                else if (location.HasFlag(Location.Up))
1584
                    y = SPPIDLabelLocation.Y2;
1585
            }
1586
        }
1587
    }
1588
}
클립보드 이미지 추가 (최대 크기: 500 MB)