프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ d19ae675

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

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

    
44
        public void Test()
45
        {
46
            _placement = new Placement();
47
            dataSource = _placement.PIDDataSource;
48

    
49
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveSelectSet[0] as Ingr.RAD2D.Symbol2d;
50
            if (symbol2d != null)
51
            {
52
                string modelID = symbol2d.AttributeSets[0][5].GetValue().ToString();
53

    
54
                LMModelItem modelItem = dataSource.GetModelItem(modelID);
55
                foreach (LMRepresentation rep in modelItem.Representations)
56
                {
57
                    if (rep.Attributes["RepresentationType"].get_Value() == "Symbol")
58
                    {
59
                        LMSymbol symbol = dataSource.GetSymbol(rep.Id);
60

    
61
                        int rotateIndex = symbol.get_IsRotatableIndex();
62
                        int mirrorIndex = symbol.get_IsMirroredIndex();
63
                        
64
                    }
65
                }
66
                    
67
                
68
                //symbol2d.AttributeSets[]
69
                //dataSource.GetSymbol()
70
            }
71
        }
72

    
73
        public void Run()
74
        {
75
            CloseOPCForm.Run();
76
            _placement = new Placement();
77
            dataSource = _placement.PIDDataSource;
78

    
79
            dynamic newDrawing = application.Drawings.Add(document.Unit, document.Template, document.DrawingNumber, document.DrawingName);
80
            application.ActiveWindow.Fit();
81
            Thread.Sleep(100);
82
            application.ActiveWindow.Zoom = 60;
83
            Thread.Sleep(100);
84

    
85
            try
86
            {
87
                // Equipment Modeling
88
                foreach (Equipment equipment in document.Equipments)
89
                    EquipmentModeling(equipment);
90
                
91
                // LineRun Symbol Modeling
92
                foreach (LineNumber lineNumber in document.LINENUMBERS)
93
                    foreach (LineRun run in lineNumber.RUNS)
94
                        SymbolModelingByRun(run);
95
                
96
                // TrimLineRun Symbol Modeling
97
                foreach (TrimLine trimLine in document.TRIMLINES)
98
                    foreach (LineRun run in trimLine.RUNS)
99
                        SymbolModelingByRun(run);
100
                
101
                // LineRun Line Modeling
102
                foreach (LineNumber lineNumber in document.LINENUMBERS)
103
                    foreach (LineRun run in lineNumber.RUNS)
104
                        LineModelingByRun(run);
105
                
106
                // TrimLineRun Line Modeling
107
                foreach (TrimLine trimLine in document.TRIMLINES)
108
                    foreach (LineRun run in trimLine.RUNS)
109
                        LineModelingByRun(run);
110
                
111
                // Branch Line Modeling
112
                foreach (var item in BranchLines)
113
                    BranchLineModeling(item);
114
                
115
                // EndBreak Modeling
116
                foreach (var item in document.EndBreaks)
117
                    EndBreakModeling(item);
118
                
119
                // LineNumber Modeling
120
                foreach (var item in document.LINENUMBERS)
121
                    LineNumberModeling(item);
122

    
123
                // Note Modeling
124
                foreach (var item in document.NOTES)
125
                    NoteModeling(item);
126

    
127
                // Text Modeling
128
                foreach (var item in document.TEXTINFOS)
129
                    TextModeling(item);
130

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

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

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

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

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

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

    
193
                                
194

    
195
                                _LMModelItem.Commit();
196
                                _LMLabelPresist.Commit();
197
                                ReleaseCOMObjects(_TargetItem);
198
                                ReleaseCOMObjects(_LMLabelPresist);
199
                                ReleaseCOMObjects(_LMModelItem);
200
                                ReleaseCOMObjects(_Attributes);
201
                            }
202
                        }
203
                    }
204
                }
205
                #endregion
206

    
207
                // LineRun Line Join
208
                foreach (LineNumber lineNumber in document.LINENUMBERS)
209
                    foreach (LineRun run in lineNumber.RUNS)
210
                        JoinRunLine(run);
211

    
212
                // TrimLineRun Line Join
213
                foreach (TrimLine trimLine in document.TRIMLINES)
214
                    foreach (LineRun run in trimLine.RUNS)
215
                        JoinRunLine(run);
216
            }
217
            catch (Exception ex)
218
            {
219
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
220
            }
221
            finally
222
            {
223
                ReleaseCOMObjects(dataSource);
224
                ReleaseCOMObjects(_placement);
225
            }
226

    
227
            CloseOPCForm.Stop();
228
            //System.Windows.Forms.MessageBox.Show("end");
229
        }
230

    
231
        private void LineModelingByRun(LineRun run)
232
        {
233
            Line prevLine = null;
234
            List<Line> lines = new List<Line>();
235
            foreach (var item in run.RUNITEMS)
236
            {
237
                // Line일 경우
238
                if (item.GetType() == typeof(Line))
239
                {
240
                    Line line = item as Line;
241
                    if (prevLine == null)
242
                        lines.Add(line);
243
                    else if (prevLine != null)
244
                    {
245
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
246
                            lines.Add(line);
247
                        else
248
                        {
249
                            if (lines.Count > 0)
250
                            {
251
                                LineModeling(lines);
252
                                lines.Clear();
253
                            }
254
                            lines.Add(line);
255
                        }
256
                    }
257

    
258
                    prevLine = line;
259
                }
260
                // Symbol 일 경우
261
                else if (item.GetType() == typeof(Symbol))
262
                {
263
                    if (lines.Count > 0)
264
                    {
265
                        LineModeling(lines);
266
                        lines.Clear();
267
                    }
268
                }
269
            }
270

    
271
            if (lines.Count > 0)
272
                LineModeling(lines);
273
        }
274

    
275
        private void SymbolModelingByRun(LineRun run)
