프로젝트

일반

사용자정보

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

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ de8b8993

이력 | 보기 | 이력해설 | 다운로드 (53 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using Llama;
7
using Plaice;
8
using Ingr.RAD2D.Interop.RAD2D;
9
using Ingr.RAD2D.Internal;
10
using Ingr.RAD2D.Helper;
11
using Converter.BaseModel;
12
using Converter.SPPID.Model;
13
using Converter.SPPID.Properties;
14
using Converter.SPPID.Util;
15
using Converter.SPPID.DB;
16
using Ingr.RAD2D.MacroControls.CmdCtrl;
17
using Ingr.RAD2D;
18
using System.Windows;
19
using System.Threading;
20
using System.Drawing;
21
using Microsoft.VisualBasic;
22
using Newtonsoft.Json;
23

    
24
namespace Converter.SPPID
25
{
26
    public class AutoModeling
27
    {
28
        Placement _placement;
29
        LMADataSource dataSource;
30
        Ingr.RAD2D.Application radApp;
31
        SPPID_Document document;
32

    
33
        List<Tuple<string, Line, Line>> BranchLines = new List<Tuple<string, Line, Line>>();
34
        public AutoModeling(SPPID_Document document, Ingr.RAD2D.Application radApp)
35
        {
36
            this.document = document;
37
            this.radApp = radApp;
38
        }
39

    
40
        public void Run()
41
        {
42
            _placement = new Placement();
43
            dataSource = _placement.PIDDataSource;
44
            //dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
45
            //dynamic newDrawing = application.Drawings.Add(document.Unit, document.Template, document.DrawingNumber, document.DrawingName);
46
            //application.ActiveWindow.Fit();
47
            //Thread.Sleep(100);
48
            //application.ActiveWindow.Zoom = 60;
49
            //Thread.Sleep(100);
50

    
51

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

    
100
                // Note Modeling
101
                foreach (var item in document.NOTES)
102
                    NoteModeling(item);
103
                
104
                // Text Modeling
105
                foreach (var item in document.TEXTINFOS)
106
                    TextModeling(item);
107

    
108
                // Input LineNumber Attribute
109
                foreach (var item in document.LINENUMBERS)
110
                    InputLineNumberAttribute(item);
111

    
112
                // Input Symbol Attribute
113
                foreach (var item in document.SYMBOLS)
114
                    InputSymbolAttribute(item);
115

    
116
                #region 임시 Label
117
                foreach (var item in document.SYMBOLS)
118
                {
119
                    if (item.SPPID.MAPPINGNAME.Contains("Labels - "))
120
                    {
121
                        SPPIDSymbolInfo info = item.SPPID;
122
                        Array points = new double[] { 0, item.SPPID.ORIGINAL_X, item.SPPID.ORIGINAL_Y };
123
                        string symbolUID = item.ATTRIBUTES.Find(x => x.ATTRIBUTE == "SYMBOLOWNER").VALUE;
124
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, symbolUID) as Symbol;
125
                        if (symbol != null)
126
                        {
127
                            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
128
                            LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(item.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: _TargetItem.AsLMRepresentation(), IsLeaderVisible: true);
129
                            LMModelItem _LMModelItem = _TargetItem.ModelItemObject;
130
                            LMAAttributes _Attributes = _LMModelItem.Attributes;
131

    
132
                            foreach (var attribute in item.ATTRIBUTES)
133
                            {
134
                                AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
135
                                if (mapping != null)
136
                                {
137
                                    LMAAttribute _LMAAttribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
138
                                    if (_LMAAttribute != null)
139
                                        _LMAAttribute.set_Value(attribute.VALUE);
140
                                }
141
                            }
142
                            _LMModelItem.Commit();
143
                            _LMLabelPresist.Commit();
144
                            ReleaseCOMObjects(_TargetItem);
145
                            ReleaseCOMObjects(_LMLabelPresist);
146
                            ReleaseCOMObjects(_LMModelItem);
147
                            ReleaseCOMObjects(_Attributes);
148
                        }
149
                    }
150
                }
151
                #endregion
152
            }
153
            catch (Exception ex)
154
            {
155
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
156
            }
157
            finally
158
            {
159
                ReleaseCOMObjects(dataSource);
160
                ReleaseCOMObjects(_placement);
161
            }
162

    
163
            //System.Windows.Forms.MessageBox.Show("end");
164
        }
165

    
166
        public void Test()
167
        {
168
            _placement = new Placement();
169
            dataSource = _placement.PIDDataSource;
170

    
171
            DependencyObject drawingObject = radApp.ActiveDocument.SelectSet[0] as DependencyObject;
172
            if (drawingObject != null)
173
            {
174
                string modelitemID = drawingObject.AttributeSets[0][5].GetValue().ToString();
175
                radApp.ActiveDocument.SelectSet.RemoveAll();
176
                LMPipeRun run = dataSource.GetPipeRun(modelitemID);
177
                _LMAItem item = run.AsLMAItem();
178
                string modelitemID2 = item.Id;
179
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
180
                string afterModelItemID = item.Id;
181
                if (modelitemID2 != afterModelItemID)
182
                {
183

    
184
                }
185
            }
186
        }
187

    
188
        private void LineModelingByRun(LineRun run)
189
        {
190
            Line prevLine = null;
191
            List<Line> lines = new List<Line>();
192
            foreach (var item in run.RUNITEMS)
193
            {
194
                // Line일 경우
195
                if (item.GetType() == typeof(Line))
196
                {
197
                    Line line = item as Line;
198
                    if (prevLine == null)
199
                        lines.Add(line);
200
                    else if (prevLine != null)
201
                    {
202
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
203
                            lines.Add(line);
204
                        else
205
                        {
206
                            if (lines.Count > 0)
207
                            {
208
                                LineModeling(lines);
209
                                lines.Clear();
210
                            }
211
                            lines.Add(line);
212
                        }
213
                    }
214

    
215
                    prevLine = line;
216
                }
217
                // Symbol 일 경우
218
                else if (item.GetType() == typeof(Symbol))
219
                {
220
                    if (lines.Count > 0)
221
                    {
222
                        LineModeling(lines);
223
                        lines.Clear();
224
                    }
225
                }
226
            }
227

    
228
            if (lines.Count > 0)
229
                LineModeling(lines);
230
        }
231

    
232
        private void SymbolModelingByRun(LineRun run)
233
        {
234
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
235
            if (run.RUNITEMS.Count > 0)
236
            {
237
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
238
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
239

    
240
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
241
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
242
            }
243

    
244
            Symbol prevSymbol = null;
245
            Symbol targetSymbol = null;
246
            foreach (var item in run.RUNITEMS)
247
            {
248
                if (item.GetType() == typeof(Symbol))
249
                {
250
                    Symbol symbol = item as Symbol;
251
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
252
                    prevSymbol = symbol;
253
                    targetSymbol = symbol;
254
                }
255
                else
256
                {
257
                    targetSymbol = null;
258
                }
259
            }
260
        }
261

    
262
        private void SymbolModelingByRunStart(Symbol symbol, LineRun run)
263
        {
264
            foreach (var connector in symbol.CONNECTORS)
265
            {
266
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
267
                if (targetItem != null &&
268
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
269
                    !IsSameLineNumber(symbol, targetItem))
270
                {
271
                    SymbolModeling(symbol, targetItem as Symbol, null);
272
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
273
                    {
274
                        object item = run.RUNITEMS[i];
275
                        if (item.GetType() == typeof(Symbol))
276
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol, null);
277
                        else
278
                            break;
279
                    }
280
                    break;
281
                }
282
            }
283

    
284

    
285
        }
