프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ a3557cbc

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

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

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

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

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

    
53

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

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

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

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

    
118
                #region 임시 Label
119
                foreach (var item in document.SYMBOLS)
120
                {
121
                    if (item.SPPID.MAPPINGNAME.Contains("Labels - "))
122
                    {
123
                        SPPIDSymbolInfo info = item.SPPID;
124
                        Array points = new double[] { 0, item.SPPID.ORIGINAL_X, item.SPPID.ORIGINAL_Y };
125
                        string symbolUID = item.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL").VALUE;
126
                        object objectItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
127
                        if (objectItem != null)
128
                        {
129

    
130
                            string sRep = null;
131
                            if (objectItem.GetType() == typeof(Symbol))
132
                                sRep = ((Symbol)objectItem).SPPID.RepresentationId;
133
                            else if (objectItem.GetType() == typeof(Equipment))
134
                                sRep = ((Equipment)objectItem).SPPID.RepresentationId;
135

    
136
                            if (!string.IsNullOrEmpty(sRep))
137
                            {
138
                                LMSymbol _TargetItem = dataSource.GetSymbol(sRep);
139
                                LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(item.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: _TargetItem.AsLMRepresentation(), IsLeaderVisible: true);
140
                                LMModelItem _LMModelItem = _TargetItem.ModelItemObject;
141
                                LMAAttributes _Attributes = _LMModelItem.Attributes;
142

    
143
                                foreach (var attribute in item.ATTRIBUTES)
144
                                {
145
                                    if (!string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
146
                                    {
147
                                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
148
                                        if (mapping != null)
149
                                        {
150
                                            LMAAttribute _LMAAttribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
151
                                            if (mapping.SPPIDATTRIBUTENAME.Contains("Description"))
152
                                            {
153
                                                if (!DBNull.Value.Equals(_LMAAttribute.get_Value()) && !string.IsNullOrEmpty(_LMAAttribute.get_Value()))
154
                                                {
155
                                                    string value = _LMAAttribute.get_Value() + "\n" + attribute.VALUE;
156
                                                    _LMAAttribute.set_Value(value);
157
                                                }
158
                                                else
159
                                                {
160
                                                    _LMAAttribute.set_Value(attribute.VALUE);
161
                                                }
162
                                            }
163
                                            else if (_LMAAttribute != null)
164
                                                _LMAAttribute.set_Value(attribute.VALUE);
165
                                        }
166
                                    }
167
                                }
168

    
169
                                
170

    
171
                                _LMModelItem.Commit();
172
                                _LMLabelPresist.Commit();
173
                                ReleaseCOMObjects(_TargetItem);
174
                                ReleaseCOMObjects(_LMLabelPresist);
175
                                ReleaseCOMObjects(_LMModelItem);
176
                                ReleaseCOMObjects(_Attributes);
177
                            }
178
                        }
179
                    }
180
                }
181
                #endregion
182
            }
183
            catch (Exception ex)
184
            {
185
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
186
            }
187
            finally
188
            {
189
                ReleaseCOMObjects(dataSource);
190
                ReleaseCOMObjects(_placement);
191
            }
192

    
193
            //System.Windows.Forms.MessageBox.Show("end");
194
        }
195

    
196
        public void Test()
197
        {
198
            _placement = new Placement();
199
            dataSource = _placement.PIDDataSource;
200

    
201
            DependencyObject drawingObject = radApp.ActiveDocument.SelectSet[0] as DependencyObject;
202
            if (drawingObject != null)
203
            {
204
                string modelitemID = drawingObject.AttributeSets[0][5].GetValue().ToString();
205
                radApp.ActiveDocument.SelectSet.RemoveAll();
206
                LMPipeRun run = dataSource.GetPipeRun(modelitemID);
207
                _LMAItem item = run.AsLMAItem();
208
                string modelitemID2 = item.Id;
209
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
210
                string afterModelItemID = item.Id;
211
                if (modelitemID2 != afterModelItemID)
212
                {
213

    
214
                }
215
            }
216
        }
217

    
218
        private void LineModelingByRun(LineRun run)
219
        {
220
            Line prevLine = null;
221
            List<Line> lines = new List<Line>();
222
            foreach (var item in run.RUNITEMS)
223
            {
224
                // Line일 경우
225
                if (item.GetType() == typeof(Line))
226
                {
227
                    Line line = item as Line;
228
                    if (prevLine == null)
229
                        lines.Add(line);
230
                    else if (prevLine != null)
231
                    {
232
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
233
                            lines.Add(line);
234
                        else
235
                        {
236
                            if (lines.Count > 0)
237
                            {
238
                                LineModeling(lines);
239
                                lines.Clear();
240
                            }
241
                            lines.Add(line);
242
                        }
243
                    }
244

    
245
                    prevLine = line;
246
                }
247
                // Symbol 일 경우
248
                else if (item.GetType() == typeof(Symbol))
249
                {
250
                    if (lines.Count > 0)
251
                    {
252
                        LineModeling(lines);
253
                        lines.Clear();
254
                    }
255
                }
256
            }
257

    
258
            if (lines.Count > 0)
259
                LineModeling(lines);
260
        }
