프로젝트

일반

사용자정보

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

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 229cfccb

이력 | 보기 | 이력해설 | 다운로드 (79 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
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
80

    
81
                    // Equipment Modeling
82
                    foreach (Equipment equipment in document.Equipments)
83
                        EquipmentModeling(equipment);
84

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

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

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

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

    
105
                    // Branch Line Modeling
106
                    foreach (var item in BranchLines)
107
                        BranchLineModeling(item);
108

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

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

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

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

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

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

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

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

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

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

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

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

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

    
225
                                                if (result)
226
                                                    break;
227
                                            }
228

    
229
                                            if (result)
230
                                                break;
231
                                        }
232
                                    }
233

    
234

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

    
263
                ReleaseCOMObjects(dataSource);
264
                ReleaseCOMObjects(_placement);
265

    
266
                SplashScreenManager.CloseForm(false);
267
            }
268
        }
269

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

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

    
310
            if (lines.Count > 0)
311
                LineModeling(lines);
312
        }
313

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

    
322
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
323
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
324
            }
325

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

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

    
366

    
367
        }
368

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

    
392
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
393
        {
394
            // 임시
395
            if (symbol.SPPID.MAPPINGNAME.Contains("Labels - "))
396
                return;
397

    
398
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
399
                return;
400

    
401
            LMSymbol _LMSymbol = null;
402

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

    
415
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
416
            {
417
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
418

    
419
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
420
                double x1 = 0;
421
                double x2 = 0;
422
                double y1 = 0;
423
                double y2 = 0;
424
                symbol2d.Range(out x1, out y1, out x2, out y2);
425

    
426
                if (y2 < y)
427
                    y = y2;
428
                else if (y1 > y)
429
                    y = y1;
430

    
431
                if (x2 < x)
432
                    x = x2;
433
                else if (x1 > x)
434
                    x = x1;
435

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

    
456

    
457
            if (_LMSymbol != null)
458
            {
459
                _LMSymbol.Commit();
460
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
461

    
462
                foreach (var item in symbol.ChildSymbols)
463
                    CreateChildSymbol(item, _LMSymbol);
464
            }
465

    
466
            ReleaseCOMObjects(_LMSymbol);
467
        }
468

    
469
        private void EquipmentModeling(Equipment equipment)
470
        {
471
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
472
                return;
473

    
474
            LMSymbol _LMSymbol = null;
475
            LMSymbol targetItem = null;
476
            string mappingPath = equipment.SPPID.MAPPINGNAME;
477
            double x = equipment.SPPID.ORIGINAL_X;
478
            double y = equipment.SPPID.ORIGINAL_Y;
479
            int mirror = 0;
480
            double angle = equipment.ANGLE;
481

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

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

    
518
            if (_LMSymbol != null)
519
            {
520
                _LMSymbol.Commit();
521
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
522
                ReleaseCOMObjects(_LMSymbol);
523
            }
524

    
525
            if (targetItem != null)
526
            {
527
                ReleaseCOMObjects(targetItem);
528
            }
529
            
530
            //if (equipment.CONNECTORS)
531
            //{
532

    
533
            //}
534

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

    
558

    
559
            //if (_LMSymbol != null)
560
            //{
561
            //    _LMSymbol.Commit();
562
            //    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
563

    
564
            //    foreach (var item in symbol.ChildSymbols)
565
            //        CreateChildSymbol(item, _LMSymbol);
566
            //}
567

    
568
            ReleaseCOMObjects(_LMSymbol);
569
        }
570

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

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

    
589
            ReleaseCOMObjects(_LMSymbol);
590
        }
591

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

    
610
                            return false;
611

    
612
                        }
613
                    }
614
                }
615
            }
616

    
617
            return false;
618
        }
619

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

    
631
            Line startBranchLine = null;
632
            Line endBranchLine = null;
633

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

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

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

    
671
                    if (i != 0)
672
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
673

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

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

    
703
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
704

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

    
716

    
717
            if (_LMSymbol1 != null)
718
                ReleaseCOMObjects(_LMSymbol1);
719
            if (_LMSymbol2 != null)
720
                ReleaseCOMObjects(_LMSymbol2);
721
            if (targetConnector1 != null)
722
                ReleaseCOMObjects(targetConnector1);
723
            if (targetConnector2 != null)
724
                ReleaseCOMObjects(targetConnector2);
725
            foreach (var item in connectorVertices1)