286

    
287
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
288
        {
289
            foreach (var connector in symbol.CONNECTORS)
290
            {
291
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
292
                if (targetItem != null &&
293
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
294
                    !IsSameLineNumber(symbol, targetItem))
295
                {
296
                    SymbolModeling(symbol, targetItem as Symbol, null);
297
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
298
                    {
299
                        object item = run.RUNITEMS[i];
300
                        if (item.GetType() == typeof(Symbol))
301
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol, null);
302
                        else
303
                            break;
304
                    }
305
                    break;
306
                }
307
            }
308
        }
309

    
310
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
311
        {
312
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
313
                return;
314

    
315
            LMSymbol _LMSymbol = null;
316

    
317
            string mappingPath = symbol.SPPID.MAPPINGNAME;
318
            double x = symbol.SPPID.ORIGINAL_X;
319
            double y = symbol.SPPID.ORIGINAL_Y;
320
            int mirror = 0;
321
            double angle = symbol.ANGLE;
322

    
323
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
324
            {
325
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
326
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
327
                ReleaseCOMObjects(_TargetItem);
328
            }
329
            else if (prevSymbol != null)
330
            {
331
                LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
332
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
333
                double prevX = _PrevSymbol.get_XCoordinate();
334
                double prevY = _PrevSymbol.get_YCoordinate();
335
                if (slopeType == SlopeType.HORIZONTAL)
336
                    y = prevY;
337
                else if (slopeType == SlopeType.VERTICAL)
338
                    x = prevX;
339
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
340
            }
341
            else
342
            {
343
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
344
            }
345

    
346

    
347
            if (_LMSymbol != null)
348
            {
349
                _LMSymbol.Commit();
350
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
351

    
352
                foreach (var item in symbol.ChildSymbols)
353
                    CreateChildSymbol(item, _LMSymbol);
354
            }
355

    
356
            ReleaseCOMObjects(_LMSymbol);
357
        }