276
        {
277
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
278
            if (run.RUNITEMS.Count > 0)
279
            {
280
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
281
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
282

    
283
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
284
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
285
            }
286

    
287
            Symbol prevSymbol = null;
288
            Symbol targetSymbol = null;
289
            foreach (var item in run.RUNITEMS)
290
            {
291
                if (item.GetType() == typeof(Symbol))
292
                {
293
                    Symbol symbol = item as Symbol;
294
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
295
                    prevSymbol = symbol;
296
                    targetSymbol = symbol;
297
                }
298
                else
299
                {
300
                    targetSymbol = null;
301
                }
302
            }
303
        }
304

    
305
        private void SymbolModelingByRunStart(Symbol symbol, LineRun run)
306
        {
307
            foreach (var connector in symbol.CONNECTORS)
308
            {
309
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
310
                if (targetItem != null &&
311
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
312
                    !IsSameLineNumber(symbol, targetItem))
313
                {
314
                    SymbolModeling(symbol, targetItem as Symbol, null);
315
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
316
                    {
317
                        object item = run.RUNITEMS[i];
318
                        if (item.GetType() == typeof(Symbol))
319
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol, null);
320
                        else
321
                            break;
322
                    }
323
                    break;
324
                }
325
            }
326

    
327

    
328
        }
329

    
330
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
331
        {
332
            foreach (var connector in symbol.CONNECTORS)
333
            {
334
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
335
                if (targetItem != null &&
336
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
337
                    !IsSameLineNumber(symbol, targetItem))
338
                {
339
                    SymbolModeling(symbol, targetItem as Symbol, null);
340
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
341
                    {
342
                        object item = run.RUNITEMS[i];
343
                        if (item.GetType() == typeof(Symbol))
344
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol, null);
345
                        else
346
                            break;
347
                    }
348
                    break;
349
                }
350
            }
351
        }
352

    
353
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
354
        {
355
            // 임시
356
            if (symbol.SPPID.MAPPINGNAME.Contains("Labels - "))
357
                return;
358

    
359
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
360
                return;
361

    
362
            LMSymbol _LMSymbol = null;
363

    
364
            string mappingPath = symbol.SPPID.MAPPINGNAME;
365
            double x = symbol.SPPID.ORIGINAL_X;
366
            double y = symbol.SPPID.ORIGINAL_Y;
367
            int mirror = 0;
368
            double angle = symbol.ANGLE;
369
            // 임시
370
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
371
            {
372
                mirror = 1;
373
            }
374
                
375

    
376
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
377
            {
378
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
379

    
380
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
381
                double x1 = 0;
382
                double x2 = 0;
383
                double y1 = 0;
384
                double y2 = 0;
385
                symbol2d.Range(out x1, out y1, out x2, out y2);
386

    
387
                if (y2 < y)
388
                    y = y2;
389
                else if (y1 > y)
390
                    y = y1;
391

    
392
                if (x2 < x)
393
                    x = x2;
394
                else if (x1 > x)
395
                    x = x1;
396

    
397
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
398
                ReleaseCOMObjects(_TargetItem);
399
            }
400
            else if (prevSymbol != null)
401
            {
402
                LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
403
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
404
                double prevX = _PrevSymbol.get_XCoordinate();
405
                double prevY = _PrevSymbol.get_YCoordinate();
406
                if (slopeType == SlopeType.HORIZONTAL)
407
                    y = prevY;
408
                else if (slopeType == SlopeType.VERTICAL)
409
                    x = prevX;
410
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
411
            }
412
            else
413
            {
414
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
415
            }
416

    
417

    
418
            if (_LMSymbol != null)
419
            {
420
                _LMSymbol.Commit();
421
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
422

    
423
                foreach (var item in symbol.ChildSymbols)
424
                    CreateChildSymbol(item, _LMSymbol);
425
            }
426

    
427
            ReleaseCOMObjects(_LMSymbol);
428
        }
429

    
430
        private void EquipmentModeling(Equipment equipment)
431
        {
432
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
433
                return;
434

    
435
            LMSymbol _LMSymbol = null;
436
            LMSymbol targetItem = null;
437
            string mappingPath = equipment.SPPID.MAPPINGNAME;
438
            double x = equipment.SPPID.ORIGINAL_X;
439
            double y = equipment.SPPID.ORIGINAL_Y;
440
            int mirror = 0;
441
            double angle = equipment.ANGLE;
442

    
443
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
444
            if (connector != null)
445
            {
446
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
447
                if (connEquipment != null)
448
                {
449
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
450
                        EquipmentModeling(connEquipment);
451

    
452
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
453
                    {
454
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
455
                        if (targetItem != null)
456
                        {
457
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
458
                        }
459
                        else
460
                        {
461
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
462
                        }
463
                    }
464
                    else
465
                    {
466
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
467
                    }
468
                }
469
                else
470
                {
471
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
472
                }
473
            }
474
            else
475
            {
476
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
477
            }
478

    
479
            if (_LMSymbol != null)
480
            {
481
                _LMSymbol.Commit();
482
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
483
                ReleaseCOMObjects(_LMSymbol);
484
            }
485

    
486
            if (targetItem != null)
487
            {
488
                ReleaseCOMObjects(targetItem);
489
            }
490
            
491
            //if (equipment.CONNECTORS)
492
            //{
493

    
494
            //}
495

    
496
            //if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
497
            //{
498
            //    LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
499
            //    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
500
            //    ReleaseCOMObjects(_TargetItem);
501
            //}
502
            //else if (prevSymbol != null)
503
            //{
504
            //    LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
505
            //    SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
506
            //    double prevX = _PrevSymbol.get_XCoordinate();
507
            //    double prevY = _PrevSymbol.get_YCoordinate();
508
            //    if (slopeType == SlopeType.HORIZONTAL)
509
            //        y = prevY;
510
            //    else if (slopeType == SlopeType.VERTICAL)
511
            //        x = prevX;
512
            //    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
513
            //}
514
            //else
515
            //{
516
            //    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
517
            //}
518

    
519

    
520
            //if (_LMSymbol != null)
521
            //{
522
            //    _LMSymbol.Commit();
523
            //    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
524

    
525
            //    foreach (var item in symbol.ChildSymbols)
526
            //        CreateChildSymbol(item, _LMSymbol);
527
            //}
528

    
529
            ReleaseCOMObjects(_LMSymbol);
530
        }