261

    
262
        private void SymbolModelingByRun(LineRun run)
263
        {
264
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
265
            if (run.RUNITEMS.Count > 0)
266
            {
267
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
268
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
269

    
270
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
271
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
272
            }
273

    
274
            Symbol prevSymbol = null;
275
            Symbol targetSymbol = null;
276
            foreach (var item in run.RUNITEMS)
277
            {
278
                if (item.GetType() == typeof(Symbol))
279
                {
280
                    Symbol symbol = item as Symbol;
281
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
282
                    prevSymbol = symbol;
283
                    targetSymbol = symbol;
284
                }
285
                else
286
                {
287
                    targetSymbol = null;
288
                }
289
            }
290
        }
291

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

    
314

    
315
        }
316

    
317
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
318
        {
319
            foreach (var connector in symbol.CONNECTORS)
320
            {
321
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
322
                if (targetItem != null &&
323
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
324
                    !IsSameLineNumber(symbol, targetItem))
325
                {
326
                    SymbolModeling(symbol, targetItem as Symbol, null);
327
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
328
                    {
329
                        object item = run.RUNITEMS[i];
330
                        if (item.GetType() == typeof(Symbol))
331
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol, null);
332
                        else
333
                            break;
334
                    }
335
                    break;
336
                }
337
            }
338
        }
339

    
340
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
341
        {
342
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
343
                return;
344

    
345
            LMSymbol _LMSymbol = null;
346

    
347
            string mappingPath = symbol.SPPID.MAPPINGNAME;
348
            double x = symbol.SPPID.ORIGINAL_X;
349
            double y = symbol.SPPID.ORIGINAL_Y;
350
            int mirror = 0;
351
            double angle = symbol.ANGLE;
352

    
353
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
354
            {
355
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
356
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
357
                ReleaseCOMObjects(_TargetItem);
358
            }
359
            else if (prevSymbol != null)
360
            {
361
                LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
362
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
363
                double prevX = _PrevSymbol.get_XCoordinate();
364
                double prevY = _PrevSymbol.get_YCoordinate();
365
                if (slopeType == SlopeType.HORIZONTAL)
366
                    y = prevY;
367
                else if (slopeType == SlopeType.VERTICAL)
368
                    x = prevX;
369
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
370
            }
371
            else
372
            {
373
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
374
            }
375

    
376

    
377
            if (_LMSymbol != null)
378
            {
379
                _LMSymbol.Commit();
380
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
381

    
382
                foreach (var item in symbol.ChildSymbols)
383
                    CreateChildSymbol(item, _LMSymbol);
384
            }
385

    
386
            ReleaseCOMObjects(_LMSymbol);
387
        }
388

    
389
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
390
        {
391
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
392
            double x1 = 0;
393
            double x2 = 0;
394
            double y1 = 0;
395
            double y2 = 0;
396
            symbol2d.Range(out x1, out y1, out x2, out y2);
397

    
398
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
399
            if (_LMSymbol != null)
400
            {
401
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
402
                foreach (var item in childSymbol.ChildSymbols)
403
                    CreateChildSymbol(item, _LMSymbol);
404
            }
405
            
406

    
407
            ReleaseCOMObjects(_LMSymbol);
408
        }
409

    
410
        private bool IsSameLineNumber(object item, object targetItem)
411
        {
412
            foreach (var lineNumber in document.LINENUMBERS)
413
            {
414
                foreach (var run in lineNumber.RUNS)
415
                {
416
                    foreach (var runItem in run.RUNITEMS)
417
                    {
418
                        if (runItem == item)
419
                        {
420
                            foreach (var findItem in run.RUNITEMS)
421
                            {
422
                                if (findItem == targetItem)
423
                                {
424
                                    return true;
425
                                }
426
                            }
427

    
428
                            return false;
429

    
430
                        }
431
                    }
432
                }
433
            }
434

    
435
            return false;
436
        }
437

    
438
        private void LineModeling(List<Line> lines)