358

    
359
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
360
        {
361
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
362
            double x1 = 0;
363
            double x2 = 0;
364
            double y1 = 0;
365
            double y2 = 0;
366
            symbol2d.Range(out x1, out y1, out x2, out y2);
367

    
368
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
369
            if (_LMSymbol != null)
370
            {
371
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
372
                foreach (var item in childSymbol.ChildSymbols)
373
                    CreateChildSymbol(item, _LMSymbol);
374
            }
375
            
376

    
377
            ReleaseCOMObjects(_LMSymbol);
378
        }
379

    
380
        private bool IsSameLineNumber(object item, object targetItem)
381
        {
382
            foreach (var lineNumber in document.LINENUMBERS)
383
            {
384
                foreach (var run in lineNumber.RUNS)
385
                {
386
                    foreach (var runItem in run.RUNITEMS)
387
                    {
388
                        if (runItem == item)
389
                        {
390
                            foreach (var findItem in run.RUNITEMS)
391
                            {
392
                                if (findItem == targetItem)
393
                                {
394
                                    return true;
395
                                }
396
                            }
397

    
398
                            return false;
399

    
400
                        }
401
                    }
402
                }
403
            }
404

    
405
            return false;
406
        }
407

    
408
        private void LineModeling(List<Line> lines)
409
        {
410
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
411
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
412
            LMSymbol _LMSymbol1 = null;
413
            LMSymbol _LMSymbol2 = null;
414
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
415
            LMConnector targetConnector1 = null;
416
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
417
            LMConnector targetConnector2 = null;
418

    
419
            Line startBranchLine = null;
420
            Line endBranchLine = null;
421

    
422
            for (int i = 0; i < lines.Count; i++)
423
            {
424
                Line line = lines[i];
425
                if (i == 0 || i + 1 != lines.Count)
426
                {
427
                    // 시작점에 연결된 Symbol 찾기
428
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
429
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
430
                    {
431
                        _LMSymbol1 = GetTargetSymbol(connItem as Symbol, line);
432
                        if (_LMSymbol1 != null)
433
                            placeRunInputs.AddSymbolTarget(_LMSymbol1, line.SPPID.START_X, line.SPPID.START_Y);
434
                        else
435
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
436
                    }
437
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
438
                    {
439
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
440
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
441

    
442
                        if (targetConnector1 != null)
443
                            placeRunInputs.AddConnectorTarget(targetConnector1, line.SPPID.START_X, line.SPPID.START_Y);
444
                        else
445
                        {
446
                            startBranchLine = connItem as Line;
447
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
448
                        }
449
                    }
450
                    else
451
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
452
                }
453

    
454
                if (i + 1 == lines.Count)
455
                {
456
                    // 끝점에 연결된 Symbol 찾기
457
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
458

    
459
                    if (i != 0)
460
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
461

    
462
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
463
                    {
464
                        _LMSymbol2 = GetTargetSymbol(connItem as Symbol, line);
465
                        if (_LMSymbol2 != null)
466
                            placeRunInputs.AddSymbolTarget(_LMSymbol2, line.SPPID.END_X, line.SPPID.END_Y);
467
                        else
468
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
469
                            
470
                    }
471
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
472
                    {
473
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
474
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
475

    
476
                        if (targetConnector2 != null)
477
                            placeRunInputs.AddConnectorTarget(targetConnector2, line.SPPID.END_X, line.SPPID.END_Y);
478
                        else
479
                        {
480
                            endBranchLine = connItem as Line;
481
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
482
                        }
483
                    }
484
                    else
485
                    {
486
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
487
                    }
488
                }
489
            }
490

    
491
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
492

    
493
            if (_lMConnector != null)
494
            {
495
                foreach (var line in lines)
496
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
497
                _lMConnector.Commit();
498
                if (startBranchLine != null || endBranchLine != null)
499
                {
500
                    BranchLines.Add(new Tuple<string, Line, Line>(_lMConnector.ModelItemID, startBranchLine, endBranchLine));
501
                }
502
            }
503

    
504

    
505
            if (_LMSymbol1 != null)
506
                ReleaseCOMObjects(_LMSymbol1);
507
            if (_LMSymbol2 != null)
508
                ReleaseCOMObjects(_LMSymbol2);
509
            if (targetConnector1 != null)
510
                ReleaseCOMObjects(targetConnector1);
511
            if (targetConnector2 != null)
512
                ReleaseCOMObjects(targetConnector2);
513
            foreach (var item in connectorVertices1)
514
                ReleaseCOMObjects(item.Key);
515
            foreach (var item in connectorVertices2)
516
                ReleaseCOMObjects(item.Key);
517

    
518
            ReleaseCOMObjects(_lMConnector);
519
            ReleaseCOMObjects(placeRunInputs);
520
            ReleaseCOMObjects(_LMAItem);
521
        }