531

    
532
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
533
        {
534
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
535
            double x1 = 0;
536
            double x2 = 0;
537
            double y1 = 0;
538
            double y2 = 0;
539
            symbol2d.Range(out x1, out y1, out x2, out y2);
540

    
541
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
542
            if (_LMSymbol != null)
543
            {
544
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
545
                foreach (var item in childSymbol.ChildSymbols)
546
                    CreateChildSymbol(item, _LMSymbol);
547
            }
548
            
549

    
550
            ReleaseCOMObjects(_LMSymbol);
551
        }
552

    
553
        private bool IsSameLineNumber(object item, object targetItem)
554
        {
555
            foreach (var lineNumber in document.LINENUMBERS)
556
            {
557
                foreach (var run in lineNumber.RUNS)
558
                {
559
                    foreach (var runItem in run.RUNITEMS)
560
                    {
561
                        if (runItem == item)
562
                        {
563
                            foreach (var findItem in run.RUNITEMS)
564
                            {
565
                                if (findItem == targetItem)
566
                                {
567
                                    return true;
568
                                }
569
                            }
570

    
571
                            return false;
572

    
573
                        }
574
                    }
575
                }
576
            }
577

    
578
            return false;
579
        }
580

    
581
        private void LineModeling(List<Line> lines)
582
        {
583
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
584
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
585
            LMSymbol _LMSymbol1 = null;
586
            LMSymbol _LMSymbol2 = null;
587
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
588
            LMConnector targetConnector1 = null;
589
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
590
            LMConnector targetConnector2 = null;
591

    
592
            Line startBranchLine = null;
593
            Line endBranchLine = null;
594

    
595
            for (int i = 0; i < lines.Count; i++)
596
            {
597
                Line line = lines[i];
598
                if (i == 0 || i + 1 != lines.Count)
599
                {
600
                    // 시작점에 연결된 Symbol 찾기
601
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
602
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
603
                    {
604
                        _LMSymbol1 = GetTargetSymbol(connItem as Symbol, line);
605
                        if (_LMSymbol1 != null)
606
                            placeRunInputs.AddSymbolTarget(_LMSymbol1, line.SPPID.START_X, line.SPPID.START_Y);
607
                        else
608
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
609
                    }
610
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
611
                    {
612
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
613
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
614

    
615
                        if (targetConnector1 != null)
616
                            placeRunInputs.AddConnectorTarget(targetConnector1, line.SPPID.START_X, line.SPPID.START_Y);
617
                        else
618
                        {
619
                            startBranchLine = connItem as Line;
620
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
621
                        }
622
                    }
623
                    else
624
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
625
                }
626

    
627
                if (i + 1 == lines.Count)
628
                {
629
                    // 끝점에 연결된 Symbol 찾기
630
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
631

    
632
                    if (i != 0)
633
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
634

    
635
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
636
                    {
637
                        _LMSymbol2 = GetTargetSymbol(connItem as Symbol, line);
638
                        if (_LMSymbol2 != null)
639
                            placeRunInputs.AddSymbolTarget(_LMSymbol2, line.SPPID.END_X, line.SPPID.END_Y);
640
                        else
641
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
642
                            
643
                    }
644
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
645
                    {
646
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
647
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
648

    
649
                        if (targetConnector2 != null)
650
                            placeRunInputs.AddConnectorTarget(targetConnector2, line.SPPID.END_X, line.SPPID.END_Y);
651
                        else
652
                        {
653
                            endBranchLine = connItem as Line;
654
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
655
                        }
656
                    }
657
                    else
658
                    {
659
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
660
                    }
661
                }
662
            }
663

    
664
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
665

    
666
            if (_lMConnector != null)
667
            {
668
                foreach (var line in lines)
669
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
670
                _lMConnector.Commit();
671
                if (startBranchLine != null || endBranchLine != null)
672
                {
673
                    BranchLines.Add(new Tuple<string, Line, Line>(_lMConnector.ModelItemID, startBranchLine, endBranchLine));
674
                }
675
            }
676

    
677

    
678
            if (_LMSymbol1 != null)
679
                ReleaseCOMObjects(_LMSymbol1);
680
            if (_LMSymbol2 != null)
681
                ReleaseCOMObjects(_LMSymbol2);
682
            if (targetConnector1 != null)
683
                ReleaseCOMObjects(targetConnector1);
684
            if (targetConnector2 != null)
685
                ReleaseCOMObjects(targetConnector2);
686
            foreach (var item in connectorVertices1)
687
                ReleaseCOMObjects(item.Key);
688
            foreach (var item in connectorVertices2)
689
                ReleaseCOMObjects(item.Key);
690

    
691
            ReleaseCOMObjects(_lMConnector);
692
            ReleaseCOMObjects(placeRunInputs);
693
            ReleaseCOMObjects(_LMAItem);
694
        }
695

    
696
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
697
        {
698
            LMSymbol _LMSymbol = null;
699
            foreach (var connector in symbol.CONNECTORS)
700
            {
701
                if (connector.CONNECTEDITEM == line.UID)
702
                {
703
                    if (connector.Index == 0)
704
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
705
                    else
706
                    {
707
                        ChildSymbol child = null;
708
                        foreach (var childSymbol in symbol.ChildSymbols)
709
                        {
710
                            if (childSymbol.Connectors.Contains(connector))
711
                                child = childSymbol;
712
                            else
713
                                child = GetChildSymbolByConnector(childSymbol, connector);
714

    
715
                            if (child != null)
716
                                break;
717
                        }
718

    
719
                        if (child != null)
720
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
721
                    }
722

    
723
                    break;  
724
                }
725
            }
726

    
727
            return _LMSymbol;
728
        }
729

    
730
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
731
        {
732
            foreach (var childSymbol in item.ChildSymbols)
733
            {
734
                if (childSymbol.Connectors.Contains(connector))
735
                    return childSymbol;
736
                else
737
                    return GetChildSymbolByConnector(childSymbol, connector);
738
            }
739

    
740
            return null;
741
        }