439
        {
440
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
441
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
442
            LMSymbol _LMSymbol1 = null;
443
            LMSymbol _LMSymbol2 = null;
444
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
445
            LMConnector targetConnector1 = null;
446
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
447
            LMConnector targetConnector2 = null;
448

    
449
            Line startBranchLine = null;
450
            Line endBranchLine = null;
451

    
452
            for (int i = 0; i < lines.Count; i++)
453
            {
454
                Line line = lines[i];
455
                if (i == 0 || i + 1 != lines.Count)
456
                {
457
                    // 시작점에 연결된 Symbol 찾기
458
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
459
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
460
                    {
461
                        _LMSymbol1 = GetTargetSymbol(connItem as Symbol, line);
462
                        if (_LMSymbol1 != null)
463
                            placeRunInputs.AddSymbolTarget(_LMSymbol1, line.SPPID.START_X, line.SPPID.START_Y);
464
                        else
465
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
466
                    }
467
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
468
                    {
469
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
470
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
471

    
472
                        if (targetConnector1 != null)
473
                            placeRunInputs.AddConnectorTarget(targetConnector1, line.SPPID.START_X, line.SPPID.START_Y);
474
                        else
475
                        {
476
                            startBranchLine = connItem as Line;
477
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
478
                        }
479
                    }
480
                    else
481
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
482
                }
483

    
484
                if (i + 1 == lines.Count)
485
                {
486
                    // 끝점에 연결된 Symbol 찾기
487
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
488

    
489
                    if (i != 0)
490
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
491

    
492
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
493
                    {
494
                        _LMSymbol2 = GetTargetSymbol(connItem as Symbol, line);
495
                        if (_LMSymbol2 != null)
496
                            placeRunInputs.AddSymbolTarget(_LMSymbol2, line.SPPID.END_X, line.SPPID.END_Y);
497
                        else
498
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
499
                            
500
                    }
501
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
502
                    {
503
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
504
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
505

    
506
                        if (targetConnector2 != null)
507
                            placeRunInputs.AddConnectorTarget(targetConnector2, line.SPPID.END_X, line.SPPID.END_Y);
508
                        else
509
                        {
510
                            endBranchLine = connItem as Line;
511
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
512
                        }
513
                    }
514
                    else
515
                    {
516
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
517
                    }
518
                }
519
            }
520

    
521
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
522

    
523
            if (_lMConnector != null)
524
            {
525
                foreach (var line in lines)
526
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
527
                _lMConnector.Commit();
528
                if (startBranchLine != null || endBranchLine != null)
529
                {
530
                    BranchLines.Add(new Tuple<string, Line, Line>(_lMConnector.ModelItemID, startBranchLine, endBranchLine));
531
                }
532
            }
533

    
534

    
535
            if (_LMSymbol1 != null)
536
                ReleaseCOMObjects(_LMSymbol1);
537
            if (_LMSymbol2 != null)
538
                ReleaseCOMObjects(_LMSymbol2);
539
            if (targetConnector1 != null)
540
                ReleaseCOMObjects(targetConnector1);
541
            if (targetConnector2 != null)
542
                ReleaseCOMObjects(targetConnector2);
543
            foreach (var item in connectorVertices1)
544
                ReleaseCOMObjects(item.Key);
545
            foreach (var item in connectorVertices2)
546
                ReleaseCOMObjects(item.Key);
547

    
548
            ReleaseCOMObjects(_lMConnector);
549
            ReleaseCOMObjects(placeRunInputs);
550
            ReleaseCOMObjects(_LMAItem);
551
        }
552

    
553
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
554
        {
555
            LMSymbol _LMSymbol = null;
556
            foreach (var connector in symbol.CONNECTORS)
557
            {
558
                if (connector.CONNECTEDITEM == line.UID)
559
                {
560
                    if (connector.Index == 0)
561
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
562
                    else
563
                    {
564
                        ChildSymbol child = null;
565
                        foreach (var childSymbol in symbol.ChildSymbols)
566
                        {
567
                            if (childSymbol.Connectors.Contains(connector))
568
                                child = childSymbol;
569
                            else
570
                                child = GetChildSymbolByConnector(childSymbol, connector);
571

    
572
                            if (child != null)
573
                                break;
574
                        }
575

    
576
                        if (child != null)
577
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
578
                    }
579

    
580
                    break;  
581
                }
582
            }
583

    
584
            return _LMSymbol;
585
        }
586

    
587
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
588
        {
589
            foreach (var childSymbol in item.ChildSymbols)
590
            {
591
                if (childSymbol.Connectors.Contains(connector))
592
                    return childSymbol;
593
                else
594
                    return GetChildSymbolByConnector(childSymbol, connector);
595
            }
596

    
597
            return null;
598
        }
