프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ ca214bc3

이력 | 보기 | 이력해설 | 다운로드 (78.8 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
using DevExpress.XtraSplashScreen;
25
namespace Converter.SPPID
26
{
27
    public class AutoModeling
28
    {
29
        Placement _placement;
30
        LMADataSource dataSource;
31
        dynamic newDrawing;
32
        dynamic application;
33
        Ingr.RAD2D.Application radApp;
34
        SPPID_Document document;
35
        ETCSetting _ETCSetting;
36

    
37
        List<Tuple<string, Line, Line>> BranchLines = new List<Tuple<string, Line, Line>>();
38
        public AutoModeling(SPPID_Document document, dynamic application, Ingr.RAD2D.Application radApp)
39
        {
40
            this.document = document;
41
            this.application = application;
42
            this.radApp = radApp;
43
            this._ETCSetting = ETCSetting.GetInstance();
44
        }
45

    
46
        public void Run()
47
        {
48
            try
49
            {
50
                _placement = new Placement();
51
                dataSource = _placement.PIDDataSource;
52

    
53
                newDrawing = application.Drawings.Add(document.Unit, document.Template, document.DrawingNumber, document.DrawingName);
54
                application.ActiveWindow.Fit();
55
                Thread.Sleep(500);
56
                application.ActiveWindow.Zoom = 2000;
57
                Thread.Sleep(1000);
58

    
59
                double maxX = 0;
60
                double maxY = 0;
61
                foreach (object drawingObj in radApp.ActiveDocument.ActiveSheet.DrawingObjects)
62
                {
63
                    Ingr.RAD2D.SmartFrame2d smartFrame2d = drawingObj as Ingr.RAD2D.SmartFrame2d;
64
                    if (smartFrame2d != null)
65
                    {
66
                        double x1 = 0;
67
                        double x2 = 0;
68
                        double y1 = 0;
69
                        double y2 = 0;
70
                        smartFrame2d.Range(out x1, out y1, out x2, out y2);
71
                        maxX = Math.Max(x2, maxX);
72
                        maxY = Math.Max(y2, maxY);
73
                    }
74
                }
75
                if (maxX != 0 && maxY != 0)
76
                {
77
                    document.SetSPPIDLocation(maxX, maxY);
78

    
79
                    // Equipment Modeling
80
                    foreach (Equipment equipment in document.Equipments)
81
                        EquipmentModeling(equipment);
82

    
83
                    // LineRun Symbol Modeling
84
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
85
                        foreach (LineRun run in lineNumber.RUNS)
86
                            SymbolModelingByRun(run);
87

    
88
                    // TrimLineRun Symbol Modeling
89
                    foreach (TrimLine trimLine in document.TRIMLINES)
90
                        foreach (LineRun run in trimLine.RUNS)
91
                            SymbolModelingByRun(run);
92

    
93
                    // LineRun Line Modeling
94
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
95
                        foreach (LineRun run in lineNumber.RUNS)
96
                            LineModelingByRun(run);
97

    
98
                    // TrimLineRun Line Modeling
99
                    foreach (TrimLine trimLine in document.TRIMLINES)
100
                        foreach (LineRun run in trimLine.RUNS)
101
                            LineModelingByRun(run);
102

    
103
                    // Branch Line Modeling
104
                    foreach (var item in BranchLines)
105
                        BranchLineModeling(item);
106

    
107
                    // EndBreak Modeling
108
                    foreach (var item in document.EndBreaks)
109
                        EndBreakModeling(item);
110

    
111
                    // LineNumber Modeling
112
                    foreach (var item in document.LINENUMBERS)
113
                        LineNumberModeling(item);
114

    
115
                    // Note Modeling
116
                    foreach (var item in document.NOTES)
117
                        NoteModeling(item);
118

    
119
                    // Text Modeling
120
                    foreach (var item in document.TEXTINFOS)
121
                        TextModeling(item);
122

    
123
                    // LineRun Line Join
124
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
125
                        foreach (LineRun run in lineNumber.RUNS)
126
                            JoinRunLine(run);
127

    
128
                    // TrimLineRun Line Join
129
                    foreach (TrimLine trimLine in document.TRIMLINES)
130
                        foreach (LineRun run in trimLine.RUNS)
131
                            JoinRunLine(run);
132

    
133
                    // Input LineNumber Attribute
134
                    foreach (var item in document.LINENUMBERS)
135
                        InputLineNumberAttribute(item);
136

    
137
                    // Input Symbol Attribute
138
                    foreach (var item in document.SYMBOLS)
139
                        InputSymbolAttribute(item);
140

    
141
                    #region 임시 Label
142
                    foreach (var item in document.SYMBOLS)
143
                    {
144
                        if (item.SPPID.MAPPINGNAME.Contains("Labels - "))
145
                        {
146
                            SPPIDSymbolInfo info = item.SPPID;
147
                            Array points = new double[] { 0, item.SPPID.ORIGINAL_X, item.SPPID.ORIGINAL_Y };
148
                            BaseModel.Attribute itemAttribute = item.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
149
                            if (itemAttribute == null)
150
                                continue;
151
                            string symbolUID = item.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL").VALUE;
152
                            object objectItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
153
                            if (objectItem != null)
154
                            {
155

    
156
                                string sRep = null;
157
                                if (objectItem.GetType() == typeof(Symbol))
158
                                    sRep = ((Symbol)objectItem).SPPID.RepresentationId;
159
                                else if (objectItem.GetType() == typeof(Equipment))
160
                                    sRep = ((Equipment)objectItem).SPPID.RepresentationId;
161

    
162
                                if (!string.IsNullOrEmpty(sRep))
163
                                {
164
                                    LMSymbol _TargetItem = dataSource.GetSymbol(sRep);
165
                                    LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(item.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: _TargetItem.AsLMRepresentation(), IsLeaderVisible: true);
166
                                    LMModelItem _LMModelItem = _TargetItem.ModelItemObject;
167
                                    LMAAttributes _Attributes = _LMModelItem.Attributes;
168

    
169
                                    foreach (var attribute in item.ATTRIBUTES)
170
                                    {
171
                                        if (!string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
172
                                        {
173
                                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
174
                                            if (mapping != null)
175
                                            {
176
                                                LMAAttribute _LMAAttribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
177
                                                if (mapping.SPPIDATTRIBUTENAME.Contains("Description"))
178
                                                {
179
                                                    if (!DBNull.Value.Equals(_LMAAttribute.get_Value()) && !string.IsNullOrEmpty(_LMAAttribute.get_Value()))
180
                                                    {
181
                                                        string value = _LMAAttribute.get_Value() + "\n" + attribute.VALUE;
182
                                                        _LMAAttribute.set_Value(value);
183
                                                    }
184
                                                    else
185
                                                    {
186
                                                        _LMAAttribute.set_Value(attribute.VALUE);
187
                                                    }
188
                                                }
189
                                                else if (_LMAAttribute != null)
190
                                                    _LMAAttribute.set_Value(attribute.VALUE);
191
                                            }
192
                                        }
193
                                    }
194

    
195
                                    string OID = _LMLabelPresist.get_GraphicOID();
196
                                    DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
197
                                    if (dependency != null)
198
                                    {
199
                                        bool result = false;
200
                                        foreach (var attributes in dependency.AttributeSets)
201
                                        {
202
                                            foreach (var attribute in attributes)
203
                                            {
204
                                                string name = attribute.Name;
205
                                                string value = attribute.GetValue().ToString();
206
                                                if (name == "DrawingItemType" && value == "LabelPersist")
207
                                                {
208
                                                    foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
209
                                                    {
210
                                                        if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
211
                                                        {
212
                                                            Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
213
                                                            double prevX = _TargetItem.get_XCoordinate();
214
                                                            double prevY = _TargetItem.get_YCoordinate();
215
                                                            lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
216
                                                            lineString2D.RemoveVertex(lineString2D.VertexCount);
217
                                                            result = true;
218
                                                            break;
219
                                                        }
220
                                                    }
221
                                                }
222

    
223
                                                if (result)
224
                                                    break;
225
                                            }
226

    
227
                                            if (result)
228
                                                break;
229
                                        }
230
                                    }
231

    
232

    
233
                                    _LMModelItem.Commit();
234
                                    _LMLabelPresist.Commit();
235
                                    ReleaseCOMObjects(_TargetItem);
236
                                    ReleaseCOMObjects(_LMLabelPresist);
237
                                    ReleaseCOMObjects(_LMModelItem);
238
                                    ReleaseCOMObjects(_Attributes);
239
                                }
240
                            }
241
                        }
242
                    }
243
                    #endregion
244
                }
245
            }
246
            catch (Exception ex)
247
            {
248
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
249
            }
250
            finally
251
            {
252
                application.ActiveWindow.Fit();
253
                
254
                if (newDrawing != null)
255
                {
256
                    radApp.ActiveDocument.SaveOnClose = false;
257
                    radApp.ActiveDocument.Save();
258
                    ReleaseCOMObjects(newDrawing);
259
                }
260

    
261
                ReleaseCOMObjects(dataSource);
262
                ReleaseCOMObjects(_placement);
263
            }
264
        }
265

    
266
        private void LineModelingByRun(LineRun run)
267
        {
268
            Line prevLine = null;
269
            List<Line> lines = new List<Line>();
270
            foreach (var item in run.RUNITEMS)
271
            {
272
                // Line일 경우
273
                if (item.GetType() == typeof(Line))
274
                {
275
                    Line line = item as Line;
276
                    if (prevLine == null)
277
                        lines.Add(line);
278
                    else if (prevLine != null)
279
                    {
280
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
281
                            lines.Add(line);
282
                        else
283
                        {
284
                            if (lines.Count > 0)
285
                            {
286
                                LineModeling(lines);
287
                                lines.Clear();
288
                            }
289
                            lines.Add(line);
290
                        }
291
                    }
292

    
293
                    prevLine = line;
294
                }
295
                // Symbol 일 경우
296
                else if (item.GetType() == typeof(Symbol))
297
                {
298
                    if (lines.Count > 0)
299
                    {
300
                        LineModeling(lines);
301
                        lines.Clear();
302
                    }
303
                }
304
            }
305

    
306
            if (lines.Count > 0)
307
                LineModeling(lines);
308
        }
309

    
310
        private void SymbolModelingByRun(LineRun run)
311
        {
312
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
313
            if (run.RUNITEMS.Count > 0)
314
            {
315
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
316
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
317

    
318
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
319
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
320
            }
321

    
322
            Symbol prevSymbol = null;
323
            Symbol targetSymbol = null;
324
            foreach (var item in run.RUNITEMS)
325
            {
326
                if (item.GetType() == typeof(Symbol))
327
                {
328
                    Symbol symbol = item as Symbol;
329
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
330
                    prevSymbol = symbol;
331
                    targetSymbol = symbol;
332
                }
333
                else
334
                {
335
                    targetSymbol = null;
336
                }
337
            }
338
        }
339

    
340
        private void SymbolModelingByRunStart(Symbol symbol, LineRun run)
341
        {
342
            foreach (var connector in symbol.CONNECTORS)
343
            {
344
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
345
                if (targetItem != null &&
346
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
347
                    !IsSameLineNumber(symbol, targetItem))
348
                {
349
                    SymbolModeling(symbol, targetItem as Symbol, null);
350
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
351
                    {
352
                        object item = run.RUNITEMS[i];
353
                        if (item.GetType() == typeof(Symbol))
354
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol, null);
355
                        else
356
                            break;
357
                    }
358
                    break;
359
                }
360
            }
361

    
362

    
363
        }
364

    
365
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
366
        {
367
            foreach (var connector in symbol.CONNECTORS)
368
            {
369
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
370
                if (targetItem != null &&
371
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
372
                    !IsSameLineNumber(symbol, targetItem))
373
                {
374
                    SymbolModeling(symbol, targetItem as Symbol, null);
375
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
376
                    {
377
                        object item = run.RUNITEMS[i];
378
                        if (item.GetType() == typeof(Symbol))
379
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol, null);
380
                        else
381
                            break;
382
                    }
383
                    break;
384
                }
385
            }
386
        }
387

    
388
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
389
        {
390
            // 임시
391
            if (symbol.SPPID.MAPPINGNAME.Contains("Labels - "))
392
                return;
393

    
394
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
395
                return;
396

    
397
            LMSymbol _LMSymbol = null;
398

    
399
            string mappingPath = symbol.SPPID.MAPPINGNAME;
400
            double x = symbol.SPPID.ORIGINAL_X;
401
            double y = symbol.SPPID.ORIGINAL_Y;
402
            int mirror = 0;
403
            double angle = symbol.ANGLE;
404
            // 임시
405
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
406
            {
407
                mirror = 1;
408
            }
409
                
410

    
411
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
412
            {
413
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
414

    
415
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
416
                double x1 = 0;
417
                double x2 = 0;
418
                double y1 = 0;
419
                double y2 = 0;
420
                symbol2d.Range(out x1, out y1, out x2, out y2);
421

    
422
                if (y2 < y)
423
                    y = y2;
424
                else if (y1 > y)
425
                    y = y1;
426

    
427
                if (x2 < x)
428
                    x = x2;
429
                else if (x1 > x)
430
                    x = x1;
431

    
432
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
433
                ReleaseCOMObjects(_TargetItem);
434
            }
435
            else if (prevSymbol != null)
436
            {
437
                LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
438
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
439
                double prevX = _PrevSymbol.get_XCoordinate();
440
                double prevY = _PrevSymbol.get_YCoordinate();
441
                if (slopeType == SlopeType.HORIZONTAL)
442
                    y = prevY;
443
                else if (slopeType == SlopeType.VERTICAL)
444
                    x = prevX;
445
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
446
            }
447
            else
448
            {
449
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
450
            }
451

    
452

    
453
            if (_LMSymbol != null)
454
            {
455
                _LMSymbol.Commit();
456
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
457

    
458
                foreach (var item in symbol.ChildSymbols)
459
                    CreateChildSymbol(item, _LMSymbol);
460
            }
461

    
462
            ReleaseCOMObjects(_LMSymbol);
463
        }
464

    
465
        private void EquipmentModeling(Equipment equipment)
466
        {
467
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
468
                return;
469

    
470
            LMSymbol _LMSymbol = null;
471
            LMSymbol targetItem = null;
472
            string mappingPath = equipment.SPPID.MAPPINGNAME;
473
            double x = equipment.SPPID.ORIGINAL_X;
474
            double y = equipment.SPPID.ORIGINAL_Y;
475
            int mirror = 0;
476
            double angle = equipment.ANGLE;
477

    
478
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
479
            if (connector != null)
480
            {
481
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
482
                if (connEquipment != null)
483
                {
484
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
485
                        EquipmentModeling(connEquipment);
486

    
487
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
488
                    {
489
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
490
                        if (targetItem != null)
491
                        {
492
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
493
                        }
494
                        else
495
                        {
496
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
497
                        }
498
                    }
499
                    else
500
                    {
501
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
502
                    }
503
                }
504
                else
505
                {
506
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
507
                }
508
            }
509
            else
510
            {
511
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
512
            }
513

    
514
            if (_LMSymbol != null)
515
            {
516
                _LMSymbol.Commit();
517
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
518
                ReleaseCOMObjects(_LMSymbol);
519
            }
520

    
521
            if (targetItem != null)
522
            {
523
                ReleaseCOMObjects(targetItem);
524
            }
525
            
526
            //if (equipment.CONNECTORS)
527
            //{
528

    
529
            //}
530

    
531
            //if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
532
            //{
533
            //    LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
534
            //    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
535
            //    ReleaseCOMObjects(_TargetItem);
536
            //}
537
            //else if (prevSymbol != null)
538
            //{
539
            //    LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
540
            //    SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
541
            //    double prevX = _PrevSymbol.get_XCoordinate();
542
            //    double prevY = _PrevSymbol.get_YCoordinate();
543
            //    if (slopeType == SlopeType.HORIZONTAL)
544
            //        y = prevY;
545
            //    else if (slopeType == SlopeType.VERTICAL)
546
            //        x = prevX;
547
            //    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
548
            //}
549
            //else
550
            //{
551
            //    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
552
            //}
553

    
554

    
555
            //if (_LMSymbol != null)
556
            //{
557
            //    _LMSymbol.Commit();
558
            //    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
559

    
560
            //    foreach (var item in symbol.ChildSymbols)
561
            //        CreateChildSymbol(item, _LMSymbol);
562
            //}
563

    
564
            ReleaseCOMObjects(_LMSymbol);
565
        }
566

    
567
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
568
        {
569
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
570
            double x1 = 0;
571
            double x2 = 0;
572
            double y1 = 0;
573
            double y2 = 0;
574
            symbol2d.Range(out x1, out y1, out x2, out y2);
575

    
576
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
577
            if (_LMSymbol != null)
578
            {
579
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
580
                foreach (var item in childSymbol.ChildSymbols)
581
                    CreateChildSymbol(item, _LMSymbol);
582
            }
583
            
584

    
585
            ReleaseCOMObjects(_LMSymbol);
586
        }
587

    
588
        private bool IsSameLineNumber(object item, object targetItem)
589
        {
590
            foreach (var lineNumber in document.LINENUMBERS)
591
            {
592
                foreach (var run in lineNumber.RUNS)
593
                {
594
                    foreach (var runItem in run.RUNITEMS)
595
                    {
596
                        if (runItem == item)
597
                        {
598
                            foreach (var findItem in run.RUNITEMS)
599
                            {
600
                                if (findItem == targetItem)
601
                                {
602
                                    return true;
603
                                }
604
                            }
605

    
606
                            return false;
607

    
608
                        }
609
                    }
610
                }
611
            }
612

    
613
            return false;
614
        }
615

    
616
        private void LineModeling(List<Line> lines)
617
        {
618
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
619
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
620
            LMSymbol _LMSymbol1 = null;
621
            LMSymbol _LMSymbol2 = null;
622
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
623
            LMConnector targetConnector1 = null;
624
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
625
            LMConnector targetConnector2 = null;
626

    
627
            Line startBranchLine = null;
628
            Line endBranchLine = null;
629

    
630
            for (int i = 0; i < lines.Count; i++)
631
            {
632
                Line line = lines[i];
633
                if (i == 0 || i + 1 != lines.Count)
634
                {
635
                    // 시작점에 연결된 Symbol 찾기
636
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
637
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
638
                    {
639
                        _LMSymbol1 = GetTargetSymbol(connItem as Symbol, line);
640
                        if (_LMSymbol1 != null)
641
                            placeRunInputs.AddSymbolTarget(_LMSymbol1, line.SPPID.START_X, line.SPPID.START_Y);
642
                        else
643
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
644
                    }
645
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
646
                    {
647
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
648
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
649

    
650
                        if (targetConnector1 != null)
651
                            placeRunInputs.AddConnectorTarget(targetConnector1, line.SPPID.START_X, line.SPPID.START_Y);
652
                        else
653
                        {
654
                            startBranchLine = connItem as Line;
655
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
656
                        }
657
                    }
658
                    else
659
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
660
                }
661

    
662
                if (i + 1 == lines.Count)
663
                {
664
                    // 끝점에 연결된 Symbol 찾기
665
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
666

    
667
                    if (i != 0)
668
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
669

    
670
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
671
                    {
672
                        _LMSymbol2 = GetTargetSymbol(connItem as Symbol, line);
673
                        if (_LMSymbol2 != null)
674
                            placeRunInputs.AddSymbolTarget(_LMSymbol2, line.SPPID.END_X, line.SPPID.END_Y);
675
                        else
676
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
677
                            
678
                    }
679
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
680
                    {
681
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
682
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
683

    
684
                        if (targetConnector2 != null)
685
                            placeRunInputs.AddConnectorTarget(targetConnector2, line.SPPID.END_X, line.SPPID.END_Y);
686
                        else
687
                        {
688
                            endBranchLine = connItem as Line;
689
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
690
                        }
691
                    }
692
                    else
693
                    {
694
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
695
                    }
696
                }
697
            }
698

    
699
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
700

    
701
            if (_lMConnector != null)
702
            {
703
                foreach (var line in lines)
704
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
705
                _lMConnector.Commit();
706
                if (startBranchLine != null || endBranchLine != null)
707
                {
708
                    BranchLines.Add(new Tuple<string, Line, Line>(_lMConnector.ModelItemID, startBranchLine, endBranchLine));
709
                }
710
            }
711

    
712

    
713
            if (_LMSymbol1 != null)
714
                ReleaseCOMObjects(_LMSymbol1);
715
            if (_LMSymbol2 != null)
716
                ReleaseCOMObjects(_LMSymbol2);
717
            if (targetConnector1 != null)
718
                ReleaseCOMObjects(targetConnector1);
719
            if (targetConnector2 != null)
720
                ReleaseCOMObjects(targetConnector2);
721
            foreach (var item in connectorVertices1)
722
                ReleaseCOMObjects(item.Key);
723
            foreach (var item in connectorVertices2)
724
                ReleaseCOMObjects(item.Key);
725

    
726
            ReleaseCOMObjects(_lMConnector);
727
            ReleaseCOMObjects(placeRunInputs);
728
            ReleaseCOMObjects(_LMAItem);
729
        }
730

    
731
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
732
        {
733
            LMSymbol _LMSymbol = null;
734
            foreach (var connector in symbol.CONNECTORS)
735
            {
736
                if (connector.CONNECTEDITEM == line.UID)
737
                {
738
                    if (connector.Index == 0)
739
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
740
                    else
741
                    {
742
                        ChildSymbol child = null;
743
                        foreach (var childSymbol in symbol.ChildSymbols)
744
                        {
745
                            if (childSymbol.Connectors.Contains(connector))
746
                                child = childSymbol;
747
                            else
748
                                child = GetChildSymbolByConnector(childSymbol, connector);
749

    
750
                            if (child != null)
751
                                break;
752
                        }
753

    
754
                        if (child != null)
755
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
756
                    }
757

    
758
                    break;  
759
                }
760
            }
761

    
762
            return _LMSymbol;
763
        }
764

    
765
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
766
        {
767
            foreach (var childSymbol in item.ChildSymbols)
768
            {
769
                if (childSymbol.Connectors.Contains(connector))
770
                    return childSymbol;
771
                else
772
                    return GetChildSymbolByConnector(childSymbol, connector);
773
            }
774

    
775
            return null;
776
        }
777

    
778
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
779
        {
780
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
781
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
782

    
783
            LMConnector _StartConnector = null;
784
            LMConnector _EndConnector = null;
785
            double lengthStart = double.MaxValue;
786
            double lengthEnd = double.MaxValue;
787
            List<double[]> startPoints = new List<double[]>();
788
            List<double[]> endPoints = new List<double[]>();
789

    
790
            foreach (var item in connectorVertices)
791
            {
792
                foreach (var point in item.Value)
793
                {
794
                    // Start Point가 Branch
795
                    if (branch.Item2 != null)
796
                    {
797
                        Line targetLine = branch.Item2;
798
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
799
                        if (lengthStart > distance)
800
                        {
801
                            _StartConnector = item.Key;
802
                            lengthStart = distance;
803
                            startPoints = item.Value;
804
                        }
805
                    }
806
                    // End Point가 Branch
807
                    if (branch.Item3 != null)
808
                    {
809
                        Line targetLine = branch.Item3;
810
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
811
                        if (lengthEnd > distance)
812
                        {
813
                            _EndConnector = item.Key;
814
                            lengthEnd = distance;
815
                            endPoints = item.Value;
816
                        }
817
                    }
818
                }
819
            }
820
            #region Branch가 양쪽 전부일 때
821
            if (_StartConnector != null && _StartConnector == _EndConnector)
822
            {
823
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
824

    
825
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
826
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
827

    
828
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
829
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
830
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
831
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
832
                   startPoints[startPoints.Count - 1][0],
833
                   startPoints[startPoints.Count - 1][1],
834
                   startPoints[startPoints.Count - 2][0],
835
                   startPoints[startPoints.Count - 2][1]);
836

    
837
                for (int i = 0; i < startPoints.Count; i++)
838
                {
839
                    double[] point = startPoints[i];
840
                    if (i == 0)
841
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
842
                    else if (i == startPoints.Count - 1)
843
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
844
                    else
845
                        placeRunInputs.AddPoint(point[0], point[1]);
846
                }
847

    
848
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
849
                if (_LMConnector != null)
850
                {
851
                    _LMConnector.Commit();
852
                    foreach (var item in lines)
853
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
854
                }
855

    
856
                foreach (var item in startConnectorVertices)
857
                    ReleaseCOMObjects(item.Key);
858
                foreach (var item in endConnectorVertices)
859
                    ReleaseCOMObjects(item.Key);
860
                ReleaseCOMObjects(placeRunInputs);
861
                ReleaseCOMObjects(_LMAItem);
862
                ReleaseCOMObjects(_LMConnector);
863
            }
864
            #endregion
865
            #region 양쪽이 다른 Branch 
866
            else
867
            {
868
                // Branch 시작 Connector
869
                if (_StartConnector != null)
870
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
871

    
872
                // Branch 끝 Connector
873
                if (_EndConnector != null)
874
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
875
            }
876
            #endregion
877

    
878
            if (_StartConnector != null)
879
                ReleaseCOMObjects(_StartConnector);
880
            if (_EndConnector != null)
881
                ReleaseCOMObjects(_EndConnector);
882
            foreach (var item in connectorVertices)
883
                ReleaseCOMObjects(item.Key);
884
        }
885

    
886
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
887
        {
888
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
889
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
890
            LMConnector _SameRunTargetConnector = null;
891
            LMSymbol _SameRunTargetSymbol = null;
892
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
893
            LMConnector _BranchTargetConnector = null;
894
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
895

    
896
            // 같은 Line Run의 Connector 찾기
897
            foreach (var item in connectorVertices)
898
            {
899
                if (item.Key == _Connector)
900
                    continue;
901

    
902
                if (IsStart &&
903
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
904
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
905
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
906
                {
907
                    _SameRunTargetConnector = item.Key;
908
                    break;
909
                }
910
                else if (!IsStart &&
911
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
912
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
913
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
914
                {
915
                    _SameRunTargetConnector = item.Key;
916
                    break;
917
                }
918
            }
919

    
920
            // Branch 반대편이 Symbol
921
            if (_SameRunTargetConnector == null)
922
            {
923
                foreach (var line in lines)
924
                {
925
                    foreach (var connector in line.CONNECTORS)
926
                    {
927
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
928
                        if (symbol != null)
929
                        {
930
                            _SameRunTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
931
                            break;
932
                        }
933
                    }
934
                }
935
            }
936

    
937
            // 기존 Connector 제거
938
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
939
            
940
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
941
            if (IsStart)
942
            {
943
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
944
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
945
            }
946
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
947
            else
948
            {
949
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
950
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
951
                    points[points.Count - 1][0],
952
                    points[points.Count - 1][1],
953
                    points[points.Count - 2][0],
954
                    points[points.Count - 2][1]);
955
            }
956

    
957
            for (int i = 0; i < points.Count; i++)
958
            {
959
                double[] point = points[i];
960
                if (i == 0)
961
                {
962
                    if (IsStart)
963
                    {
964
                        placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
965
                    }
966
                    else
967
                    {
968
                        if (_SameRunTargetConnector != null)
969
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
970
                        else if (_SameRunTargetSymbol != null)
971
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
972
                        else
973
                            placeRunInputs.AddPoint(point[0], point[1]);
974
                    }
975
                }
976
                else if (i == points.Count - 1)
977
                {
978
                    if (IsStart)
979
                    {
980
                        if (_SameRunTargetConnector != null)
981
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
982
                        else if (_SameRunTargetSymbol != null)
983
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
984
                        else
985
                            placeRunInputs.AddPoint(point[0], point[1]);
986
                    }
987
                    else
988
                    {
989
                        if (_BranchTargetConnector != null)
990
                        {
991
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
992
                        }
993
                    }
994
                }
995
                else
996
                    placeRunInputs.AddPoint(point[0], point[1]);
997
            }
998
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
999
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1000

    
1001
            if (_LMConnector != null)
1002
            {
1003
                if (_SameRunTargetConnector != null)
1004
                {
1005
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
1006
                }
1007
                else
1008
                {
1009
                    foreach (var item in lines)
1010
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
1011
                }
1012

    
1013
                _LMConnector.Commit();
1014
                ReleaseCOMObjects(_LMConnector);
1015
            }
1016

    
1017
            ReleaseCOMObjects(placeRunInputs);
1018
            ReleaseCOMObjects(_LMAItem);
1019
            if (_BranchTargetConnector != null)
1020
                ReleaseCOMObjects(_BranchTargetConnector);
1021
            if (_SameRunTargetConnector != null)
1022
                ReleaseCOMObjects(_SameRunTargetConnector);
1023
            if (_SameRunTargetSymbol != null)
1024
                ReleaseCOMObjects(_SameRunTargetSymbol);
1025
            foreach (var item in connectorVertices)
1026
                ReleaseCOMObjects(item.Key);
1027
            foreach (var item in branchConnectorVertices)
1028
                ReleaseCOMObjects(item.Key);
1029
        }
1030

    
1031
        private void EndBreakModeling(EndBreak endBreak)
1032
        {
1033
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
1034
            LMConnector targetLMConnector = null;
1035
            if (ownerObj !=null && ownerObj.GetType() == typeof(Line))
1036
            {
1037
                Line ownerLine = ownerObj as Line;
1038
                LMLabelPersist _LmLabelPersist = null;
1039
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(ownerLine.SPPID.ModelItemId);
1040

    
1041
                targetLMConnector = FindTargetLMConnectorByPoint(connectorVertices, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1042
                
1043
                if (targetLMConnector != null)
1044
                {
1045
                    //double[] point = connectorVertices[targetLMConnector][connectorVertices[targetLMConnector].Count - 1];
1046
                    //Array array = new double[] { 0, point[0], point[1] };
1047
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1048
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1049
                }
1050

    
1051
                if (_LmLabelPersist != null)
1052
                {
1053
                    _LmLabelPersist.Commit();
1054
                    ReleaseCOMObjects(_LmLabelPersist);
1055
                }
1056
                else
1057
                    RetryEndBreakModeling(endBreak, targetLMConnector);
1058

    
1059
                foreach (var item in connectorVertices)
1060
                    ReleaseCOMObjects(item.Key);
1061

    
1062
            }
1063
            else if (ownerObj != null && ownerObj.GetType() == typeof(Symbol))
1064
            {
1065
                Symbol ownerSymbol = ownerObj as Symbol;
1066
                LMSymbol _LMSymbol = dataSource.GetSymbol(ownerSymbol.SPPID.RepresentationId);
1067

    
1068
                targetLMConnector = null;
1069
                double distance = double.MaxValue;
1070

    
1071
                foreach (LMConnector connector in _LMSymbol.Avoid1Connectors)
1072
                {
1073
                    if (connector.get_ItemStatus() == "Active")
1074
                    {
1075
                        dynamic OID = connector.get_GraphicOID();
1076
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1077
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1078
                        int verticesCount = lineStringGeometry.VertexCount;
1079
                        double[] vertices = null;
1080
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1081
                        for (int i = 0; i < verticesCount; i++)
1082
                        {
1083
                            double x = 0;
1084
                            double y = 0;
1085
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1086

    
1087
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1088
                            if (result < distance)
1089
                            {
1090
                                targetLMConnector = connector;
1091
                                distance = result;
1092
                            }
1093
                        }
1094
                    }
1095
                }
1096

    
1097
                foreach (LMConnector connector in _LMSymbol.Avoid2Connectors)
1098
                {
1099
                    if (connector.get_ItemStatus() == "Active")
1100
                    {
1101
                        dynamic OID = connector.get_GraphicOID();
1102
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1103
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1104
                        int verticesCount = lineStringGeometry.VertexCount;
1105
                        double[] vertices = null;
1106
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1107
                        for (int i = 0; i < verticesCount; i++)
1108
                        {
1109
                            double x = 0;
1110
                            double y = 0;
1111
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1112

    
1113
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1114
                            if (result < distance)
1115
                            {
1116
                                targetLMConnector = connector;
1117
                                distance = result;
1118
                            }
1119
                        }
1120
                    }
1121
                }
1122

    
1123
                if (targetLMConnector != null)
1124
                {
1125
                    LMLabelPersist _LmLabelPersist = null;
1126
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1127
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1128
                    if (_LmLabelPersist != null)
1129
                    {
1130
                        _LmLabelPersist.Commit();
1131
                        ReleaseCOMObjects(_LmLabelPersist);
1132
                    }
1133
                    else
1134
                        RetryEndBreakModeling(endBreak, targetLMConnector);
1135
                }
1136
                
1137
                ReleaseCOMObjects(_LMSymbol);
1138
            }
1139
        }
1140

    
1141
        private void RetryEndBreakModeling(EndBreak endBreak, LMConnector targetLMConnector)
1142
        {
1143
            bool isZeroLength = Convert.ToBoolean(targetLMConnector.get_IsZeroLength());
1144
            Array array = null;
1145
            LMLabelPersist _LMLabelPersist = null;
1146
            LMConnector _LMConnector = null;
1147
            dynamic OID = targetLMConnector.get_GraphicOID();
1148
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1149
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1150
            int verticesCount = lineStringGeometry.VertexCount;
1151
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1152
            _LMAItem _LMAItem = _placement.PIDCreateItem(@"\Piping\Routing\Process Lines\Primary Piping.sym");
1153

    
1154
            if (isZeroLength)
1155
            {
1156
                double[] vertices = null;
1157
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1158
                double x = 0;
1159
                double y = 0;
1160
                lineStringGeometry.GetVertex(1, ref x, ref y);
1161

    
1162
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, x, y);
1163
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, x, y);
1164

    
1165
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1166
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1167

    
1168
                array = new double[] { 0, x, y };
1169
                _LMLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: _LMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1170

    
1171
                AutoJoinPipeRun(_LMConnector.ModelItemID);
1172
            }
1173
            else
1174
            {
1175
                List<double[]> vertices = new List<double[]>();
1176
                for (int i = 1; i <= verticesCount; i++)
1177
                {
1178
                    double x = 0;
1179
                    double y = 0;
1180
                    lineStringGeometry.GetVertex(i, ref x, ref y);
1181
                    vertices.Add(new double[] { x, y });
1182
                }
1183

    
1184
                for (int i = 0; i < vertices.Count; i++)
1185
                {
1186
                    double[] points = vertices[i];
1187
                    if (i == 0)
1188
                    {
1189
                        if (targetLMConnector.ConnectItem1SymbolObject != null)
1190
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1191
                        else
1192
                            placeRunInputs.AddPoint(points[0], points[1]);
1193
                    }
1194
                    else if (i == vertices.Count - 1)
1195
                    {
1196
                        if (targetLMConnector.ConnectItem2SymbolObject != null)
1197
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1198
                        else
1199
                            placeRunInputs.AddPoint(points[0], points[1]);
1200
                    }
1201
                    else
1202
                        placeRunInputs.AddPoint(points[0], points[1]);
1203
                }
1204

    
1205
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, targetLMConnector.ModelItemID);
1206
                
1207
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1208
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1209

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

    
1213
                AutoJoinPipeRun(_LMConnector.ModelItemID);
1214
                foreach (var line in lines)
1215
                    line.SPPID.ModelItemId = _LMConnector.ModelItemID;
1216
            }
1217

    
1218

    
1219
            if (_LMLabelPersist != null)
1220
            {
1221
                _LMLabelPersist.Commit();
1222
                ReleaseCOMObjects(_LMLabelPersist);
1223
            }
1224
            else
1225
            {
1226
                
1227
            }
1228

    
1229
            ReleaseCOMObjects(_LMConnector);
1230
            ReleaseCOMObjects(placeRunInputs);
1231
            ReleaseCOMObjects(_LMAItem);
1232
        }