742

    
743
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
744
        {
745
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
746
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
747

    
748
            LMConnector _StartConnector = null;
749
            LMConnector _EndConnector = null;
750
            double lengthStart = double.MaxValue;
751
            double lengthEnd = double.MaxValue;
752
            List<double[]> startPoints = new List<double[]>();
753
            List<double[]> endPoints = new List<double[]>();
754

    
755
            foreach (var item in connectorVertices)
756
            {
757
                foreach (var point in item.Value)
758
                {
759
                    // Start Point가 Branch
760
                    if (branch.Item2 != null)
761
                    {
762
                        Line targetLine = branch.Item2;
763
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
764
                        if (lengthStart > distance)
765
                        {
766
                            _StartConnector = item.Key;
767
                            lengthStart = distance;
768
                            startPoints = item.Value;
769
                        }
770
                    }
771
                    // End Point가 Branch
772
                    if (branch.Item3 != null)
773
                    {
774
                        Line targetLine = branch.Item3;
775
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
776
                        if (lengthEnd > distance)
777
                        {
778
                            _EndConnector = item.Key;
779
                            lengthEnd = distance;
780
                            endPoints = item.Value;
781
                        }
782
                    }
783
                }
784
            }
785
            #region Branch가 양쪽 전부일 때
786
            if (_StartConnector != null && _StartConnector == _EndConnector)
787
            {
788
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
789

    
790
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
791
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
792

    
793
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
794
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
795
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
796
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
797
                   startPoints[startPoints.Count - 1][0],
798
                   startPoints[startPoints.Count - 1][1],
799
                   startPoints[startPoints.Count - 2][0],
800
                   startPoints[startPoints.Count - 2][1]);
801

    
802
                for (int i = 0; i < startPoints.Count; i++)
803
                {
804
                    double[] point = startPoints[i];
805
                    if (i == 0)
806
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
807
                    else if (i == startPoints.Count - 1)
808
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
809
                    else
810
                        placeRunInputs.AddPoint(point[0], point[1]);
811
                }
812

    
813
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
814
                if (_LMConnector != null)
815
                {
816
                    _LMConnector.Commit();
817
                    foreach (var item in lines)
818
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
819
                }
820

    
821
                foreach (var item in startConnectorVertices)
822
                    ReleaseCOMObjects(item.Key);
823
                foreach (var item in endConnectorVertices)
824
                    ReleaseCOMObjects(item.Key);
825
                ReleaseCOMObjects(placeRunInputs);
826
                ReleaseCOMObjects(_LMAItem);
827
                ReleaseCOMObjects(_LMConnector);
828
            }
829
            #endregion
830
            #region 양쪽이 다른 Branch 
831
            else
832
            {
833
                // Branch 시작 Connector
834
                if (_StartConnector != null)
835
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
836

    
837
                // Branch 끝 Connector
838
                if (_EndConnector != null)
839
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
840
            }
841
            #endregion
842

    
843
            if (_StartConnector != null)
844
                ReleaseCOMObjects(_StartConnector);
845
            if (_EndConnector != null)
846
                ReleaseCOMObjects(_EndConnector);
847
            foreach (var item in connectorVertices)
848
                ReleaseCOMObjects(item.Key);
849
        }