599

    
600
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
601
        {
602
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
603
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
604

    
605
            LMConnector _StartConnector = null;
606
            LMConnector _EndConnector = null;
607
            double lengthStart = double.MaxValue;
608
            double lengthEnd = double.MaxValue;
609
            List<double[]> startPoints = new List<double[]>();
610
            List<double[]> endPoints = new List<double[]>();
611

    
612
            foreach (var item in connectorVertices)
613
            {
614
                foreach (var point in item.Value)
615
                {
616
                    // Start Point가 Branch
617
                    if (branch.Item2 != null)
618
                    {
619
                        Line targetLine = branch.Item2;
620
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
621
                        if (lengthStart > distance)
622
                        {
623
                            _StartConnector = item.Key;
624
                            lengthStart = distance;
625
                            startPoints = item.Value;
626
                        }
627
                    }
628
                    // End Point가 Branch
629
                    if (branch.Item3 != null)
630
                    {
631
                        Line targetLine = branch.Item3;
632
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
633
                        if (lengthEnd > distance)
634
                        {
635
                            _EndConnector = item.Key;
636
                            lengthEnd = distance;
637
                            endPoints = item.Value;
638
                        }
639
                    }
640
                }
641
            }
642
            #region Branch가 양쪽 전부일 때
643
            if (_StartConnector != null && _StartConnector == _EndConnector)
644
            {
645
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
646

    
647
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
648
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
649

    
650
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
651
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
652
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
653
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
654
                   startPoints[startPoints.Count - 1][0],
655
                   startPoints[startPoints.Count - 1][1],
656
                   startPoints[startPoints.Count - 2][0],
657
                   startPoints[startPoints.Count - 2][1]);
658

    
659
                for (int i = 0; i < startPoints.Count; i++)
660
                {
661
                    double[] point = startPoints[i];
662
                    if (i == 0)
663
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
664
                    else if (i == startPoints.Count - 1)
665
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
666
                    else
667
                        placeRunInputs.AddPoint(point[0], point[1]);
668
                }
669

    
670
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
671
                if (_LMConnector != null)
672
                {
673
                    _LMConnector.Commit();
674
                    foreach (var item in lines)
675
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
676
                }
677

    
678
                foreach (var item in startConnectorVertices)
679
                    ReleaseCOMObjects(item.Key);
680
                foreach (var item in endConnectorVertices)
681
                    ReleaseCOMObjects(item.Key);
682
                ReleaseCOMObjects(placeRunInputs);
683
                ReleaseCOMObjects(_LMAItem);
684
                ReleaseCOMObjects(_LMConnector);
685
            }
686
            #endregion
687
            #region 양쪽이 다른 Branch 
688
            else
689
            {
690
                // Branch 시작 Connector
691
                if (_StartConnector != null)
692
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
693

    
694
                // Branch 끝 Connector
695
                if (_EndConnector != null)
696
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
697
            }
698
            #endregion
699

    
700
            if (_StartConnector != null)
701
                ReleaseCOMObjects(_StartConnector);
702
            if (_EndConnector != null)
703
                ReleaseCOMObjects(_EndConnector);
704
            foreach (var item in connectorVertices)
705
                ReleaseCOMObjects(item.Key);
706
        }