726
                ReleaseCOMObjects(item.Key);
727
            foreach (var item in connectorVertices2)
728
                ReleaseCOMObjects(item.Key);
729

    
730
            ReleaseCOMObjects(_lMConnector);
731
            ReleaseCOMObjects(placeRunInputs);
732
            ReleaseCOMObjects(_LMAItem);
733
        }
734

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

    
754
                            if (child != null)
755
                                break;
756
                        }
757

    
758
                        if (child != null)
759
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
760
                    }
761

    
762
                    break;  
763
                }
764
            }
765

    
766
            return _LMSymbol;
767
        }
768

    
769
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
770
        {
771
            foreach (var childSymbol in item.ChildSymbols)
772
            {
773
                if (childSymbol.Connectors.Contains(connector))
774
                    return childSymbol;
775
                else
776
                    return GetChildSymbolByConnector(childSymbol, connector);
777
            }
778

    
779
            return null;
780
        }
781

    
782
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
783
        {
784
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
785
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
786

    
787
            LMConnector _StartConnector = null;
788
            LMConnector _EndConnector = null;
789
            double lengthStart = double.MaxValue;
790
            double lengthEnd = double.MaxValue;
791
            List<double[]> startPoints = new List<double[]>();
792
            List<double[]> endPoints = new List<double[]>();
793

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

    
829
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
830
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
831

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

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

    
852
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
853
                if (_LMConnector != null)
854
                {
855
                    _LMConnector.Commit();
856
                    foreach (var item in lines)
857
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
858
                }
859

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

    
876
                // Branch 끝 Connector
877
                if (_EndConnector != null)
878
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
879
            }
880
            #endregion
881

    
882
            if (_StartConnector != null)
883
                ReleaseCOMObjects(_StartConnector);
884
            if (_EndConnector != null)
885
                ReleaseCOMObjects(_EndConnector);
886
            foreach (var item in connectorVertices)
887
                ReleaseCOMObjects(item.Key);
888
        }
889

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

    
900
            // 같은 Line Run의 Connector 찾기
901
            foreach (var item in connectorVertices)
902
            {
903
                if (item.Key == _Connector)
904
                    continue;
905

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

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

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

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

    
1005
            if (_LMConnector != null)
1006
            {
1007
                if (_SameRunTargetConnector != null)
1008
                {
1009
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
1010
                }
1011
                else
1012
                {
1013
                    foreach (var item in lines)
1014
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
1015
                }
1016

    
1017
                _LMConnector.Commit();
1018
                ReleaseCOMObjects(_LMConnector);
1019
            }
1020

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

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

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

    
1055
                if (_LmLabelPersist != null)
1056
                {
1057
                    _LmLabelPersist.Commit();
1058
                    ReleaseCOMObjects(_LmLabelPersist);
1059
                }
1060
                else
1061
                    RetryEndBreakModeling(endBreak, targetLMConnector);
1062

    
1063
                foreach (var item in connectorVertices)
1064
                    ReleaseCOMObjects(item.Key);
1065

    
1066
            }
1067
            else if (ownerObj != null && ownerObj.GetType() == typeof(Symbol))
1068
            {
1069
                Symbol ownerSymbol = ownerObj as Symbol;
1070
                LMSymbol _LMSymbol = dataSource.GetSymbol(ownerSymbol.SPPID.RepresentationId);
1071

    
1072
                targetLMConnector = null;
1073
                double distance = double.MaxValue;
1074

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

    
1091
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1092
                            if (result < distance)
1093
                            {
1094
                                targetLMConnector = connector;
1095
                                distance = result;
1096
                            }
1097
                        }
1098
                    }
1099
                }
1100

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

    
1117
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1118
                            if (result < distance)
1119
                            {
1120
                                targetLMConnector = connector;
1121
                                distance = result;
1122
                            }
1123
                        }
1124
                    }
1125
                }
1126

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

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

    
1158
            if (isZeroLength)
1159
            {
1160
                double[] vertices = null;
1161
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1162
                double x = 0;
1163
                double y = 0;
1164
                lineStringGeometry.GetVertex(1, ref x, ref y);
1165

    
1166
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, x, y);
1167
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, x, y);
1168

    
1169
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1170
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1171

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

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

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

    
1209
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, targetLMConnector.ModelItemID);
1210
                
1211
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1212
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1213

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

    
1217
                AutoJoinPipeRun(_LMConnector.ModelItemID);