850

    
851
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
852
        {
853
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
854
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
855
            LMConnector _SameRunTargetConnector = null;
856
            LMSymbol _SameRunTargetSymbol = null;
857
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
858
            LMConnector _BranchTargetConnector = null;
859
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
860

    
861
            // 같은 Line Run의 Connector 찾기
862
            foreach (var item in connectorVertices)
863
            {
864
                if (item.Key == _Connector)
865
                    continue;
866

    
867
                if (IsStart &&
868
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
869
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
870
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
871
                {
872
                    _SameRunTargetConnector = item.Key;
873
                    break;
874
                }
875
                else if (!IsStart &&
876
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
877
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
878
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
879
                {
880
                    _SameRunTargetConnector = item.Key;
881
                    break;
882
                }
883
            }
884

    
885
            // Branch 반대편이 Symbol
886
            if (_SameRunTargetConnector == null)
887
            {
888
                foreach (var line in lines)
889
                {
890
                    foreach (var connector in line.CONNECTORS)
891
                    {
892
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
893
                        if (symbol != null)
894
                        {
895
                            _SameRunTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
896
                            break;
897
                        }
898
                    }
899
                }
900
            }
901

    
902
            // 기존 Connector 제거
903
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
904
            
905
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
906
            if (IsStart)
907
            {
908
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
909
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
910
            }
911
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
912
            else
913
            {
914
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
915
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
916
                    points[points.Count - 1][0],
917
                    points[points.Count - 1][1],
918
                    points[points.Count - 2][0],
919
                    points[points.Count - 2][1]);
920
            }
921

    
922
            for (int i = 0; i < points.Count; i++)
923
            {
924
                double[] point = points[i];
925
                if (i == 0)
926
                {
927
                    if (IsStart)
928
                    {
929
                        placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
930
                    }
931
                    else
932
                    {
933
                        if (_SameRunTargetConnector != null)
934
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
935
                        else if (_SameRunTargetSymbol != null)
936
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
937
                        else
938
                            placeRunInputs.AddPoint(point[0], point[1]);
939
                    }
940
                }
941
                else if (i == points.Count - 1)
942
                {
943
                    if (IsStart)
944
                    {
945
                        if (_SameRunTargetConnector != null)
946
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
947
                        else if (_SameRunTargetSymbol != null)
948
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
949
                        else
950
                            placeRunInputs.AddPoint(point[0], point[1]);
951
                    }
952
                    else
953
                    {
954
                        if (_BranchTargetConnector != null)
955
                        {
956
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
957
                        }
958
                    }
959
                }
960
                else
961
                    placeRunInputs.AddPoint(point[0], point[1]);
962
            }
963
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
964
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
965

    
966
            if (_LMConnector != null)
967
            {
968
                if (_SameRunTargetConnector != null)
969
                {
970
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
971
                }
972
                else
973
                {
974
                    foreach (var item in lines)
975
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
976
                }
977

    
978
                _LMConnector.Commit();
979
                ReleaseCOMObjects(_LMConnector);
980
            }
981

    
982
            ReleaseCOMObjects(placeRunInputs);
983
            ReleaseCOMObjects(_LMAItem);
984
            if (_BranchTargetConnector != null)
985
                ReleaseCOMObjects(_BranchTargetConnector);
986
            if (_SameRunTargetConnector != null)
987
                ReleaseCOMObjects(_SameRunTargetConnector);
988
            if (_SameRunTargetSymbol != null)
989
                ReleaseCOMObjects(_SameRunTargetSymbol);
990
            foreach (var item in connectorVertices)
991
                ReleaseCOMObjects(item.Key);
992
            foreach (var item in branchConnectorVertices)
993
                ReleaseCOMObjects(item.Key);
994
        }
995

    
996
        private void EndBreakModeling(EndBreak endBreak)
997
        {
998
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
999
            LMConnector targetLMConnector = null;
1000
            if (ownerObj !=null && ownerObj.GetType() == typeof(Line))
1001
            {
1002
                Line ownerLine = ownerObj as Line;
1003
                LMLabelPersist _LmLabelPersist = null;
1004
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(ownerLine.SPPID.ModelItemId);
1005

    
1006
                targetLMConnector = FindTargetLMConnectorByPoint(connectorVertices, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1007
                
1008
                if (targetLMConnector != null)
1009
                {
1010
                    //double[] point = connectorVertices[targetLMConnector][connectorVertices[targetLMConnector].Count - 1];
1011
                    //Array array = new double[] { 0, point[0], point[1] };
1012
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1013
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1014
                }
1015

    
1016
                if (_LmLabelPersist != null)
1017
                {
1018
                    _LmLabelPersist.Commit();
1019
                    ReleaseCOMObjects(_LmLabelPersist);
1020
                }
1021
                else
1022
                    RetryEndBreakModeling(endBreak, targetLMConnector);
1023

    
1024
                foreach (var item in connectorVertices)
1025
                    ReleaseCOMObjects(item.Key);
1026

    
1027
            }
1028
            else if (ownerObj != null && ownerObj.GetType() == typeof(Symbol))
1029
            {
1030
                Symbol ownerSymbol = ownerObj as Symbol;
1031
                LMSymbol _LMSymbol = dataSource.GetSymbol(ownerSymbol.SPPID.RepresentationId);
1032

    
1033
                targetLMConnector = null;
1034
                double distance = double.MaxValue;
1035

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

    
1052
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1053
                            if (result < distance)
1054
                            {
1055
                                targetLMConnector = connector;
1056
                                distance = result;
1057
                            }
1058
                        }
1059
                    }
1060
                }
1061

    
1062
                foreach (LMConnector connector in _LMSymbol.Avoid2Connectors)
1063
                {
1064
                    if (connector.get_ItemStatus() == "Active")
1065
                    {
1066
                        dynamic OID = connector.get_GraphicOID();
1067
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1068
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1069
                        int verticesCount = lineStringGeometry.VertexCount;
1070
                        double[] vertices = null;
1071
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1072
                        for (int i = 0; i < verticesCount; i++)
1073
                        {
1074
                            double x = 0;
1075
                            double y = 0;
1076
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1077

    
1078
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1079
                            if (result < distance)
1080
                            {
1081
                                targetLMConnector = connector;
1082
                                distance = result;
1083
                            }
1084
                        }
1085
                    }
1086
                }
1087

    
1088
                if (targetLMConnector != null)
1089
                {
1090
                    LMLabelPersist _LmLabelPersist = null;
1091
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1092
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1093
                    if (_LmLabelPersist != null)
1094
                    {
1095
                        _LmLabelPersist.Commit();
1096
                        ReleaseCOMObjects(_LmLabelPersist);
1097
                    }
1098
                    else
1099
                        RetryEndBreakModeling(endBreak, targetLMConnector);
1100
                }
1101
                
1102
                ReleaseCOMObjects(_LMSymbol);
1103
            }
1104
        }
1105

    
1106
        private void RetryEndBreakModeling(EndBreak endBreak, LMConnector targetLMConnector)
1107
        {
1108
            bool isZeroLength = Convert.ToBoolean(targetLMConnector.get_IsZeroLength());
1109
            Array array = null;
1110
            LMLabelPersist _LMLabelPersist = null;
1111
            LMConnector _LMConnector = null;
1112
            dynamic OID = targetLMConnector.get_GraphicOID();
1113
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1114
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1115
            int verticesCount = lineStringGeometry.VertexCount;
1116
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1117
            _LMAItem _LMAItem = _placement.PIDCreateItem(@"\Piping\Routing\Process Lines\Primary Piping.sym");
1118

    
1119
            if (isZeroLength)
1120
            {
1121
                double[] vertices = null;
1122
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1123
                double x = 0;
1124
                double y = 0;
1125
                lineStringGeometry.GetVertex(1, ref x, ref y);
1126

    
1127
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, x, y);
1128
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, x, y);
1129

    
1130
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1131
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1132

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

    
1136
                AutoJoinPipeRun(_LMConnector.ModelItemID);
1137
            }
1138
            else
1139
            {
1140
                List<double[]> vertices = new List<double[]>();
1141
                for (int i = 1; i <= verticesCount; i++)
1142
                {
1143
                    double x = 0;
1144
                    double y = 0;
1145
                    lineStringGeometry.GetVertex(i, ref x, ref y);
1146
                    vertices.Add(new double[] { x, y });
1147
                }
1148

    
1149
                for (int i = 0; i < vertices.Count; i++)
1150
                {
1151
                    double[] points = vertices[i];
1152
                    if (i == 0)
1153
                    {
1154
                        if (targetLMConnector.ConnectItem1SymbolObject != null)
1155
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1156
                        else
1157
                            placeRunInputs.AddPoint(points[0], points[1]);
1158
                    }
1159
                    else if (i == vertices.Count - 1)
1160
                    {
1161
                        if (targetLMConnector.ConnectItem2SymbolObject != null)
1162
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1163
                        else
1164
                            placeRunInputs.AddPoint(points[0], points[1]);
1165
                    }
1166
                    else
1167
                        placeRunInputs.AddPoint(points[0], points[1]);
1168
                }
1169

    
1170
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, targetLMConnector.ModelItemID);
1171
                