1233

    
1234
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
1235
        {
1236
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
1237
            _LMAItem item1 = modelItem1.AsLMAItem();
1238
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
1239
            _LMAItem item2 = modelItem2.AsLMAItem();
1240
            
1241
            // item2가 item1으로 조인
1242
            try
1243
            {
1244
                _placement.PIDJoinRuns(ref item1, ref item2);
1245
                item1.Commit();
1246
                item2.Commit();
1247

    
1248
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
1249
                foreach (var line in lines)
1250
                    line.SPPID.ModelItemId = toModelItemId;
1251
            }
1252
            catch (Exception ex)
1253
            {
1254
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1255
            }
1256
            finally
1257
            {
1258
                ReleaseCOMObjects(modelItem1);
1259
                ReleaseCOMObjects(item1);
1260
                ReleaseCOMObjects(modelItem2);
1261
                ReleaseCOMObjects(item2);
1262
            }
1263
        }
1264

    
1265
        private void AutoJoinPipeRun(string modelItemId)
1266
        {
1267
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
1268
            _LMAItem item = modelItem.AsLMAItem();
1269
            try
1270
            {
1271
                string modelitemID = item.Id;
1272
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
1273
                string afterModelItemID = item.Id;
1274
                
1275
                if (modelitemID != afterModelItemID)
1276
                {
1277
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
1278
                    foreach (var line in lines)
1279
                        line.SPPID.ModelItemId = afterModelItemID;
1280
                }
1281
                item.Commit();
1282
            }
1283
            catch (Exception ex)
1284
            {
1285
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1286
            }
1287
            finally
1288
            {
1289
                ReleaseCOMObjects(modelItem);
1290
                ReleaseCOMObjects(item);
1291
            }
1292
        }