707

    
708
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
709
        {
710
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
711
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
712
            LMConnector _SameRunTargetConnector = null;
713
            LMSymbol _SameRunTargetSymbol = null;
714
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
715
            LMConnector _BranchTargetConnector = null;
716
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
717

    
718
            // 같은 Line Run의 Connector 찾기
719
            foreach (var item in connectorVertices)
720
            {
721
                if (item.Key == _Connector)
722
                    continue;
723

    
724
                if (IsStart &&
725
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
726
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
727
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
728
                {
729
                    _SameRunTargetConnector = item.Key;
730
                    break;
731
                }
732
                else if (!IsStart &&
733
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
734
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
735
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
736
                {
737
                    _SameRunTargetConnector = item.Key;
738
                    break;
739
                }
740
            }
741

    
742
            // Branch 반대편이 Symbol
743
            if (_SameRunTargetConnector == null)
744
            {
745
                foreach (var line in lines)
746
                {
747
                    foreach (var connector in line.CONNECTORS)
748
                    {
749
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
750
                        if (symbol != null)
751
                        {
752
                            _SameRunTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
753
                            break;
754
                        }
755
                    }
756
                }
757
            }
758

    
759
            // 기존 Connector 제거
760
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
761
            
762
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
763
            if (IsStart)
764
            {
765
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
766
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
767
            }
768
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
769
            else
770
            {
771
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
772
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
773
                    points[points.Count - 1][0],
774
                    points[points.Count - 1][1],
775
                    points[points.Count - 2][0],
776
                    points[points.Count - 2][1]);
777
            }
778

    
779
            for (int i = 0; i < points.Count; i++)
780
            {
781
                double[] point = points[i];
782
                if (i == 0)
783
                {
784
                    if (IsStart)
785
                    {
786
                        placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
787
                    }
788
                    else
789
                    {
790
                        if (_SameRunTargetConnector != null)
791
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
792
                        else if (_SameRunTargetSymbol != null)
793
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
794
                        else
795
                            placeRunInputs.AddPoint(point[0], point[1]);
796
                    }
797
                }
798
                else if (i == points.Count - 1)
799
                {
800
                    if (IsStart)
801
                    {
802
                        if (_SameRunTargetConnector != null)
803
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
804
                        else if (_SameRunTargetSymbol != null)
805
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
806
                        else
807
                            placeRunInputs.AddPoint(point[0], point[1]);
808
                    }
809
                    else
810
                    {
811
                        if (_BranchTargetConnector != null)
812
                        {
813
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
814
                        }
815
                    }
816
                }
817
                else
818
                    placeRunInputs.AddPoint(point[0], point[1]);
819
            }
820
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
821
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
822

    
823
            if (_LMConnector != null)
824
            {
825
                if (_SameRunTargetConnector != null)
826
                {
827
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
828
                }
829
                else
830
                {
831
                    foreach (var item in lines)
832
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
833
                }
834

    
835
                _LMConnector.Commit();
836
                ReleaseCOMObjects(_LMConnector);
837
            }
838

    
839
            ReleaseCOMObjects(placeRunInputs);
840
            ReleaseCOMObjects(_LMAItem);
841
            if (_BranchTargetConnector != null)
842
                ReleaseCOMObjects(_BranchTargetConnector);
843
            if (_SameRunTargetConnector != null)
844
                ReleaseCOMObjects(_SameRunTargetConnector);
845
            if (_SameRunTargetSymbol != null)
846
                ReleaseCOMObjects(_SameRunTargetSymbol);
847
            foreach (var item in connectorVertices)
848
                ReleaseCOMObjects(item.Key);
849
            foreach (var item in branchConnectorVertices)
850
                ReleaseCOMObjects(item.Key);
851
        }
852

    
853
        private void EndBreakModeling(EndBreak endBreak)
854
        {
855
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
856
            if (ownerObj !=null && ownerObj.GetType() == typeof(Line))
857
            {
858
                Line ownerLine = ownerObj as Line;
859
                LMLabelPersist _LmLabelPersist = null;
860
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(ownerLine.SPPID.ModelItemId);
861

    
862
                LMConnector connectedLMConnector = FindTargetLMConnectorByPoint(connectorVertices, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
863
                if (connectedLMConnector != null)
864
                {
865
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
866
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
867
                    ReleaseCOMObjects(connectedLMConnector);
868
                }
869

    
870
                if (_LmLabelPersist != null)
871
                {
872
                    _LmLabelPersist.Commit();
873
                    ReleaseCOMObjects(_LmLabelPersist);
874
                }
875
                foreach (var item in connectorVertices)
876
                    ReleaseCOMObjects(item.Key);
877
            }
878
            else if (ownerObj != null && ownerObj.GetType() == typeof(Symbol))
879
            {
880
                Symbol ownerSymbol = ownerObj as Symbol;
881
                LMSymbol _LMSymbol = dataSource.GetSymbol(ownerSymbol.SPPID.RepresentationId);
882

    
883
                LMConnector targetLMConnector = null;
884
                double distance = double.MaxValue;
885
                foreach (LMConnector connector in _LMSymbol.Avoid1Connectors)
886
                {
887
                    if (connector.get_ItemStatus() == "Active")
888
                    {
889
                        dynamic OID = connector.get_GraphicOID();
890
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
891
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
892
                        int verticesCount = lineStringGeometry.VertexCount;
893
                        double[] vertices = null;
894
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
895
                        for (int i = 0; i < verticesCount; i++)
896
                        {
897
                            double x = 0;
898
                            double y = 0;
899
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
900

    
901
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, ownerSymbol.SPPID.ORIGINAL_X, ownerSymbol.SPPID.ORIGINAL_Y);
902
                            if (result < distance)
903
                            {
904
                                targetLMConnector = connector;
905
                                distance = result;
906
                            }
907
                        }
908
                    }
909
                }
910

    
911
                foreach (LMConnector connector in _LMSymbol.Avoid2Connectors)
912
                {
913
                    dynamic OID = connector.get_GraphicOID();
914
                    DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
915
                    Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
916
                    int verticesCount = lineStringGeometry.VertexCount;
917
                    double[] vertices = null;
918
                    lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
919
                    for (int i = 0; i < verticesCount; i++)
920
                    {
921
                        double x = 0;
922
                        double y = 0;
923
                        lineStringGeometry.GetVertex(i + 1, ref x, ref y);
924

    
925
                        double result = SPPIDUtil.CalcPointToPointdDistance(x, y, ownerSymbol.SPPID.ORIGINAL_X, ownerSymbol.SPPID.ORIGINAL_Y);
926
                        if (result < distance)
927
                        {
928
                            targetLMConnector = connector;
929
                            distance = result;
930
                        }
931
                    }
932
                }
933

    
934
                if (targetLMConnector != null)
935
                {
936
                    LMLabelPersist _LmLabelPersist = null;
937
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
938
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
939
                    if (_LmLabelPersist != null)
940
                    {
941
                        _LmLabelPersist.Commit();
942
                        ReleaseCOMObjects(_LmLabelPersist);
943
                    }
944
                }
945
                ReleaseCOMObjects(_LMSymbol);
946
            }
947
        }
948

    
949
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
950
        {
951
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
952
            _LMAItem item1 = modelItem1.AsLMAItem();
953
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
954
            _LMAItem item2 = modelItem2.AsLMAItem();
955
            
956
            // item2가 item1으로 조인
957
            try
958
            {
959
                _placement.PIDJoinRuns(ref item1, ref item2);
960
                item1.Commit();
961
                item2.Commit();
962
            }
963
            catch (Exception ex)
964
            {
965
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
966
            }
967
            finally
968
            {
969
                ReleaseCOMObjects(modelItem1);
970
                ReleaseCOMObjects(item1);
971
                ReleaseCOMObjects(modelItem2);
972
                ReleaseCOMObjects(item2);
973
            }
974
        }
975

    
976
        private void AutoJoinPipeRun(string modelItemId)
977
        {
978
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
979
            _LMAItem item = modelItem.AsLMAItem();
980
            try
981
            {
982
                string modelitemID = item.Id;
983
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
984
                string afterModelItemID = item.Id;
985
                if (modelitemID != afterModelItemID)
986
                {
987
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
988
                    foreach (var line in lines)
989
                        line.SPPID.ModelItemId = afterModelItemID;
990
                }
991
                item.Commit();
992
            }
993
            catch (Exception ex)
994
            {
995
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
996
            }
997
            finally
998
            {
999
                ReleaseCOMObjects(modelItem);
1000
                ReleaseCOMObjects(item);
1001
            }
1002
        }
1003

    
1004
        private void JoinRunLine(LineRun run)
1005
        {
1006
            string modelItemId = string.Empty;
1007
            foreach (var item in run.RUNITEMS)
1008
            {
1009
                if (item.GetType() == typeof(Line))
1010
                {
1011
                    Line line = item as Line;
1012
                    if (modelItemId != line.SPPID.ModelItemId)
1013
                    {
1014
                        AutoJoinPipeRun(line.SPPID.ModelItemId);
1015
                        modelItemId = line.SPPID.ModelItemId;
1016
                    }
1017
                }
1018
            }
1019
        }
1020

    
1021
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
1022
        {
1023
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
1024
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
1025

    
1026
            if (modelItem != null)
1027
            {
1028
                foreach (LMRepresentation rep in modelItem.Representations)
1029
                {
1030
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1031
                    {
1032
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1033
                        connectorVertices.Add(_LMConnector, new List<double[]>());
1034
                        dynamic OID = rep.get_GraphicOID();
1035
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1036
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1037
                        int verticesCount = lineStringGeometry.VertexCount;
1038
                        double[] vertices = null;
1039
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1040
                        for (int i = 0; i < verticesCount; i++)
1041
                        {
1042
                            double x = 0;
1043
                            double y = 0;
1044
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1045
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
1046
                        }
1047
                    }
1048
                }
1049

    
1050
                ReleaseCOMObjects(modelItem);
1051
            }
1052

    
1053
            return connectorVertices;
1054
        }
1055

    
1056
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
1057
        {
1058
            double length = double.MaxValue;
1059
            LMConnector targetConnector = null;
1060
            foreach (var item in connectorVertices)
1061
            {
1062
                List<double[]> points = item.Value;
1063
                for (int i = 0; i < points.Count - 1; i++)
1064
                {
1065
                    double[] point1 = points[i];
1066
                    double[] point2 = points[i + 1];
1067

    
1068
                    double maxLineX = Math.Max(point1[0], point2[0]);
1069
                    double minLineX = Math.Min(point1[0], point2[0]);
1070
                    double maxLineY = Math.Max(point1[1], point2[1]);
1071
                    double minLineY = Math.Min(point1[1], point2[1]);
1072

    
1073
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
1074

    
1075
                    // 두직선의 교차점
1076
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
1077
                    if (crossingPoint != null)
1078
                    {
1079
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
1080
                        if (length >= distance)
1081
                        {
1082
                            if (slope == SlopeType.Slope &&
1083
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
1084
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1085
                            {
1086
                                targetConnector = item.Key;
1087
                                length = distance;
1088
                            }
1089
                            else if (slope == SlopeType.HORIZONTAL &&
1090
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
1091
                            {
1092
                                targetConnector = item.Key;
1093
                                length = distance;
1094
                            }
1095
                            else if (slope == SlopeType.VERTICAL &&
1096
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1097
                            {
1098
                                targetConnector = item.Key;
1099
                                length = distance;
1100
                            }
1101
                        }
1102
                    }
1103
                }
1104
            }
1105

    
1106
            return targetConnector;
1107
        }
1108

    
1109
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1110
        {
1111
            double length = double.MaxValue;
1112
            LMConnector targetConnector = null;
1113
            foreach (var item in connectorVertices)
1114
            {
1115
                List<double[]> points = item.Value;
1116

    
1117
                foreach (double[] point in points)
1118
                {
1119
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1120
                    if (length >= distance)
1121
                    {
1122
                        targetConnector = item.Key;
1123
                        length = distance;
1124
                    }
1125
                }
1126
            }
1127

    
1128
            return targetConnector;
1129
        }
1130

    
1131
        private void LineNumberModeling(LineNumber lineNumber)
1132
        {
1133
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
1134
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
1135
            LMConnector connectedLMConnector = FindTargetLMConnectorByPoint(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
1136
            if (connectedLMConnector != null)
1137
            {
1138
                double x = 0;
1139
                double y = 0;
1140
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
1141

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

    
1146
                foreach (var item in connectorVertices)
1147
                    ReleaseCOMObjects(item.Key);
1148
                if (_LmLabelPresist != null)
1149
                {
1150
                    lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
1151
                    ReleaseCOMObjects(_LmLabelPresist);
1152
                }
1153
            }
1154
        }
1155

    
1156
        private void InputLineNumberAttribute(LineNumber lineNumber)
1157
        {
1158
            foreach (var run in lineNumber.RUNS)
1159
            {
1160
                foreach (var item in run.RUNITEMS)
1161
                {
1162
                    if (item.GetType() == typeof(Line))
1163
                    {
1164
                        Line line = item as Line;
1165
                        LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1166
                        if (_LMModelItem.get_ItemStatus() == "Active")
1167
                        {
1168
                            foreach (var attribute in lineNumber.ATTRIBUTES)
1169
                            {
1170
                                LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1171
                                if (mapping != null)
1172
                                {
1173
                                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1174
                                    if (_LMAAttribute != null)
1175
                                    {
1176
                                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1177
                                            _LMAAttribute.set_Value(attribute.VALUE);
1178
                                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
1179
                                            _LMAAttribute.set_Value(attribute.VALUE);
1180
                                    }
1181
                                }
1182
                            }
1183
                            _LMModelItem.Commit();
1184
                            break;
1185
                        }
1186
                        ReleaseCOMObjects(_LMModelItem);
1187
                    }
1188
                }
1189
            }
1190
        }
1191

    
1192
        private void InputSymbolAttribute(Symbol symbol)
1193
        {
1194
            try
1195
            {
1196
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1197
                {
1198
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1199
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1200
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
1201

    
1202
                    foreach (var item in symbol.PROPERTIES)
1203
                    {
1204

    
1205

    
1206
                    }
1207

    
1208
                    foreach (var item in symbol.ATTRIBUTES)
1209
                    {
1210
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
1211
                        if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
1212
                        {
1213
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
1214
                            if (_Attribute != null)
1215
                            {
1216
                                // 임시
1217
                                if (item.ATTRIBUTETYPE == "String")
1218
                                {
1219
                                    if (!string.IsNullOrEmpty(item.VALUE))
1220
                                    {
1221
                                        if (!DBNull.Value.Equals(_Attribute.get_Value()) && !string.IsNullOrEmpty(_Attribute.get_Value()))
1222
                                        {
1223
                                            string value = _Attribute.get_Value() + "\n" + item.VALUE;
1224
                                            _Attribute.set_Value(value);
1225
                                        }
1226
                                        else
1227
                                        {
1228
                                            _Attribute.set_Value(item.VALUE);
1229
                                        }
1230
                                    }
1231
                                }
1232
                                else
1233
                                {
1234
                                    _Attribute.set_Value(item.VALUE);
1235
                                }
1236
                            }
1237
                        }
1238
                    }
1239

    
1240
                    foreach (var item in symbol.ASSOCIATIONS)
1241
                    {
1242

    
1243
                    }
1244

    
1245
                    ReleaseCOMObjects(_LMSymbol);
1246
                    ReleaseCOMObjects(_Attributes);
1247
                    ReleaseCOMObjects(_LMModelItem);
1248
                }
1249
            }
1250
            catch (Exception ex)
1251
            {
1252
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1253
            }
1254
        }
1255

    
1256
        private void TextModeling(Text text)
1257
        {
1258
            LMSymbol _LMSymbol = null;
1259
            try
1260
            {
1261
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
1262
                if (text.ASSOCIATION)
1263
                {
1264
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
1265
                    if (owner.GetType() == typeof(Symbol))
1266
                    {
1267
                        Symbol symbol = owner as Symbol;
1268
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1269
                        if (_LMSymbol != null)
1270
                        {
1271
                            Association association = symbol.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
1272
                            List<BaseModel.Attribute> attributes = symbol.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
1273
                            AttributeMapping mapping = null;
1274
                            foreach (var attribute in attributes)
1275
                            {
1276
                                if (string.IsNullOrEmpty(attribute.VALUE) || attribute.VALUE == "None")
1277
                                    continue;
1278

    
1279
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
1280
                                if (mapping != null)
1281
                                    break;  
1282
                            }
1283

    
1284
                            if (mapping != null)
1285
                            {
1286
                                double x = 0;
1287
                                double y = 0;
1288

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

    
1292
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: true);
1293
                                if (_LMLabelPersist!=null)
1294
                                {
1295
                                    _LMLabelPersist.Commit();
1296
                                    ReleaseCOMObjects(_LMLabelPersist);
1297
                                }
1298
                            }
1299
                        }
1300
                    }
1301
                    else if (owner.GetType() == typeof(Line))
1302
                    {
1303

    
1304
                    }
1305
                }
1306
                else
1307
                {
1308
                    LMItemNote _LMItemNote = null;
1309
                    LMAAttribute _LMAAttribute = null;
1310

    
1311
                    double x = 0;
1312
                    double y = 0;
1313

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

    
1316
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y);
1317
                    _LMSymbol.Commit();
1318
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1319
                    _LMItemNote.Commit();
1320
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1321
                    _LMAAttribute.set_Value(text.VALUE);
1322
                    _LMItemNote.Commit();
1323

    
1324
                    if (_LMAAttribute != null)
1325
                        ReleaseCOMObjects(_LMAAttribute);
1326
                    if (_LMItemNote != null)
1327
                        ReleaseCOMObjects(_LMItemNote);
1328
                }
1329
            }
1330
            catch (Exception ex)
1331
            {
1332

    
1333
            }
1334
            finally
1335
            {
1336
                if (_LMSymbol != null)
1337
                    ReleaseCOMObjects(_LMSymbol);
1338
            }
1339
        }
1340

    
1341
        private void NoteModeling(Note note)
1342
        {
1343
            LMSymbol _LMSymbol = null;
1344
            LMItemNote _LMItemNote = null;
1345
            LMAAttribute _LMAAttribute = null;
1346

    
1347
            try
1348
            {
1349
                double x = 0;
1350
                double y = 0;
1351

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

    
1354
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
1355
                _LMSymbol.Commit();
1356
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1357
                _LMItemNote.Commit();
1358
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1359
                _LMAAttribute.set_Value(note.VALUE);
1360
                _LMItemNote.Commit();
1361
            }
1362
            catch (Exception ex)
1363
            {
1364

    
1365
            }
1366
            finally
1367
            {
1368
                if (_LMAAttribute != null)
1369
                    ReleaseCOMObjects(_LMAAttribute);
1370
                if (_LMItemNote != null)
1371
                    ReleaseCOMObjects(_LMItemNote);
1372
                if (_LMSymbol != null)
1373
                    ReleaseCOMObjects(_LMSymbol);
1374
            }
1375
            
1376
        }
1377

    
1378
        public void ReleaseCOMObjects(params object[] objVars)
1379
        {
1380
            int intNewRefCount = 0;
1381
            foreach (object obj in objVars)
1382
            {
1383
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
1384
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
1385
            }
1386
        }
1387

    
1388
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
1389
        {
1390
            if (location == Location.None)
1391
            {
1392
                x = originX;
1393
                y = originY;
1394
            }
1395
            else
1396
            {
1397
                if (location.HasFlag(Location.Center))
1398
                {
1399
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
1400
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
1401
                }
1402

    
1403
                if (location.HasFlag(Location.Left))
1404
                    x = SPPIDLabelLocation.X1;
1405
                else if (location.HasFlag(Location.Right))
1406
                    x = SPPIDLabelLocation.X2;
1407

    
1408
                if (location.HasFlag(Location.Down))
1409
                    y = SPPIDLabelLocation.Y1;
1410
                else if (location.HasFlag(Location.Up))
1411
                    y = SPPIDLabelLocation.Y2;
1412
            }
1413
        }
1414
    }
1415
}
클립보드 이미지 추가 (최대 크기: 500 MB)