1172
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1173
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1174

    
1175
                foreach (var line in lines)
1176
                    line.SPPID.ModelItemId = _LMConnector.ModelItemID;
1177

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

    
1182

    
1183
            if (_LMLabelPersist != null)
1184
            {
1185
                _LMLabelPersist.Commit();
1186
                ReleaseCOMObjects(_LMLabelPersist);
1187
            }
1188
            else
1189
            {
1190
                
1191
            }
1192

    
1193
            ReleaseCOMObjects(_LMConnector);
1194
            ReleaseCOMObjects(placeRunInputs);
1195
            ReleaseCOMObjects(_LMAItem);
1196
        }
1197

    
1198
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
1199
        {
1200
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
1201
            _LMAItem item1 = modelItem1.AsLMAItem();
1202
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
1203
            _LMAItem item2 = modelItem2.AsLMAItem();
1204
            
1205
            // item2가 item1으로 조인
1206
            try
1207
            {
1208
                _placement.PIDJoinRuns(ref item1, ref item2);
1209
                item1.Commit();
1210
                item2.Commit();
1211
            }
1212
            catch (Exception ex)
1213
            {
1214
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1215
            }
1216
            finally
1217
            {
1218
                ReleaseCOMObjects(modelItem1);
1219
                ReleaseCOMObjects(item1);
1220
                ReleaseCOMObjects(modelItem2);
1221
                ReleaseCOMObjects(item2);
1222
            }
1223
        }
1224

    
1225
        private void AutoJoinPipeRun(string modelItemId)
1226
        {
1227
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
1228
            _LMAItem item = modelItem.AsLMAItem();
1229
            try
1230
            {
1231
                string modelitemID = item.Id;
1232
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
1233
                string afterModelItemID = item.Id;
1234
                
1235
                if (modelitemID != afterModelItemID)
1236
                {
1237
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
1238
                    foreach (var line in lines)
1239
                        line.SPPID.ModelItemId = afterModelItemID;
1240
                }
1241
                item.Commit();
1242
            }
1243
            catch (Exception ex)
1244
            {
1245
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1246
            }
1247
            finally
1248
            {
1249
                ReleaseCOMObjects(modelItem);
1250
                ReleaseCOMObjects(item);
1251
            }
1252
        }
1253

    
1254
        private void JoinRunLine(LineRun run)
1255
        {
1256
            string modelItemId = string.Empty;
1257
            foreach (var item in run.RUNITEMS)
1258
            {
1259
                if (item.GetType() == typeof(Line))
1260
                {
1261
                    Line line = item as Line;
1262
                    if (modelItemId != line.SPPID.ModelItemId)
1263
                    {
1264
                        AutoJoinPipeRun(line.SPPID.ModelItemId);
1265
                        modelItemId = line.SPPID.ModelItemId;
1266
                    }
1267
                }
1268
            }
1269
        }
1270

    
1271
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
1272
        {
1273
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
1274
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
1275

    
1276
            if (modelItem != null)
1277
            {
1278
                foreach (LMRepresentation rep in modelItem.Representations)
1279
                {
1280
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1281
                    {
1282
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1283
                        connectorVertices.Add(_LMConnector, new List<double[]>());
1284
                        dynamic OID = rep.get_GraphicOID();
1285
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1286
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1287
                        int verticesCount = lineStringGeometry.VertexCount;
1288
                        double[] vertices = null;
1289
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1290
                        for (int i = 0; i < verticesCount; i++)
1291
                        {
1292
                            double x = 0;
1293
                            double y = 0;
1294
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1295
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
1296
                        }
1297
                    }
1298
                }
1299

    
1300
                ReleaseCOMObjects(modelItem);
1301
            }
1302

    
1303
            return connectorVertices;
1304
        }