1293

    
1294
        private void JoinRunLine(LineRun run)
1295
        {
1296
            string modelItemId = string.Empty;
1297
            foreach (var item in run.RUNITEMS)
1298
            {
1299
                if (item.GetType() == typeof(Line))
1300
                {
1301
                    Line line = item as Line;
1302
                    AutoJoinPipeRun(line.SPPID.ModelItemId);
1303
                    modelItemId = line.SPPID.ModelItemId;
1304
                }
1305
            }
1306
        }
1307

    
1308
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
1309
        {
1310
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
1311
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
1312

    
1313
            if (modelItem != null)
1314
            {
1315
                foreach (LMRepresentation rep in modelItem.Representations)
1316
                {
1317
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1318
                    {
1319
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1320
                        connectorVertices.Add(_LMConnector, new List<double[]>());
1321
                        dynamic OID = rep.get_GraphicOID();
1322
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1323
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1324
                        int verticesCount = lineStringGeometry.VertexCount;
1325
                        double[] vertices = null;
1326
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1327
                        for (int i = 0; i < verticesCount; i++)
1328
                        {
1329
                            double x = 0;
1330
                            double y = 0;
1331
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1332
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
1333
                        }
1334
                    }
1335
                }
1336

    
1337
                ReleaseCOMObjects(modelItem);
1338
            }
1339

    
1340
            return connectorVertices;
1341
        }