1218
                foreach (var line in lines)
1219
                    line.SPPID.ModelItemId = _LMConnector.ModelItemID;
1220
            }
1221

    
1222

    
1223
            if (_LMLabelPersist != null)
1224
            {
1225
                _LMLabelPersist.Commit();
1226
                ReleaseCOMObjects(_LMLabelPersist);
1227
            }
1228
            else
1229
            {
1230
                
1231
            }
1232

    
1233
            ReleaseCOMObjects(_LMConnector);
1234
            ReleaseCOMObjects(placeRunInputs);
1235
            ReleaseCOMObjects(_LMAItem);
1236
        }
1237

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

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

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

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

    
1312
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
1313
        {
1314
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
1315
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
1316

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

    
1341
                ReleaseCOMObjects(modelItem);
1342
            }
1343

    
1344
            return connectorVertices;
1345
        }
1346

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

    
1359
                    double maxLineX = Math.Max(point1[0], point2[0]);
1360
                    double minLineX = Math.Min(point1[0], point2[0]);
1361
                    double maxLineY = Math.Max(point1[1], point2[1]);
1362
                    double minLineY = Math.Min(point1[1], point2[1]);
1363

    
1364
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
1365

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

    
1396

    
1397
            }
1398

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

    
1415
            }
1416

    
1417
            return targetConnector;
1418
        }
1419

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

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

    
1439
            return targetConnector;
1440
        }
1441

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

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

    
1468
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
1469
                        if (length >= distance)
1470
                        {
1471
                            targetConnector = item.Key;
1472
                            length = distance;
1473
                        }
1474
                    }
1475
                }
1476
            }
1477

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

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

    
1498
            return targetConnector;
1499
        }
1500

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

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

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

    
1526
                }
1527
            }
1528
        }
1529

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

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

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

    
1610
                    foreach (var item in symbol.PROPERTIES)
1611
                    {
1612

    
1613

    
1614
                    }
1615

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

    
1648
                    foreach (var item in symbol.ASSOCIATIONS)
1649
                    {
1650

    
1651
                    }
1652

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

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

    
1687
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
1688
                                if (mapping != null)
1689
                                    break;  
1690
                            }
1691

    
1692
                            if (mapping != null)
1693
                            {
1694
                                double x = 0;
1695
                                double y = 0;
1696

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

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

    
1712
                    }
1713
                }
1714
                else
1715
                {
1716
                    LMItemNote _LMItemNote = null;
1717
                    LMAAttribute _LMAAttribute = null;
1718

    
1719
                    double x = 0;
1720
                    double y = 0;
1721

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

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

    
1732
                    if (_LMAAttribute != null)
1733
                        ReleaseCOMObjects(_LMAAttribute);
1734
                    if (_LMItemNote != null)
1735
                        ReleaseCOMObjects(_LMItemNote);
1736
                }
1737
            }
1738
            catch (Exception ex)
1739
            {
1740

    
1741
            }
1742
            finally
1743
            {
1744
                if (_LMSymbol != null)
1745
                    ReleaseCOMObjects(_LMSymbol);
1746
            }
1747
        }
1748

    
1749
        private void NoteModeling(Note note)
1750
        {
1751
            LMSymbol _LMSymbol = null;
1752
            LMItemNote _LMItemNote = null;
1753
            LMAAttribute _LMAAttribute = null;
1754

    
1755
            try
1756
            {
1757
                double x = 0;
1758
                double y = 0;
1759

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

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

    
1773
            }
1774
            finally
1775
            {
1776
                if (_LMAAttribute != null)
1777
                    ReleaseCOMObjects(_LMAAttribute);
1778
                if (_LMItemNote != null)
1779
                    ReleaseCOMObjects(_LMItemNote);
1780
                if (_LMSymbol != null)
1781
                    ReleaseCOMObjects(_LMSymbol);
1782
            }
1783
            
1784
        }
1785

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

    
1801
                if (location.HasFlag(Location.Left))
1802
                    x = SPPIDLabelLocation.X1;
1803
                else if (location.HasFlag(Location.Right))
1804
                    x = SPPIDLabelLocation.X2;
1805

    
1806
                if (location.HasFlag(Location.Down))
1807
                    y = SPPIDLabelLocation.Y1;
1808
                else if (location.HasFlag(Location.Up))
1809
                    y = SPPIDLabelLocation.Y2;
1810
            }
1811
        }
1812

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