1305

    
1306
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
1307
        {
1308
            double length = double.MaxValue;
1309
            LMConnector targetConnector = null;
1310
            foreach (var item in connectorVertices)
1311
            {
1312
                List<double[]> points = item.Value;
1313
                for (int i = 0; i < points.Count - 1; i++)
1314
                {
1315
                    double[] point1 = points[i];
1316
                    double[] point2 = points[i + 1];
1317

    
1318
                    double maxLineX = Math.Max(point1[0], point2[0]);
1319
                    double minLineX = Math.Min(point1[0], point2[0]);
1320
                    double maxLineY = Math.Max(point1[1], point2[1]);
1321
                    double minLineY = Math.Min(point1[1], point2[1]);
1322

    
1323
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
1324

    
1325
                    // 두직선의 교차점
1326
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
1327
                    if (crossingPoint != null)
1328
                    {
1329
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
1330
                        if (length >= distance)
1331
                        {
1332
                            if (slope == SlopeType.Slope &&
1333
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
1334
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1335
                            {
1336
                                targetConnector = item.Key;
1337
                                length = distance;
1338
                            }
1339
                            else if (slope == SlopeType.HORIZONTAL &&
1340
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
1341
                            {
1342
                                targetConnector = item.Key;
1343
                                length = distance;
1344
                            }
1345
                            else if (slope == SlopeType.VERTICAL &&
1346
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1347
                            {
1348
                                targetConnector = item.Key;
1349
                                length = distance;
1350
                            }
1351
                        }
1352
                    }
1353
                }
1354

    
1355

    
1356
            }
1357

    
1358
            if (targetConnector == null)
1359
            {
1360
                foreach (var item in connectorVertices)
1361
                {
1362
                    List<double[]> points = item.Value;
1363
                    foreach (var point in points)
1364
                    {
1365
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, point[0], point[1]);
1366
                        if (length >= distance)
1367
                        {
1368
                            targetConnector = item.Key;
1369
                            length = distance;
1370
                        }
1371
                    }
1372
                }
1373

    
1374
            }
1375

    
1376
            return targetConnector;
1377
        }
1378

    
1379
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1380
        {
1381
            double length = double.MaxValue;
1382
            LMConnector targetConnector = null;
1383
            foreach (var item in connectorVertices)
1384
            {
1385
                List<double[]> points = item.Value;
1386

    
1387
                foreach (double[] point in points)
1388
                {
1389
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1390
                    if (length >= distance)
1391
                    {
1392
                        targetConnector = item.Key;
1393
                        length = distance;
1394
                    }
1395
                }
1396
            }
1397

    
1398
            return targetConnector;
1399
        }
1400

    
1401
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1402
        {
1403
            double length = double.MaxValue;
1404
            LMConnector targetConnector = null;
1405
            foreach (var item in connectorVertices)
1406
            {
1407
                List<double[]> points = item.Value;
1408
                for (int i = 0; i < points.Count - 1; i++)
1409
                {
1410
                    double[] point1 = points[i];
1411
                    double[] point2 = points[i + 1];
1412
                    double x1 = Math.Min(point1[0], point2[0]);
1413
                    double y1 = Math.Min(point1[1], point2[1]);
1414
                    double x2 = Math.Max(point1[0], point2[0]);
1415
                    double y2 = Math.Max(point1[1], point2[1]);
1416

    
1417
                    if ((x1 <= connX && x2 >= connX) ||
1418
                        (y1 <= connY && y2 >= connY))
1419
                    {
1420
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
1421
                        if (length >= distance)
1422
                        {
1423
                            targetConnector = item.Key;
1424
                            length = distance;
1425
                        }
1426

    
1427
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
1428
                        if (length >= distance)
1429
                        {
1430
                            targetConnector = item.Key;
1431
                            length = distance;
1432
                        }
1433
                    }
1434
                }
1435
            }
1436

    
1437
            // 못찾았을때.
1438
            length = double.MaxValue;
1439
            if (targetConnector == null)
1440
            {
1441
                foreach (var item in connectorVertices)
1442
                {
1443
                    List<double[]> points = item.Value;
1444

    
1445
                    foreach (double[] point in points)
1446
                    {
1447
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1448
                        if (length >= distance)
1449
                        {
1450
                            targetConnector = item.Key;
1451
                            length = distance;
1452
                        }
1453
                    }
1454
                }
1455
            }
1456

    
1457
            return targetConnector;
1458
        }
1459

    
1460
        private void LineNumberModeling(LineNumber lineNumber)
1461
        {
1462
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
1463
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
1464
            LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
1465
            if (connectedLMConnector != null)
1466
            {
1467
                double x = 0;
1468
                double y = 0;
1469
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
1470

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

    
1475
                foreach (var item in connectorVertices)
1476
                    ReleaseCOMObjects(item.Key);
1477
                if (_LmLabelPresist != null)
1478
                {
1479
                    _LmLabelPresist.Commit();
1480
                    lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
1481
                    ReleaseCOMObjects(_LmLabelPresist);
1482
                }
1483
                else
1484
                {
1485

    
1486
                }
1487
            }
1488
        }
1489

    
1490
        private void InputLineNumberAttribute(LineNumber lineNumber)
1491
        {
1492
            foreach (LineRun run in lineNumber.RUNS)
1493
            {
1494
                foreach (var item in run.RUNITEMS)
1495
                {
1496
                    if (item.GetType() == typeof(Symbol))
1497
                    {
1498
                        Symbol symbol = item as Symbol;
1499
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1500
                        LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1501

    
1502
                        if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
1503
                        {
1504
                            foreach (var attribute in lineNumber.ATTRIBUTES)
1505
                            {
1506
                                LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1507
                                if (mapping != null)
1508
                                {
1509
                                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1510
                                    if (_LMAAttribute != null)
1511
                                    {
1512
                                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1513
                                            _LMAAttribute.set_Value(attribute.VALUE);
1514
                                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
1515
                                            _LMAAttribute.set_Value(attribute.VALUE);
1516
                                    }
1517
                                }
1518
                            }
1519
                            _LMModelItem.Commit();
1520
                        }
1521
                        if (_LMModelItem != null)
1522
                            ReleaseCOMObjects(_LMModelItem);
1523
                        if (_LMSymbol != null)
1524
                            ReleaseCOMObjects(_LMSymbol);
1525
                    }
1526
                    else if (item.GetType() == typeof(Line))
1527
                    {
1528
                        Line line = item as Line;
1529
                        if (line != null)
1530
                        {
1531
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1532
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
1533
                            {
1534
                                foreach (var attribute in lineNumber.ATTRIBUTES)
1535
                                {
1536
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1537
                                    if (mapping != null)
1538
                                    {
1539
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1540
                                        if (_LMAAttribute != null)
1541
                                        {
1542
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1543
                                                _LMAAttribute.set_Value(attribute.VALUE);
1544
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
1545
                                                _LMAAttribute.set_Value(attribute.VALUE);
1546
                                            
1547
                                        }
1548
                                    }
1549
                                }
1550
                                _LMModelItem.Commit();
1551
                            }
1552
                            if (_LMModelItem != null)
1553
                                ReleaseCOMObjects(_LMModelItem);
1554
                        }
1555
                    }
1556
                }
1557
            }
1558
        }
1559

    
1560
        private void InputSymbolAttribute(Symbol symbol)
1561
        {
1562
            try
1563
            {
1564
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1565
                {
1566
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1567
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1568
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
1569

    
1570
                    foreach (var item in symbol.PROPERTIES)
1571
                    {
1572

    
1573

    
1574
                    }
1575

    
1576
                    foreach (var item in symbol.ATTRIBUTES)
1577
                    {
1578
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
1579
                        if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
1580
                        {
1581
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
1582
                            if (_Attribute != null)
1583
                            {
1584
                                // 임시
1585
                                if (item.ATTRIBUTETYPE == "String")
1586
                                {
1587
                                    if (!string.IsNullOrEmpty(item.VALUE))
1588
                                    {
1589
                                        if (!DBNull.Value.Equals(_Attribute.get_Value()) && !string.IsNullOrEmpty(_Attribute.get_Value()))
1590
                                        {
1591
                                            string value = _Attribute.get_Value() + "\n" + item.VALUE;
1592
                                            _Attribute.set_Value(value);
1593
                                        }
1594
                                        else
1595
                                        {
1596
                                            _Attribute.set_Value(item.VALUE);
1597
                                        }
1598
                                    }
1599
                                }
1600
                                else
1601
                                {
1602
                                    _Attribute.set_Value(item.VALUE);
1603
                                }
1604
                            }
1605
                        }
1606
                    }
1607

    
1608
                    foreach (var item in symbol.ASSOCIATIONS)
1609
                    {
1610

    
1611
                    }
1612

    
1613
                    ReleaseCOMObjects(_LMSymbol);
1614
                    ReleaseCOMObjects(_Attributes);
1615
                    ReleaseCOMObjects(_LMModelItem);
1616
                }
1617
            }
1618
            catch (Exception ex)
1619
            {
1620
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1621
            }
1622
        }
1623

    
1624
        private void TextModeling(Text text)
1625
        {
1626
            LMSymbol _LMSymbol = null;
1627
            try
1628
            {
1629
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
1630
                if (text.ASSOCIATION)
1631
                {
1632
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
1633
                    if (owner.GetType() == typeof(Symbol))
1634
                    {
1635
                        Symbol symbol = owner as Symbol;
1636
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1637
                        if (_LMSymbol != null)
1638
                        {
1639
                            Association association = symbol.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
1640
                            List<BaseModel.Attribute> attributes = symbol.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
1641
                            AttributeMapping mapping = null;
1642
                            foreach (var attribute in attributes)
1643
                            {
1644
                                if (string.IsNullOrEmpty(attribute.VALUE) || attribute.VALUE == "None")
1645
                                    continue;
1646

    
1647
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
1648
                                if (mapping != null)
1649
                                    break;  
1650
                            }
1651

    
1652
                            if (mapping != null)
1653
                            {
1654
                                double x = 0;
1655
                                double y = 0;
1656

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

    
1660
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: true);
1661
                                if (_LMLabelPersist!=null)
1662
                                {
1663
                                    _LMLabelPersist.Commit();
1664
                                    ReleaseCOMObjects(_LMLabelPersist);
1665
                                }
1666
                            }
1667
                        }
1668
                    }
1669
                    else if (owner.GetType() == typeof(Line))
1670
                    {
1671

    
1672
                    }
1673
                }
1674
                else
1675
                {
1676
                    LMItemNote _LMItemNote = null;
1677
                    LMAAttribute _LMAAttribute = null;
1678

    
1679
                    double x = 0;
1680
                    double y = 0;
1681

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

    
1684
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y);
1685
                    _LMSymbol.Commit();
1686
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1687
                    _LMItemNote.Commit();
1688
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1689
                    _LMAAttribute.set_Value(text.VALUE);
1690
                    _LMItemNote.Commit();
1691

    
1692
                    if (_LMAAttribute != null)
1693
                        ReleaseCOMObjects(_LMAAttribute);
1694
                    if (_LMItemNote != null)
1695
                        ReleaseCOMObjects(_LMItemNote);
1696
                }
1697
            }
1698
            catch (Exception ex)
1699
            {
1700

    
1701
            }
1702
            finally
1703
            {
1704
                if (_LMSymbol != null)
1705
                    ReleaseCOMObjects(_LMSymbol);
1706
            }
1707
        }