1342

    
1343
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
1344
        {
1345
            double length = double.MaxValue;
1346
            LMConnector targetConnector = null;
1347
            foreach (var item in connectorVertices)
1348
            {
1349
                List<double[]> points = item.Value;
1350
                for (int i = 0; i < points.Count - 1; i++)
1351
                {
1352
                    double[] point1 = points[i];
1353
                    double[] point2 = points[i + 1];
1354

    
1355
                    double maxLineX = Math.Max(point1[0], point2[0]);
1356
                    double minLineX = Math.Min(point1[0], point2[0]);
1357
                    double maxLineY = Math.Max(point1[1], point2[1]);
1358
                    double minLineY = Math.Min(point1[1], point2[1]);
1359

    
1360
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
1361

    
1362
                    // 두직선의 교차점
1363
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
1364
                    if (crossingPoint != null)
1365
                    {
1366
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
1367
                        if (length >= distance)
1368
                        {
1369
                            if (slope == SlopeType.Slope &&
1370
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
1371
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1372
                            {
1373
                                targetConnector = item.Key;
1374
                                length = distance;
1375
                            }
1376
                            else if (slope == SlopeType.HORIZONTAL &&
1377
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
1378
                            {
1379
                                targetConnector = item.Key;
1380
                                length = distance;
1381
                            }
1382
                            else if (slope == SlopeType.VERTICAL &&
1383
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1384
                            {
1385
                                targetConnector = item.Key;
1386
                                length = distance;
1387
                            }
1388
                        }
1389
                    }
1390
                }
1391

    
1392

    
1393
            }
1394

    
1395
            if (targetConnector == null)
1396
            {
1397
                foreach (var item in connectorVertices)
1398
                {
1399
                    List<double[]> points = item.Value;
1400
                    foreach (var point in points)
1401
                    {
1402
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, point[0], point[1]);
1403
                        if (length >= distance)
1404
                        {
1405
                            targetConnector = item.Key;
1406
                            length = distance;
1407
                        }
1408
                    }
1409
                }
1410

    
1411
            }
1412

    
1413
            return targetConnector;
1414
        }
1415

    
1416
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1417
        {
1418
            double length = double.MaxValue;
1419
            LMConnector targetConnector = null;
1420
            foreach (var item in connectorVertices)
1421
            {
1422
                List<double[]> points = item.Value;
1423

    
1424
                foreach (double[] point in points)
1425
                {
1426
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1427
                    if (length >= distance)
1428
                    {
1429
                        targetConnector = item.Key;
1430
                        length = distance;
1431
                    }
1432
                }
1433
            }
1434

    
1435
            return targetConnector;
1436
        }
1437

    
1438
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1439
        {
1440
            double length = double.MaxValue;
1441
            LMConnector targetConnector = null;
1442
            foreach (var item in connectorVertices)
1443
            {
1444
                List<double[]> points = item.Value;
1445
                for (int i = 0; i < points.Count - 1; i++)
1446
                {
1447
                    double[] point1 = points[i];
1448
                    double[] point2 = points[i + 1];
1449
                    double x1 = Math.Min(point1[0], point2[0]);
1450
                    double y1 = Math.Min(point1[1], point2[1]);
1451
                    double x2 = Math.Max(point1[0], point2[0]);
1452
                    double y2 = Math.Max(point1[1], point2[1]);
1453

    
1454
                    if ((x1 <= connX && x2 >= connX) ||
1455
                        (y1 <= connY && y2 >= connY))
1456
                    {
1457
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
1458
                        if (length >= distance)
1459
                        {
1460
                            targetConnector = item.Key;
1461
                            length = distance;
1462
                        }
1463

    
1464
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
1465
                        if (length >= distance)
1466
                        {
1467
                            targetConnector = item.Key;
1468
                            length = distance;
1469
                        }
1470
                    }
1471
                }
1472
            }
1473

    
1474
            // 못찾았을때.
1475
            length = double.MaxValue;
1476
            if (targetConnector == null)
1477
            {
1478
                foreach (var item in connectorVertices)
1479
                {
1480
                    List<double[]> points = item.Value;
1481

    
1482
                    foreach (double[] point in points)
1483
                    {
1484
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1485
                        if (length >= distance)
1486
                        {
1487
                            targetConnector = item.Key;
1488
                            length = distance;
1489
                        }
1490
                    }
1491
                }
1492
            }
1493

    
1494
            return targetConnector;
1495
        }
1496

    
1497
        private void LineNumberModeling(LineNumber lineNumber)
1498
        {
1499
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
1500
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
1501
            LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
1502
            if (connectedLMConnector != null)
1503
            {
1504
                double x = 0;
1505
                double y = 0;
1506
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
1507

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

    
1511
                foreach (var item in connectorVertices)
1512
                    ReleaseCOMObjects(item.Key);
1513
                if (_LmLabelPresist != null)
1514
                {
1515
                    _LmLabelPresist.Commit();
1516
                    lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
1517
                    ReleaseCOMObjects(_LmLabelPresist);
1518
                }
1519
                else
1520
                {
1521

    
1522
                }
1523
            }
1524
        }
1525

    
1526
        private void InputLineNumberAttribute(LineNumber lineNumber)
1527
        {
1528
            foreach (LineRun run in lineNumber.RUNS)
1529
            {
1530
                foreach (var item in run.RUNITEMS)
1531
                {
1532
                    if (item.GetType() == typeof(Symbol))
1533
                    {
1534
                        Symbol symbol = item as Symbol;
1535
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1536
                        LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1537

    
1538
                        if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
1539
                        {
1540
                            foreach (var attribute in lineNumber.ATTRIBUTES)
1541
                            {
1542
                                LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1543
                                if (mapping != null)
1544
                                {
1545
                                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1546
                                    if (_LMAAttribute != null)
1547
                                    {
1548
                                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1549
                                            _LMAAttribute.set_Value(attribute.VALUE);
1550
                                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
1551
                                            _LMAAttribute.set_Value(attribute.VALUE);
1552
                                    }
1553
                                }
1554
                            }
1555
                            _LMModelItem.Commit();
1556
                        }
1557
                        if (_LMModelItem != null)
1558
                            ReleaseCOMObjects(_LMModelItem);
1559
                        if (_LMSymbol != null)
1560
                            ReleaseCOMObjects(_LMSymbol);
1561
                    }
1562
                    else if (item.GetType() == typeof(Line))
1563
                    {
1564
                        Line line = item as Line;
1565
                        if (line != null)
1566
                        {
1567
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1568
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
1569
                            {
1570
                                foreach (var attribute in lineNumber.ATTRIBUTES)
1571
                                {
1572
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1573
                                    if (mapping != null)
1574
                                    {
1575
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1576
                                        if (_LMAAttribute != null)
1577
                                        {
1578
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1579
                                                _LMAAttribute.set_Value(attribute.VALUE);
1580
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
1581
                                                _LMAAttribute.set_Value(attribute.VALUE);
1582
                                            
1583
                                        }
1584
                                    }
1585
                                }
1586
                                _LMModelItem.Commit();
1587
                            }
1588
                            if (_LMModelItem != null)
1589
                                ReleaseCOMObjects(_LMModelItem);
1590
                        }
1591
                    }
1592
                }
1593
            }
1594
        }
1595

    
1596
        private void InputSymbolAttribute(Symbol symbol)
1597
        {
1598
            try
1599
            {
1600
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1601
                {
1602
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1603
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1604
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
1605

    
1606
                    foreach (var item in symbol.PROPERTIES)
1607
                    {
1608

    
1609

    
1610
                    }
1611

    
1612
                    foreach (var item in symbol.ATTRIBUTES)
1613
                    {
1614
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
1615
                        if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
1616
                        {
1617
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
1618
                            if (_Attribute != null)
1619
                            {
1620
                                // 임시
1621
                                if (item.ATTRIBUTETYPE == "String")
1622
                                {
1623
                                    if (!string.IsNullOrEmpty(item.VALUE))
1624
                                    {
1625
                                        if (!DBNull.Value.Equals(_Attribute.get_Value()) && !string.IsNullOrEmpty(_Attribute.get_Value()))
1626
                                        {
1627
                                            string value = _Attribute.get_Value() + "\n" + item.VALUE;
1628
                                            _Attribute.set_Value(value);
1629
                                        }
1630
                                        else
1631
                                        {
1632
                                            _Attribute.set_Value(item.VALUE);
1633
                                        }
1634
                                    }
1635
                                }
1636
                                else
1637
                                {
1638
                                    _Attribute.set_Value(item.VALUE);
1639
                                }
1640
                            }
1641
                        }
1642
                    }
1643

    
1644
                    foreach (var item in symbol.ASSOCIATIONS)
1645
                    {
1646

    
1647
                    }
1648

    
1649
                    ReleaseCOMObjects(_LMSymbol);
1650
                    ReleaseCOMObjects(_Attributes);
1651
                    ReleaseCOMObjects(_LMModelItem);
1652
                }
1653
            }
1654
            catch (Exception ex)
1655
            {
1656
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1657
            }
1658
        }
1659

    
1660
        private void TextModeling(Text text)
1661
        {
1662
            LMSymbol _LMSymbol = null;
1663
            try
1664
            {
1665
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
1666
                if (text.ASSOCIATION)
1667
                {
1668
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
1669
                    if (owner.GetType() == typeof(Symbol))
1670
                    {
1671
                        Symbol symbol = owner as Symbol;
1672
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1673
                        if (_LMSymbol != null)
1674
                        {
1675
                            Association association = symbol.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
1676
                            List<BaseModel.Attribute> attributes = symbol.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
1677
                            AttributeMapping mapping = null;
1678
                            foreach (var attribute in attributes)
1679
                            {
1680
                                if (string.IsNullOrEmpty(attribute.VALUE) || attribute.VALUE == "None")
1681
                                    continue;
1682

    
1683
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
1684
                                if (mapping != null)
1685
                                    break;  
1686
                            }
1687

    
1688
                            if (mapping != null)
1689
                            {
1690
                                double x = 0;
1691
                                double y = 0;
1692

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

    
1696
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
1697
                                if (_LMLabelPersist!=null)
1698
                                {
1699
                                    _LMLabelPersist.Commit();
1700
                                    ReleaseCOMObjects(_LMLabelPersist);
1701
                                }
1702
                            }
1703
                        }
1704
                    }
1705
                    else if (owner.GetType() == typeof(Line))
1706
                    {
1707

    
1708
                    }
1709
                }
1710
                else
1711
                {
1712
                    LMItemNote _LMItemNote = null;
1713
                    LMAAttribute _LMAAttribute = null;
1714

    
1715
                    double x = 0;
1716
                    double y = 0;
1717

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

    
1720
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y);
1721
                    _LMSymbol.Commit();
1722
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1723
                    _LMItemNote.Commit();
1724
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1725
                    _LMAAttribute.set_Value(text.VALUE);
1726
                    _LMItemNote.Commit();
1727

    
1728
                    if (_LMAAttribute != null)
1729
                        ReleaseCOMObjects(_LMAAttribute);
1730
                    if (_LMItemNote != null)
1731
                        ReleaseCOMObjects(_LMItemNote);
1732
                }
1733
            }
1734
            catch (Exception ex)
1735
            {
1736

    
1737
            }
1738
            finally
1739
            {
1740
                if (_LMSymbol != null)
1741
                    ReleaseCOMObjects(_LMSymbol);
1742
            }
1743
        }
1744

    
1745
        private void NoteModeling(Note note)
1746
        {
1747
            LMSymbol _LMSymbol = null;
1748
            LMItemNote _LMItemNote = null;
1749
            LMAAttribute _LMAAttribute = null;
1750

    
1751
            try
1752
            {
1753
                double x = 0;
1754
                double y = 0;
1755

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

    
1758
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
1759
                _LMSymbol.Commit();
1760
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1761
                _LMItemNote.Commit();
1762
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1763
                _LMAAttribute.set_Value(note.VALUE);
1764
                _LMItemNote.Commit();
1765
            }
1766
            catch (Exception ex)
1767
            {
1768

    
1769
            }
1770
            finally
1771
            {
1772
                if (_LMAAttribute != null)
1773
                    ReleaseCOMObjects(_LMAAttribute);
1774
                if (_LMItemNote != null)
1775
                    ReleaseCOMObjects(_LMItemNote);
1776
                if (_LMSymbol != null)
1777
                    ReleaseCOMObjects(_LMSymbol);
1778
            }
1779
            
1780
        }
1781

    
1782
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
1783
        {
1784
            if (location == Location.None)
1785
            {
1786
                x = originX;
1787
                y = originY;
1788
            }
1789
            else
1790
            {
1791
                if (location.HasFlag(Location.Center))
1792
                {
1793
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
1794
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
1795
                }
1796

    
1797
                if (location.HasFlag(Location.Left))
1798
                    x = SPPIDLabelLocation.X1;
1799
                else if (location.HasFlag(Location.Right))
1800
                    x = SPPIDLabelLocation.X2;
1801

    
1802
                if (location.HasFlag(Location.Down))
1803
                    y = SPPIDLabelLocation.Y1;
1804
                else if (location.HasFlag(Location.Up))
1805
                    y = SPPIDLabelLocation.Y2;
1806
            }
1807
        }
1808

    
1809
        public void ReleaseCOMObjects(params object[] objVars)
1810
        {
1811
            int intNewRefCount = 0;
1812
            foreach (object obj in objVars)
1813
            {
1814
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
1815
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
1816
            }
1817
        }
1818
    }
1819
}
클립보드 이미지 추가 (최대 크기: 500 MB)