522

    
523
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
524
        {
525
            LMSymbol _LMSymbol = null;
526
            foreach (var connector in symbol.CONNECTORS)
527
            {
528
                if (connector.CONNECTEDITEM == line.UID)
529
                {
530
                    if (connector.Index == 0)
531
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
532
                    else
533
                    {
534
                        ChildSymbol child = null;
535
                        foreach (var childSymbol in symbol.ChildSymbols)
536
                        {
537
                            if (childSymbol.Connectors.Contains(connector))
538
                                child = childSymbol;
539
                            else
540
                                child = GetChildSymbolByConnector(childSymbol, connector);
541

    
542
                            if (child != null)
543
                                break;
544
                        }
545

    
546
                        if (child != null)
547
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
548
                    }
549

    
550
                    break;  
551
                }
552
            }
553

    
554
            return _LMSymbol;
555
        }
556

    
557
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
558
        {
559
            foreach (var childSymbol in item.ChildSymbols)
560
            {
561
                if (childSymbol.Connectors.Contains(connector))
562
                    return childSymbol;
563
                else
564
                    return GetChildSymbolByConnector(childSymbol, connector);
565
            }
566

    
567
            return null;
568
        }
569

    
570
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
571
        {
572
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
573
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
574

    
575
            LMConnector _StartConnector = null;
576
            LMConnector _EndConnector = null;
577
            double lengthStart = double.MaxValue;
578
            double lengthEnd = double.MaxValue;
579
            List<double[]> startPoints = new List<double[]>();
580
            List<double[]> endPoints = new List<double[]>();
581

    
582
            foreach (var item in connectorVertices)
583
            {
584
                foreach (var point in item.Value)
585
                {
586
                    // Start Point가 Branch
587
                    if (branch.Item2 != null)
588
                    {
589
                        Line targetLine = branch.Item2;
590
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
591
                        if (lengthStart > distance)
592
                        {
593
                            _StartConnector = item.Key;
594
                            lengthStart = distance;
595
                            startPoints = item.Value;
596
                        }
597
                    }
598
                    // End Point가 Branch
599
                    if (branch.Item3 != null)
600
                    {
601
                        Line targetLine = branch.Item3;
602
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
603
                        if (lengthEnd > distance)
604
                        {
605
                            _EndConnector = item.Key;
606
                            lengthEnd = distance;
607
                            endPoints = item.Value;
608
                        }
609
                    }
610
                }
611
            }
612
            #region Branch가 양쪽 전부일 때
613
            if (_StartConnector != null && _StartConnector == _EndConnector)
614
            {
615
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
616

    
617
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
618
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
619

    
620
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
621
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
622
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
623
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
624
                   startPoints[startPoints.Count - 1][0],
625
                   startPoints[startPoints.Count - 1][1],
626
                   startPoints[startPoints.Count - 2][0],
627
                   startPoints[startPoints.Count - 2][1]);
628

    
629
                for (int i = 0; i < startPoints.Count; i++)
630
                {
631
                    double[] point = startPoints[i];
632
                    if (i == 0)
633
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
634
                    else if (i == startPoints.Count - 1)
635
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
636
                    else
637
                        placeRunInputs.AddPoint(point[0], point[1]);
638
                }
639

    
640
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
641
                if (_LMConnector != null)
642
                {
643
                    _LMConnector.Commit();
644
                    foreach (var item in lines)
645
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
646
                }
647

    
648
                foreach (var item in startConnectorVertices)
649
                    ReleaseCOMObjects(item.Key);
650
                foreach (var item in endConnectorVertices)
651
                    ReleaseCOMObjects(item.Key);
652
                ReleaseCOMObjects(placeRunInputs);
653
                ReleaseCOMObjects(_LMAItem);
654
                ReleaseCOMObjects(_LMConnector);
655
            }