1708

    
1709
        private void NoteModeling(Note note)
1710
        {
1711
            LMSymbol _LMSymbol = null;
1712
            LMItemNote _LMItemNote = null;
1713
            LMAAttribute _LMAAttribute = null;
1714

    
1715
            try
1716
            {
1717
                double x = 0;
1718
                double y = 0;
1719

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

    
1722
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
1723
                _LMSymbol.Commit();
1724
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1725
                _LMItemNote.Commit();
1726
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1727
                _LMAAttribute.set_Value(note.VALUE);
1728
                _LMItemNote.Commit();
1729
            }
1730
            catch (Exception ex)
1731
            {
1732

    
1733
            }
1734
            finally
1735
            {
1736
                if (_LMAAttribute != null)
1737
                    ReleaseCOMObjects(_LMAAttribute);
1738
                if (_LMItemNote != null)
1739
                    ReleaseCOMObjects(_LMItemNote);
1740
                if (_LMSymbol != null)
1741
                    ReleaseCOMObjects(_LMSymbol);
1742
            }
1743
            
1744
        }
1745

    
1746
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
1747
        {
1748
            if (location == Location.None)
1749
            {
1750
                x = originX;
1751
                y = originY;
1752
            }
1753
            else
1754
            {
1755
                if (location.HasFlag(Location.Center))
1756
                {
1757
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
1758
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
1759
                }
1760

    
1761
                if (location.HasFlag(Location.Left))
1762
                    x = SPPIDLabelLocation.X1;
1763
                else if (location.HasFlag(Location.Right))
1764
                    x = SPPIDLabelLocation.X2;
1765

    
1766
                if (location.HasFlag(Location.Down))
1767
                    y = SPPIDLabelLocation.Y1;
1768
                else if (location.HasFlag(Location.Up))
1769
                    y = SPPIDLabelLocation.Y2;
1770
            }
1771
        }
1772

    
1773
        public void ReleaseCOMObjects(params object[] objVars)
1774
        {
1775
            int intNewRefCount = 0;
1776
            foreach (object obj in objVars)
1777
            {
1778
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
1779
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
1780
            }
1781
        }
1782
    }
1783
}
클립보드 이미지 추가 (최대 크기: 500 MB)