656
            #endregion
657
            #region 양쪽이 다른 Branch 
658
            else
659
            {
660
                // Branch 시작 Connector
661
                if (_StartConnector != null)
662
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
663

    
664
                // Branch 끝 Connector
665
                if (_EndConnector != null)
666
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
667
            }
668
            #endregion
669

    
670
            if (_StartConnector != null)
671
                ReleaseCOMObjects(_StartConnector);
672
            if (_EndConnector != null)
673
                ReleaseCOMObjects(_EndConnector);
674
            foreach (var item in connectorVertices)
675
                ReleaseCOMObjects(item.Key);
676
        }
677

    
678
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
679
        {
680
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
681
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
682
            LMConnector _SameRunTargetConnector = null;
683
            LMSymbol _SameRunTargetSymbol = null;
684
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
685
            LMConnector _BranchTargetConnector = null;
686
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
687

    
688
            // 같은 Line Run의 Connector 찾기
689
            foreach (var item in connectorVertices)
690
            {
691
                if (item.Key == _Connector)
692
                    continue;
693

    
694
                if (IsStart &&
695
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
696
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
697
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
698
                {
699
                    _SameRunTargetConnector = item.Key;
700
                    break;
701
                }
702
                else if (!IsStart &&
703
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
704
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
705
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
706
                {
707
                    _SameRunTargetConnector = item.Key;
708
                    break;
709
                }
710
            }
711

    
712
            // Branch 반대편이 Symbol
713
            if (_SameRunTargetConnector == null)
714
            {
715
                foreach (var line in lines)
716
                {
717
                    foreach (var connector in line.CONNECTORS)
718
                    {
719
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
720
                        if (symbol != null)
721
                        {
722
                            _SameRunTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
723
                            break;
724
                        }
725
                    }
726
                }
727
            }
728

    
729
            // 기존 Connector 제거
730
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
731
            
732
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
733
            if (IsStart)
734
            {
735
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
736
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
737
            }
738
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
739
            else
740
            {
741
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
742
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
743
                    points[points.Count - 1][0],
744
                    points[points.Count - 1][1],
745
                    points[points.Count - 2][0],
746
                    points[points.Count - 2][1]);
747
            }
748

    
749
            for (int i = 0; i < points.Count; i++)
750
            {
751
                double[] point = points[i];
752
                if (i == 0)
753
                {
754
                    if (IsStart)
755
                    {
756
                        placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
757
                    }
758
                    else
759
                    {
760
                        if (_SameRunTargetConnector != null)
761
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
762
                        else if (_SameRunTargetSymbol != null)
763
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
764
                        else
765
                            placeRunInputs.AddPoint(point[0], point[1]);
766
                    }
767
                }
768
                else if (i == points.Count - 1)
769
                {
770
                    if (IsStart)
771
                    {
772
                        if (_SameRunTargetConnector != null)
773
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
774
                        else if (_SameRunTargetSymbol != null)
775
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
776
                        else
777
                            placeRunInputs.AddPoint(point[0], point[1]);
778
                    }
779
                    else
780
                    {
781
                        placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
782
                    }
783
                }
784
                else
785
                    placeRunInputs.AddPoint(point[0], point[1]);
786
            }
787
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
788
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
789

    
790
            if (_LMConnector != null)
791
            {
792
                if (_SameRunTargetConnector != null)
793
                {
794
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
795
                }
796
                else
797
                {
798
                    foreach (var item in lines)
799
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
800
                }
801

    
802
                _LMConnector.Commit();
803
                ReleaseCOMObjects(_LMConnector);
804
            }
805

    
806
            ReleaseCOMObjects(placeRunInputs);
807
            ReleaseCOMObjects(_LMAItem);
808
            if (_BranchTargetConnector != null)
809
                ReleaseCOMObjects(_BranchTargetConnector);
810
            if (_SameRunTargetConnector != null)
811
                ReleaseCOMObjects(_SameRunTargetConnector);
812
            if (_SameRunTargetSymbol != null)
813
                ReleaseCOMObjects(_SameRunTargetSymbol);
814
            foreach (var item in connectorVertices)
815
                ReleaseCOMObjects(item.Key);
816
            foreach (var item in branchConnectorVertices)
817
                ReleaseCOMObjects(item.Key);
818
        }
819

    
820
        private void EndBreakModeling(EndBreak endBreak)
821
        {
822
            Line ownerLine = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER) as Line;
823
            
824
            if (ownerLine != null)
825
            {
826
                LMLabelPersist _LmLabelPersist = null;
827
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(ownerLine.SPPID.ModelItemId);
828

    
829
                LMConnector connectedLMConnector = FindTargetLMConnectorByPoint(connectorVertices, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
830
                if (connectedLMConnector != null)
831
                {
832
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
833
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
834
                    ReleaseCOMObjects(connectedLMConnector);
835
                }
836

    
837
                if (_LmLabelPersist != null)
838
                {
839
                    _LmLabelPersist.Commit();
840
                    ReleaseCOMObjects(_LmLabelPersist);
841
                }
842
                foreach (var item in connectorVertices)
843
                    ReleaseCOMObjects(item.Key);
844
            }
845
        }
846

    
847
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
848
        {
849
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
850
            _LMAItem item1 = modelItem1.AsLMAItem();
851
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
852
            _LMAItem item2 = modelItem2.AsLMAItem();
853
            
854
            // item2가 item1으로 조인
855
            try
856
            {
857
                _placement.PIDJoinRuns(ref item1, ref item2);
858
                item1.Commit();
859
                item2.Commit();
860
            }
861
            catch (Exception ex)
862
            {
863
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
864
            }
865
            finally
866
            {
867
                ReleaseCOMObjects(modelItem1);
868
                ReleaseCOMObjects(item1);
869
                ReleaseCOMObjects(modelItem2);
870
                ReleaseCOMObjects(item2);
871
            }
872
        }
873

    
874
        private void AutoJoinPipeRun(string modelItemId)
875
        {
876
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
877
            _LMAItem item = modelItem.AsLMAItem();
878
            try
879
            {
880
                string modelitemID = item.Id;
881
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
882
                string afterModelItemID = item.Id;
883
                if (modelitemID != afterModelItemID)
884
                {
885
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
886
                    foreach (var line in lines)
887
                        line.SPPID.ModelItemId = afterModelItemID;
888
                }
889
                item.Commit();
890
            }
891
            catch (Exception ex)
892
            {
893
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
894
            }
895
            finally
896
            {
897
                ReleaseCOMObjects(modelItem);
898
                ReleaseCOMObjects(item);
899
            }
900
        }
901

    
902
        private void JoinRunLine(LineRun run)
903
        {
904
            string modelItemId = string.Empty;
905
            foreach (var item in run.RUNITEMS)
906
            {
907
                if (item.GetType() == typeof(Line))
908
                {
909
                    Line line = item as Line;
910
                    if (modelItemId != line.SPPID.ModelItemId)
911
                    {
912
                        AutoJoinPipeRun(line.SPPID.ModelItemId);
913
                        modelItemId = line.SPPID.ModelItemId;
914
                    }
915
                }
916
            }
917
        }
918

    
919
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
920
        {
921
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
922
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
923

    
924
            if (modelItem != null)
925
            {
926
                foreach (LMRepresentation rep in modelItem.Representations)
927
                {
928
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
929
                    {
930
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
931
                        connectorVertices.Add(_LMConnector, new List<double[]>());
932
                        dynamic OID = rep.get_GraphicOID();
933
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
934
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
935
                        int verticesCount = lineStringGeometry.VertexCount;
936
                        double[] vertices = null;
937
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
938
                        for (int i = 0; i < verticesCount; i++)
939
                        {
940
                            double x = 0;
941
                            double y = 0;
942
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
943
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
944
                        }
945
                    }
946
                }
947

    
948
                ReleaseCOMObjects(modelItem);
949
            }
950

    
951
            return connectorVertices;
952
        }
953

    
954
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
955
        {
956
            double length = double.MaxValue;
957
            LMConnector targetConnector = null;
958
            foreach (var item in connectorVertices)
959
            {
960
                List<double[]> points = item.Value;
961
                for (int i = 0; i < points.Count - 1; i++)
962
                {
963
                    double[] point1 = points[i];
964
                    double[] point2 = points[i + 1];
965

    
966
                    double maxLineX = Math.Max(point1[0], point2[0]);
967
                    double minLineX = Math.Min(point1[0], point2[0]);
968
                    double maxLineY = Math.Max(point1[1], point2[1]);
969
                    double minLineY = Math.Min(point1[1], point2[1]);
970

    
971
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
972

    
973
                    // 두직선의 교차점
974
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
975
                    if (crossingPoint != null)
976
                    {
977
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
978
                        if (length >= distance)
979
                        {
980
                            if (slope == SlopeType.Slope &&
981
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
982
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
983
                            {
984
                                targetConnector = item.Key;
985
                                length = distance;
986
                            }
987
                            else if (slope == SlopeType.HORIZONTAL &&
988
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
989
                            {
990
                                targetConnector = item.Key;
991
                                length = distance;
992
                            }
993
                            else if (slope == SlopeType.VERTICAL &&
994
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
995
                            {
996
                                targetConnector = item.Key;
997
                                length = distance;
998
                            }
999
                        }
1000
                    }
1001
                }
1002
            }
1003

    
1004
            return targetConnector;
1005
        }
1006

    
1007
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1008
        {
1009
            double length = double.MaxValue;
1010
            LMConnector targetConnector = null;
1011
            foreach (var item in connectorVertices)
1012
            {
1013
                List<double[]> points = item.Value;
1014

    
1015
                foreach (double[] point in points)
1016
                {
1017
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1018
                    if (length >= distance)
1019
                    {
1020
                        targetConnector = item.Key;
1021
                        length = distance;
1022
                    }
1023
                }
1024
            }
1025

    
1026
            return targetConnector;
1027
        }
1028

    
1029
        private void LineNumberModeling(LineNumber lineNumber)
1030
        {
1031
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
1032
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
1033
            LMConnector connectedLMConnector = FindTargetLMConnectorByPoint(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
1034
            if (connectedLMConnector != null)
1035
            {
1036
                Array points = new double[] { 0, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y };
1037
                LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: false);
1038
                _LmLabelPresist.Commit();
1039

    
1040
                foreach (var item in connectorVertices)
1041
                    ReleaseCOMObjects(item.Key);
1042
                if (_LmLabelPresist != null)
1043
                {
1044
                    lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
1045
                    ReleaseCOMObjects(_LmLabelPresist);
1046
                }
1047
            }
1048
        }
1049

    
1050
        private void InputLineNumberAttribute(LineNumber lineNumber)
1051
        {
1052
            foreach (var run in lineNumber.RUNS)
1053
            {
1054
                foreach (var item in run.RUNITEMS)
1055
                {
1056
                    if (item.GetType() == typeof(Line))
1057
                    {
1058
                        Line line = item as Line;
1059
                        LMPipeRun _LMPipeRun = dataSource.GetPipeRun(line.SPPID.ModelItemId);
1060
                        foreach (var attribute in lineNumber.ATTRIBUTES)
1061
                        {
1062
                            LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1063
                            if (mapping != null)
1064
                            {
1065
                                LMAAttribute _LMAAttribute = _LMPipeRun.Attributes[mapping.SPPIDATTRIBUTENAME];
1066
                                if (_LMAAttribute != null)
1067
                                {
1068
                                    if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1069
                                        _LMAAttribute.set_Value(attribute.VALUE);
1070
                                    else if (_LMAAttribute.get_Value() != attribute.VALUE)
1071
                                        _LMAAttribute.set_Value(attribute.VALUE);
1072
                                }
1073
                            }
1074
                        }
1075

    
1076
                        _LMPipeRun.Commit();
1077
                        ReleaseCOMObjects(_LMPipeRun);
1078
                        break;
1079
                    }
1080
                }
1081
            }
1082
        }
1083

    
1084
        private void InputSymbolAttribute(Symbol symbol)
1085
        {
1086
            try
1087
            {
1088
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1089
                {
1090
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1091
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1092
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
1093

    
1094
                    foreach (var item in symbol.PROPERTIES)
1095
                    {
1096

    
1097

    
1098
                    }
1099

    
1100
                    foreach (var item in symbol.ATTRIBUTES)
1101
                    {
1102
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
1103
                        if (mapping != null)
1104
                        {
1105
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
1106
                            if (_Attribute != null)
1107
                            {
1108
                                // 임시
1109
                                if (item.ATTRIBUTETYPE == "String")
1110
                                {
1111
                                    if (!string.IsNullOrEmpty(item.VALUE))
1112
                                    {
1113
                                        if (!DBNull.Value.Equals(_Attribute.get_Value()) && !string.IsNullOrEmpty(_Attribute.get_Value()))
1114
                                        {
1115
                                            string value = _Attribute.get_Value() + "\n" + item.VALUE;
1116
                                            _Attribute.set_Value(value);
1117
                                        }
1118
                                        else
1119
                                        {
1120
                                            _Attribute.set_Value(item.VALUE);
1121
                                        }
1122
                                    }
1123
                                }
1124
                                else
1125
                                {
1126
                                    _Attribute.set_Value(item.VALUE);
1127
                                }
1128
                            }
1129
                        }
1130
                    }
1131

    
1132
                    foreach (var item in symbol.ASSOCIATIONS)
1133
                    {
1134

    
1135
                    }
1136

    
1137
                    ReleaseCOMObjects(_LMSymbol);
1138
                    ReleaseCOMObjects(_Attributes);
1139
                    ReleaseCOMObjects(_LMModelItem);
1140
                }
1141
            }
1142
            catch (Exception ex)
1143
            {
1144
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1145
            }
1146
        }
1147

    
1148
        private void TextModeling(Text text)
1149
        {
1150
            LMSymbol _LMSymbol = null;
1151
            try
1152
            {
1153
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
1154
                if (text.ASSOCIATION)
1155
                {
1156
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
1157
                    if (owner.GetType() == typeof(Symbol))
1158
                    {
1159
                        Symbol symbol = owner as Symbol;
1160
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1161
                        if (_LMSymbol != null)
1162
                        {
1163
                            Association association = symbol.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
1164
                            List<BaseModel.Attribute> attributes = symbol.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
1165
                            AttributeMapping mapping = null;
1166
                            foreach (var attribute in attributes)
1167
                            {
1168
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
1169
                                if (mapping != null)
1170
                                    break;  
1171
                            }
1172

    
1173
                            if (mapping != null)
1174
                            {
1175
                                Array array = new double[] { 0, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y };
1176
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: true);
1177
                                if (_LMLabelPersist!=null)
1178
                                {
1179
                                    _LMLabelPersist.Commit();
1180
                                    ReleaseCOMObjects(_LMLabelPersist);
1181
                                }
1182
                            }
1183
                        }
1184
                    }
1185
                    else if (owner.GetType() == typeof(Line))
1186
                    {
1187

    
1188
                    }
1189
                }
1190
                else
1191
                {
1192
                    LMItemNote _LMItemNote = null;
1193
                    LMAAttribute _LMAAttribute = null;
1194

    
1195
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
1196
                    _LMSymbol.Commit();
1197
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1198
                    _LMItemNote.Commit();
1199
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1200
                    _LMAAttribute.set_Value(text.VALUE);
1201
                    _LMItemNote.Commit();
1202

    
1203
                    if (_LMAAttribute != null)
1204
                        ReleaseCOMObjects(_LMAAttribute);
1205
                    if (_LMItemNote != null)
1206
                        ReleaseCOMObjects(_LMItemNote);
1207
                }
1208
            }
1209
            catch (Exception ex)
1210
            {
1211

    
1212
            }
1213
            finally
1214
            {
1215
                if (_LMSymbol != null)
1216
                    ReleaseCOMObjects(_LMSymbol);
1217
            }
1218
        }
1219

    
1220
        private void NoteModeling(Note note)
1221
        {
1222
            LMSymbol _LMSymbol = null;
1223
            LMItemNote _LMItemNote = null;
1224
            LMAAttribute _LMAAttribute = null;
1225

    
1226
            try
1227
            {
1228
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, note.SPPID.ORIGINAL_X, note.SPPID.ORIGINAL_Y);
1229
                _LMSymbol.Commit();
1230
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1231
                _LMItemNote.Commit();
1232
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1233
                _LMAAttribute.set_Value(note.VALUE);
1234
                _LMItemNote.Commit();
1235
            }
1236
            catch (Exception ex)
1237
            {
1238

    
1239
            }
1240
            finally
1241
            {
1242
                if (_LMAAttribute != null)
1243
                    ReleaseCOMObjects(_LMAAttribute);
1244
                if (_LMItemNote != null)
1245
                    ReleaseCOMObjects(_LMItemNote);
1246
                if (_LMSymbol != null)
1247
                    ReleaseCOMObjects(_LMSymbol);
1248
            }
1249
            
1250
        }
1251

    
1252
        public void ReleaseCOMObjects(params object[] objVars)
1253
        {
1254
            int intNewRefCount = 0;
1255
            foreach (object obj in objVars)
1256
            {
1257
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
1258
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
1259
            }
1260
        }
1261
    }
1262
}
클립보드 이미지 추가 (최대 크기: 500 MB)