프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 4abf114c

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

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

    
24
using DevExpress.XtraSplashScreen;
25
namespace Converter.SPPID
26
{
27
    public class AutoModeling
28
    {
29
        Placement _placement;
30
        LMADataSource dataSource;
31
        dynamic newDrawing;
32
        dynamic application;
33
        Ingr.RAD2D.Application radApp;
34
        SPPID_Document document;
35
        ETCSetting _ETCSetting;
36

    
37
        int EquipCount;
38
        int SymbolCount;
39
        int LineCount;
40
        int NoteCount;
41
        int TextCount;
42
        int EndBreakCount;
43
        int LineNumberCount;
44

    
45
        int CurrentCount;
46
        int AllCount;
47

    
48
        List <Tuple<string, Line, Line>> BranchLines = new List<Tuple<string, Line, Line>>();
49
        public AutoModeling(SPPID_Document document, dynamic application, Ingr.RAD2D.Application radApp)
50
        {
51
            this.document = document;
52
            this.application = application;
53
            this.radApp = radApp;
54
            this._ETCSetting = ETCSetting.GetInstance();
55
        }
56

    
57
        private void ClacProgressCount()
58
        {
59
            EquipCount = document.Equipments.Count;
60
            SymbolCount = document.SYMBOLS.Count;
61
            SymbolCount = SymbolCount * 3;
62
            
63
            foreach (LineNumber lineNumber in document.LINENUMBERS)
64
                foreach (LineRun run in lineNumber.RUNS)
65
                    foreach (var item in run.RUNITEMS)
66
                        if (item.GetType() == typeof(Line))
67
                            LineCount++;
68
            foreach (TrimLine trimLine in document.TRIMLINES)
69
                foreach (LineRun run in trimLine.RUNS)
70
                    foreach (var item in run.RUNITEMS)
71
                        if (item.GetType() == typeof(Line))
72
                            LineCount++;
73

    
74
            LineCount = LineCount * 2;
75
            NoteCount = document.NOTES.Count;
76
            TextCount = document.TEXTINFOS.Count;
77
            EndBreakCount = document.EndBreaks.Count;
78
            LineNumberCount = document.LINENUMBERS.Count;
79
            LineNumberCount = LineNumberCount * 2;
80

    
81
            AllCount = EquipCount + SymbolCount + LineCount + NoteCount + TextCount + EndBreakCount;
82
        }
83

    
84
        public void Run()
85
        {
86
            try
87
            {
88
                _placement = new Placement();
89
                dataSource = _placement.PIDDataSource;
90

    
91
                newDrawing = application.Drawings.Add(document.Unit, document.Template, document.DrawingNumber, document.DrawingName);
92
                application.ActiveWindow.Fit();
93
                Thread.Sleep(500);
94
                application.ActiveWindow.Zoom = 2000;
95
                Thread.Sleep(1000);
96

    
97
                double maxX = 0;
98
                double maxY = 0;
99
                foreach (object drawingObj in radApp.ActiveDocument.ActiveSheet.DrawingObjects)
100
                {
101
                    Ingr.RAD2D.SmartFrame2d smartFrame2d = drawingObj as Ingr.RAD2D.SmartFrame2d;
102
                    if (smartFrame2d != null)
103
                    {
104
                        double x1 = 0;
105
                        double x2 = 0;
106
                        double y1 = 0;
107
                        double y2 = 0;
108
                        smartFrame2d.Range(out x1, out y1, out x2, out y2);
109
                        maxX = Math.Max(x2, maxX);
110
                        maxY = Math.Max(y2, maxY);
111
                    }
112
                }
113
                if (maxX != 0 && maxY != 0)
114
                {
115
                    document.SetSPPIDLocation(maxX, maxY);
116
                    ClacProgressCount();
117

    
118
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
119
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetParent, (IntPtr)radApp.HWnd);
120
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllStep, AllCount);
121

    
122
                    // Equipment Modeling
123
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
124
                    foreach (Equipment equipment in document.Equipments)
125
                        EquipmentModeling(equipment);
126

    
127
                    // LineRun Symbol Modeling
128
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbols Modeling");
129
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
130
                        foreach (LineRun run in lineNumber.RUNS)
131
                            SymbolModelingByRun(run);
132
                    // TrimLineRun Symbol Modeling
133
                    foreach (TrimLine trimLine in document.TRIMLINES)
134
                        foreach (LineRun run in trimLine.RUNS)
135
                            SymbolModelingByRun(run);
136

    
137
                    // LineRun Line Modeling
138
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Lines Modeling");
139
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
140
                        foreach (LineRun run in lineNumber.RUNS)
141
                            LineModelingByRun(run);
142
                    // TrimLineRun Line Modeling
143
                    foreach (TrimLine trimLine in document.TRIMLINES)
144
                        foreach (LineRun run in trimLine.RUNS)
145
                            LineModelingByRun(run);
146

    
147
                    // Branch Line Modeling
148
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Branch Lines Modeling");
149
                    foreach (var item in BranchLines)
150
                        BranchLineModeling(item);
151

    
152
                    // EndBreak Modeling
153
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
154
                    foreach (var item in document.EndBreaks)
155
                        EndBreakModeling(item);
156

    
157
                    // LineNumber Modeling
158
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "LineNumbers Modeling");
159
                    foreach (var item in document.LINENUMBERS)
160
                        LineNumberModeling(item);
161

    
162
                    // Note Modeling
163
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
164
                    foreach (var item in document.NOTES)
165
                        NoteModeling(item);
166

    
167
                    // Text Modeling
168
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
169
                    foreach (var item in document.TEXTINFOS)
170
                        TextModeling(item);
171

    
172
                    // LineRun Line Join
173
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "LineRuns Join");
174
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
175
                        foreach (LineRun run in lineNumber.RUNS)
176
                            JoinRunLine(run);
177
                    // TrimLineRun Line Join
178
                    foreach (TrimLine trimLine in document.TRIMLINES)
179
                        foreach (LineRun run in trimLine.RUNS)
180
                            JoinRunLine(run);
181

    
182
                    // Input LineNumber Attribute
183
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Lines Attribute");
184
                    foreach (var item in document.LINENUMBERS)
185
                        InputLineNumberAttribute(item);
186

    
187
                    // Input Symbol Attribute
188
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
189
                    foreach (var item in document.SYMBOLS)
190
                        InputSymbolAttribute(item);
191

    
192
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
193
                    #region 임시 Label
194
                    foreach (var item in document.SYMBOLS)
195
                    {
196
                        if (item.SPPID.MAPPINGNAME.Contains("Labels - "))
197
                        {
198
                            SPPIDSymbolInfo info = item.SPPID;
199
                            Array points = new double[] { 0, item.SPPID.ORIGINAL_X, item.SPPID.ORIGINAL_Y };
200
                            BaseModel.Attribute itemAttribute = item.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
201
                            if (itemAttribute == null)
202
                                continue;
203
                            string symbolUID = item.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL").VALUE;
204
                            object objectItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
205
                            if (objectItem != null)
206
                            {
207

    
208
                                string sRep = null;
209
                                if (objectItem.GetType() == typeof(Symbol))
210
                                    sRep = ((Symbol)objectItem).SPPID.RepresentationId;
211
                                else if (objectItem.GetType() == typeof(Equipment))
212
                                    sRep = ((Equipment)objectItem).SPPID.RepresentationId;
213

    
214
                                if (!string.IsNullOrEmpty(sRep))
215
                                {
216
                                    LMSymbol _TargetItem = dataSource.GetSymbol(sRep);
217
                                    LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(item.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: _TargetItem.AsLMRepresentation(), IsLeaderVisible: true);
218
                                    LMModelItem _LMModelItem = _TargetItem.ModelItemObject;
219
                                    LMAAttributes _Attributes = _LMModelItem.Attributes;
220

    
221
                                    foreach (var attribute in item.ATTRIBUTES)
222
                                    {
223
                                        if (!string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
224
                                        {
225
                                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
226
                                            if (mapping != null)
227
                                            {
228
                                                LMAAttribute _LMAAttribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
229
                                                if (mapping.SPPIDATTRIBUTENAME.Contains("Description"))
230
                                                {
231
                                                    if (!DBNull.Value.Equals(_LMAAttribute.get_Value()) && !string.IsNullOrEmpty(_LMAAttribute.get_Value()))
232
                                                    {
233
                                                        string value = _LMAAttribute.get_Value() + "\n" + attribute.VALUE;
234
                                                        _LMAAttribute.set_Value(value);
235
                                                    }
236
                                                    else
237
                                                    {
238
                                                        _LMAAttribute.set_Value(attribute.VALUE);
239
                                                    }
240
                                                }
241
                                                else if (_LMAAttribute != null)
242
                                                    _LMAAttribute.set_Value(attribute.VALUE);
243
                                            }
244
                                        }
245
                                    }
246

    
247
                                    string OID = _LMLabelPresist.get_GraphicOID();
248
                                    DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
249
                                    if (dependency != null)
250
                                    {
251
                                        bool result = false;
252
                                        foreach (var attributes in dependency.AttributeSets)
253
                                        {
254
                                            foreach (var attribute in attributes)
255
                                            {
256
                                                string name = attribute.Name;
257
                                                string value = attribute.GetValue().ToString();
258
                                                if (name == "DrawingItemType" && value == "LabelPersist")
259
                                                {
260
                                                    foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
261
                                                    {
262
                                                        if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
263
                                                        {
264
                                                            Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
265
                                                            double prevX = _TargetItem.get_XCoordinate();
266
                                                            double prevY = _TargetItem.get_YCoordinate();
267
                                                            lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
268
                                                            lineString2D.RemoveVertex(lineString2D.VertexCount);
269
                                                            result = true;
270
                                                            break;
271
                                                        }
272
                                                    }
273
                                                }
274

    
275
                                                if (result)
276
                                                    break;
277
                                            }
278

    
279
                                            if (result)
280
                                                break;
281
                                        }
282
                                    }
283

    
284

    
285
                                    _LMModelItem.Commit();
286
                                    _LMLabelPresist.Commit();
287
                                    ReleaseCOMObjects(_TargetItem);
288
                                    ReleaseCOMObjects(_LMLabelPresist);
289
                                    ReleaseCOMObjects(_LMModelItem);
290
                                    ReleaseCOMObjects(_Attributes);
291
                                }
292
                            }
293
                        }
294

    
295
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
296
                    }
297
                    #endregion
298

    
299

    
300
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, AllCount);
301
                }
302
            }
303
            catch (Exception ex)
304
            {
305
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
306
            }
307
            finally
308
            {
309
                application.ActiveWindow.Fit();
310
                
311
                if (newDrawing != null)
312
                {
313
                    radApp.ActiveDocument.SaveOnClose = false;
314
                    radApp.ActiveDocument.Save();
315
                    ReleaseCOMObjects(newDrawing);
316
                }
317

    
318
                ReleaseCOMObjects(dataSource);
319
                ReleaseCOMObjects(_placement);
320

    
321
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
322
                SplashScreenManager.CloseForm(false);
323
            }
324
        }
325

    
326
        private void LineModelingByRun(LineRun run)
327
        {
328
            Line prevLine = null;
329
            List<Line> lines = new List<Line>();
330
            foreach (var item in run.RUNITEMS)
331
            {
332
                // Line일 경우
333
                if (item.GetType() == typeof(Line))
334
                {
335
                    Line line = item as Line;
336
                    if (prevLine == null)
337
                        lines.Add(line);
338
                    else if (prevLine != null)
339
                    {
340
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
341
                            lines.Add(line);
342
                        else
343
                        {
344
                            if (lines.Count > 0)
345
                            {
346
                                LineModeling(lines);
347
                                lines.Clear();
348
                            }
349
                            lines.Add(line);
350
                        }
351
                    }
352

    
353
                    prevLine = line;
354

    
355
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
356
                }
357
                // Symbol 일 경우
358
                else if (item.GetType() == typeof(Symbol))
359
                {
360
                    if (lines.Count > 0)
361
                    {
362
                        LineModeling(lines);
363
                        lines.Clear();
364
                    }
365
                }
366
            }
367

    
368
            if (lines.Count > 0)
369
                LineModeling(lines);
370
        }
371

    
372
        private void SymbolModelingByRun(LineRun run)
373
        {
374
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
375
            if (run.RUNITEMS.Count > 0)
376
            {
377
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
378
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
379

    
380
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
381
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
382
            }
383

    
384
            Symbol prevSymbol = null;
385
            Symbol targetSymbol = null;
386
            foreach (var item in run.RUNITEMS)
387
            {
388
                if (item.GetType() == typeof(Symbol))
389
                {
390
                    Symbol symbol = item as Symbol;
391
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
392
                    prevSymbol = symbol;
393
                    targetSymbol = symbol;
394
                }
395
                else
396
                {
397
                    targetSymbol = null;
398
                }
399
            }
400
        }
401

    
402
        private void SymbolModelingByRunStart(Symbol symbol, LineRun run)
403
        {
404
            foreach (var connector in symbol.CONNECTORS)
405
            {
406
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
407
                if (targetItem != null &&
408
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
409
                    !IsSameLineNumber(symbol, targetItem))
410
                {
411
                    SymbolModeling(symbol, targetItem as Symbol, null);
412
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
413
                    {
414
                        object item = run.RUNITEMS[i];
415
                        if (item.GetType() == typeof(Symbol))
416
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol, null);
417
                        else
418
                            break;
419
                    }
420
                    break;
421
                }
422
            }
423

    
424

    
425
        }
426

    
427
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
428
        {
429
            foreach (var connector in symbol.CONNECTORS)
430
            {
431
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
432
                if (targetItem != null &&
433
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
434
                    !IsSameLineNumber(symbol, targetItem))
435
                {
436
                    SymbolModeling(symbol, targetItem as Symbol, null);
437
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
438
                    {
439
                        object item = run.RUNITEMS[i];
440
                        if (item.GetType() == typeof(Symbol))
441
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol, null);
442
                        else
443
                            break;
444
                    }
445
                    break;
446
                }
447
            }
448
        }
449

    
450
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
451
        {
452
            // 임시
453
            if (symbol.SPPID.MAPPINGNAME.Contains("Labels - "))
454
                return;
455

    
456
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
457
                return;
458

    
459
            LMSymbol _LMSymbol = null;
460

    
461
            string mappingPath = symbol.SPPID.MAPPINGNAME;
462
            double x = symbol.SPPID.ORIGINAL_X;
463
            double y = symbol.SPPID.ORIGINAL_Y;
464
            int mirror = 0;
465
            double angle = symbol.ANGLE;
466
            // 임시
467
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
468
            {
469
                mirror = 1;
470
            }
471
                
472

    
473
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
474
            {
475
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
476

    
477
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
478
                double x1 = 0;
479
                double x2 = 0;
480
                double y1 = 0;
481
                double y2 = 0;
482
                symbol2d.Range(out x1, out y1, out x2, out y2);
483

    
484
                if (y2 < y)
485
                    y = y2;
486
                else if (y1 > y)
487
                    y = y1;
488

    
489
                if (x2 < x)
490
                    x = x2;
491
                else if (x1 > x)
492
                    x = x1;
493

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

    
514

    
515
            if (_LMSymbol != null)
516
            {
517
                _LMSymbol.Commit();
518
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
519

    
520
                foreach (var item in symbol.ChildSymbols)
521
                    CreateChildSymbol(item, _LMSymbol);
522
            }
523

    
524
            ReleaseCOMObjects(_LMSymbol);
525
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
526
        }
527

    
528
        private void EquipmentModeling(Equipment equipment)
529
        {
530
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
531
                return;
532

    
533
            LMSymbol _LMSymbol = null;
534
            LMSymbol targetItem = null;
535
            string mappingPath = equipment.SPPID.MAPPINGNAME;
536
            double x = equipment.SPPID.ORIGINAL_X;
537
            double y = equipment.SPPID.ORIGINAL_Y;
538
            int mirror = 0;
539
            double angle = equipment.ANGLE;
540

    
541
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
542
            if (connector != null)
543
            {
544
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
545
                if (connEquipment != null)
546
                {
547
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
548
                        EquipmentModeling(connEquipment);
549

    
550
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
551
                    {
552
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
553
                        if (targetItem != null)
554
                        {
555
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
556
                        }
557
                        else
558
                        {
559
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
560
                        }
561
                    }
562
                    else
563
                    {
564
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
565
                    }
566
                }
567
                else
568
                {
569
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
570
                }
571
            }
572
            else
573
            {
574
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
575
            }
576

    
577
            if (_LMSymbol != null)
578
            {
579
                _LMSymbol.Commit();
580
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
581
                ReleaseCOMObjects(_LMSymbol);
582
            }
583

    
584
            if (targetItem != null)
585
            {
586
                ReleaseCOMObjects(targetItem);
587
            }
588
            
589
            ReleaseCOMObjects(_LMSymbol);
590

    
591
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
592
        }
593

    
594
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
595
        {
596
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
597
            double x1 = 0;
598
            double x2 = 0;
599
            double y1 = 0;
600
            double y2 = 0;
601
            symbol2d.Range(out x1, out y1, out x2, out y2);
602

    
603
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
604
            if (_LMSymbol != null)
605
            {
606
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
607
                foreach (var item in childSymbol.ChildSymbols)
608
                    CreateChildSymbol(item, _LMSymbol);
609
            }
610
            
611

    
612
            ReleaseCOMObjects(_LMSymbol);
613
        }
614

    
615
        private bool IsSameLineNumber(object item, object targetItem)
616
        {
617
            foreach (var lineNumber in document.LINENUMBERS)
618
            {
619
                foreach (var run in lineNumber.RUNS)
620
                {
621
                    foreach (var runItem in run.RUNITEMS)
622
                    {
623
                        if (runItem == item)
624
                        {
625
                            foreach (var findItem in run.RUNITEMS)
626
                            {
627
                                if (findItem == targetItem)
628
                                {
629
                                    return true;
630
                                }
631
                            }
632

    
633
                            return false;
634

    
635
                        }
636
                    }
637
                }
638
            }
639

    
640
            return false;
641
        }
642

    
643
        private void LineModeling(List<Line> lines)
644
        {
645
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
646
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
647
            LMSymbol _LMSymbol1 = null;
648
            LMSymbol _LMSymbol2 = null;
649
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
650
            LMConnector targetConnector1 = null;
651
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
652
            LMConnector targetConnector2 = null;
653

    
654
            Line startBranchLine = null;
655
            Line endBranchLine = null;
656

    
657
            for (int i = 0; i < lines.Count; i++)
658
            {
659
                Line line = lines[i];
660
                if (i == 0 || i + 1 != lines.Count)
661
                {
662
                    // 시작점에 연결된 Symbol 찾기
663
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
664
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
665
                    {
666
                        _LMSymbol1 = GetTargetSymbol(connItem as Symbol, line);
667
                        if (_LMSymbol1 != null)
668
                            placeRunInputs.AddSymbolTarget(_LMSymbol1, line.SPPID.START_X, line.SPPID.START_Y);
669
                        else
670
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
671
                    }
672
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
673
                    {
674
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
675
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
676

    
677
                        if (targetConnector1 != null)
678
                            placeRunInputs.AddConnectorTarget(targetConnector1, line.SPPID.START_X, line.SPPID.START_Y);
679
                        else
680
                        {
681
                            startBranchLine = connItem as Line;
682
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
683
                        }
684
                    }
685
                    else
686
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
687
                }
688

    
689
                if (i + 1 == lines.Count)
690
                {
691
                    // 끝점에 연결된 Symbol 찾기
692
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
693

    
694
                    if (i != 0)
695
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
696

    
697
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
698
                    {
699
                        _LMSymbol2 = GetTargetSymbol(connItem as Symbol, line);
700
                        if (_LMSymbol2 != null)
701
                            placeRunInputs.AddSymbolTarget(_LMSymbol2, line.SPPID.END_X, line.SPPID.END_Y);
702
                        else
703
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
704
                            
705
                    }
706
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
707
                    {
708
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
709
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
710

    
711
                        if (targetConnector2 != null)
712
                            placeRunInputs.AddConnectorTarget(targetConnector2, line.SPPID.END_X, line.SPPID.END_Y);
713
                        else
714
                        {
715
                            endBranchLine = connItem as Line;
716
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
717
                        }
718
                    }
719
                    else
720
                    {
721
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
722
                    }
723
                }
724
            }
725

    
726
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
727

    
728
            if (_lMConnector != null)
729
            {
730
                foreach (var line in lines)
731
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
732
                _lMConnector.Commit();
733
                if (startBranchLine != null || endBranchLine != null)
734
                {
735
                    BranchLines.Add(new Tuple<string, Line, Line>(_lMConnector.ModelItemID, startBranchLine, endBranchLine));
736
                }
737
            }
738

    
739

    
740
            if (_LMSymbol1 != null)
741
                ReleaseCOMObjects(_LMSymbol1);
742
            if (_LMSymbol2 != null)
743
                ReleaseCOMObjects(_LMSymbol2);
744
            if (targetConnector1 != null)
745
                ReleaseCOMObjects(targetConnector1);
746
            if (targetConnector2 != null)
747
                ReleaseCOMObjects(targetConnector2);
748
            foreach (var item in connectorVertices1)
749
                ReleaseCOMObjects(item.Key);
750
            foreach (var item in connectorVertices2)
751
                ReleaseCOMObjects(item.Key);
752

    
753
            ReleaseCOMObjects(_lMConnector);
754
            ReleaseCOMObjects(placeRunInputs);
755
            ReleaseCOMObjects(_LMAItem);
756
        }
757

    
758
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
759
        {
760
            LMSymbol _LMSymbol = null;
761
            foreach (var connector in symbol.CONNECTORS)
762
            {
763
                if (connector.CONNECTEDITEM == line.UID)
764
                {
765
                    if (connector.Index == 0)
766
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
767
                    else
768
                    {
769
                        ChildSymbol child = null;
770
                        foreach (var childSymbol in symbol.ChildSymbols)
771
                        {
772
                            if (childSymbol.Connectors.Contains(connector))
773
                                child = childSymbol;
774
                            else
775
                                child = GetChildSymbolByConnector(childSymbol, connector);
776

    
777
                            if (child != null)
778
                                break;
779
                        }
780

    
781
                        if (child != null)
782
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
783
                    }
784

    
785
                    break;  
786
                }
787
            }
788

    
789
            return _LMSymbol;
790
        }
791

    
792
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
793
        {
794
            foreach (var childSymbol in item.ChildSymbols)
795
            {
796
                if (childSymbol.Connectors.Contains(connector))
797
                    return childSymbol;
798
                else
799
                    return GetChildSymbolByConnector(childSymbol, connector);
800
            }
801

    
802
            return null;
803
        }
804

    
805
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
806
        {
807
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
808
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
809

    
810
            LMConnector _StartConnector = null;
811
            LMConnector _EndConnector = null;
812
            double lengthStart = double.MaxValue;
813
            double lengthEnd = double.MaxValue;
814
            List<double[]> startPoints = new List<double[]>();
815
            List<double[]> endPoints = new List<double[]>();
816

    
817
            foreach (var item in connectorVertices)
818
            {
819
                foreach (var point in item.Value)
820
                {
821
                    // Start Point가 Branch
822
                    if (branch.Item2 != null)
823
                    {
824
                        Line targetLine = branch.Item2;
825
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
826
                        if (lengthStart > distance)
827
                        {
828
                            _StartConnector = item.Key;
829
                            lengthStart = distance;
830
                            startPoints = item.Value;
831
                        }
832
                    }
833
                    // End Point가 Branch
834
                    if (branch.Item3 != null)
835
                    {
836
                        Line targetLine = branch.Item3;
837
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
838
                        if (lengthEnd > distance)
839
                        {
840
                            _EndConnector = item.Key;
841
                            lengthEnd = distance;
842
                            endPoints = item.Value;
843
                        }
844
                    }
845
                }
846
            }
847
            #region Branch가 양쪽 전부일 때
848
            if (_StartConnector != null && _StartConnector == _EndConnector)
849
            {
850
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
851

    
852
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
853
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
854

    
855
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
856
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
857
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
858
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
859
                   startPoints[startPoints.Count - 1][0],
860
                   startPoints[startPoints.Count - 1][1],
861
                   startPoints[startPoints.Count - 2][0],
862
                   startPoints[startPoints.Count - 2][1]);
863

    
864
                for (int i = 0; i < startPoints.Count; i++)
865
                {
866
                    double[] point = startPoints[i];
867
                    if (i == 0)
868
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
869
                    else if (i == startPoints.Count - 1)
870
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
871
                    else
872
                        placeRunInputs.AddPoint(point[0], point[1]);
873
                }
874

    
875
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
876
                if (_LMConnector != null)
877
                {
878
                    _LMConnector.Commit();
879
                    foreach (var item in lines)
880
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
881
                }
882

    
883
                foreach (var item in startConnectorVertices)
884
                    ReleaseCOMObjects(item.Key);
885
                foreach (var item in endConnectorVertices)
886
                    ReleaseCOMObjects(item.Key);
887
                ReleaseCOMObjects(placeRunInputs);
888
                ReleaseCOMObjects(_LMAItem);
889
                ReleaseCOMObjects(_LMConnector);
890
            }
891
            #endregion
892
            #region 양쪽이 다른 Branch 
893
            else
894
            {
895
                // Branch 시작 Connector
896
                if (_StartConnector != null)
897
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
898

    
899
                // Branch 끝 Connector
900
                if (_EndConnector != null)
901
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
902
            }
903
            #endregion
904

    
905
            if (_StartConnector != null)
906
                ReleaseCOMObjects(_StartConnector);
907
            if (_EndConnector != null)
908
                ReleaseCOMObjects(_EndConnector);
909
            foreach (var item in connectorVertices)
910
                ReleaseCOMObjects(item.Key);
911
        }
912

    
913
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
914
        {
915
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
916
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
917
            LMConnector _SameRunTargetConnector = null;
918
            LMSymbol _SameRunTargetSymbol = null;
919
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
920
            LMConnector _BranchTargetConnector = null;
921
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
922

    
923
            // 같은 Line Run의 Connector 찾기
924
            foreach (var item in connectorVertices)
925
            {
926
                if (item.Key == _Connector)
927
                    continue;
928

    
929
                if (IsStart &&
930
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
931
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
932
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
933
                {
934
                    _SameRunTargetConnector = item.Key;
935
                    break;
936
                }
937
                else if (!IsStart &&
938
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
939
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
940
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
941
                {
942
                    _SameRunTargetConnector = item.Key;
943
                    break;
944
                }
945
            }
946

    
947
            // Branch 반대편이 Symbol
948
            if (_SameRunTargetConnector == null)
949
            {
950
                foreach (var line in lines)
951
                {
952
                    foreach (var connector in line.CONNECTORS)
953
                    {
954
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
955
                        if (symbol != null)
956
                        {
957
                            _SameRunTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
958
                            break;
959
                        }
960
                    }
961
                }
962
            }
963

    
964
            // 기존 Connector 제거
965
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
966
            
967
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
968
            if (IsStart)
969
            {
970
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
971
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
972
            }
973
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
974
            else
975
            {
976
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
977
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
978
                    points[points.Count - 1][0],
979
                    points[points.Count - 1][1],
980
                    points[points.Count - 2][0],
981
                    points[points.Count - 2][1]);
982
            }
983

    
984
            for (int i = 0; i < points.Count; i++)
985
            {
986
                double[] point = points[i];
987
                if (i == 0)
988
                {
989
                    if (IsStart)
990
                    {
991
                        placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
992
                    }
993
                    else
994
                    {
995
                        if (_SameRunTargetConnector != null)
996
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
997
                        else if (_SameRunTargetSymbol != null)
998
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
999
                        else
1000
                            placeRunInputs.AddPoint(point[0], point[1]);
1001
                    }
1002
                }
1003
                else if (i == points.Count - 1)
1004
                {
1005
                    if (IsStart)
1006
                    {
1007
                        if (_SameRunTargetConnector != null)
1008
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
1009
                        else if (_SameRunTargetSymbol != null)
1010
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
1011
                        else
1012
                            placeRunInputs.AddPoint(point[0], point[1]);
1013
                    }
1014
                    else
1015
                    {
1016
                        if (_BranchTargetConnector != null)
1017
                        {
1018
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
1019
                        }
1020
                    }
1021
                }
1022
                else
1023
                    placeRunInputs.AddPoint(point[0], point[1]);
1024
            }
1025
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
1026
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1027

    
1028
            if (_LMConnector != null)
1029
            {
1030
                if (_SameRunTargetConnector != null)
1031
                {
1032
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
1033
                }
1034
                else
1035
                {
1036
                    foreach (var item in lines)
1037
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
1038
                }
1039

    
1040
                _LMConnector.Commit();
1041
                ReleaseCOMObjects(_LMConnector);
1042
            }
1043

    
1044
            ReleaseCOMObjects(placeRunInputs);
1045
            ReleaseCOMObjects(_LMAItem);
1046
            if (_BranchTargetConnector != null)
1047
                ReleaseCOMObjects(_BranchTargetConnector);
1048
            if (_SameRunTargetConnector != null)
1049
                ReleaseCOMObjects(_SameRunTargetConnector);
1050
            if (_SameRunTargetSymbol != null)
1051
                ReleaseCOMObjects(_SameRunTargetSymbol);
1052
            foreach (var item in connectorVertices)
1053
                ReleaseCOMObjects(item.Key);
1054
            foreach (var item in branchConnectorVertices)
1055
                ReleaseCOMObjects(item.Key);
1056
        }
1057

    
1058
        private void EndBreakModeling(EndBreak endBreak)
1059
        {
1060
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
1061
            LMConnector targetLMConnector = null;
1062
            if (ownerObj !=null && ownerObj.GetType() == typeof(Line))
1063
            {
1064
                Line ownerLine = ownerObj as Line;
1065
                LMLabelPersist _LmLabelPersist = null;
1066
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(ownerLine.SPPID.ModelItemId);
1067

    
1068
                targetLMConnector = FindTargetLMConnectorByPoint(connectorVertices, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1069
                
1070
                if (targetLMConnector != null)
1071
                {
1072
                    //double[] point = connectorVertices[targetLMConnector][connectorVertices[targetLMConnector].Count - 1];
1073
                    //Array array = new double[] { 0, point[0], point[1] };
1074
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1075
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1076
                }
1077

    
1078
                if (_LmLabelPersist != null)
1079
                {
1080
                    _LmLabelPersist.Commit();
1081
                    ReleaseCOMObjects(_LmLabelPersist);
1082
                }
1083
                else
1084
                    RetryEndBreakModeling(endBreak, targetLMConnector);
1085

    
1086
                foreach (var item in connectorVertices)
1087
                    ReleaseCOMObjects(item.Key);
1088

    
1089
            }
1090
            else if (ownerObj != null && ownerObj.GetType() == typeof(Symbol))
1091
            {
1092
                Symbol ownerSymbol = ownerObj as Symbol;
1093
                LMSymbol _LMSymbol = dataSource.GetSymbol(ownerSymbol.SPPID.RepresentationId);
1094

    
1095
                targetLMConnector = null;
1096
                double distance = double.MaxValue;
1097

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

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

    
1124
                foreach (LMConnector connector in _LMSymbol.Avoid2Connectors)
1125
                {
1126
                    if (connector.get_ItemStatus() == "Active")
1127
                    {
1128
                        dynamic OID = connector.get_GraphicOID();
1129
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1130
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1131
                        int verticesCount = lineStringGeometry.VertexCount;
1132
                        double[] vertices = null;
1133
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1134
                        for (int i = 0; i < verticesCount; i++)
1135
                        {
1136
                            double x = 0;
1137
                            double y = 0;
1138
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1139

    
1140
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1141
                            if (result < distance)
1142
                            {
1143
                                targetLMConnector = connector;
1144
                                distance = result;
1145
                            }
1146
                        }
1147
                    }
1148
                }
1149

    
1150
                if (targetLMConnector != null)
1151
                {
1152
                    LMLabelPersist _LmLabelPersist = null;
1153
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1154
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1155
                    if (_LmLabelPersist != null)
1156
                    {
1157
                        _LmLabelPersist.Commit();
1158
                        ReleaseCOMObjects(_LmLabelPersist);
1159
                    }
1160
                    else
1161
                        RetryEndBreakModeling(endBreak, targetLMConnector);
1162
                }
1163
                
1164
                ReleaseCOMObjects(_LMSymbol);
1165
            }
1166

    
1167
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1168
        }
1169

    
1170
        private void RetryEndBreakModeling(EndBreak endBreak, LMConnector targetLMConnector)
1171
        {
1172
            bool isZeroLength = Convert.ToBoolean(targetLMConnector.get_IsZeroLength());
1173
            Array array = null;
1174
            LMLabelPersist _LMLabelPersist = null;
1175
            LMConnector _LMConnector = null;
1176
            dynamic OID = targetLMConnector.get_GraphicOID();
1177
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1178
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1179
            int verticesCount = lineStringGeometry.VertexCount;
1180
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1181
            _LMAItem _LMAItem = _placement.PIDCreateItem(@"\Piping\Routing\Process Lines\Primary Piping.sym");
1182

    
1183
            if (isZeroLength)
1184
            {
1185
                double[] vertices = null;
1186
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1187
                double x = 0;
1188
                double y = 0;
1189
                lineStringGeometry.GetVertex(1, ref x, ref y);
1190

    
1191
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, x, y);
1192
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, x, y);
1193

    
1194
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1195
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1196

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

    
1200
                AutoJoinPipeRun(_LMConnector.ModelItemID);
1201
            }
1202
            else
1203
            {
1204
                List<double[]> vertices = new List<double[]>();
1205
                for (int i = 1; i <= verticesCount; i++)
1206
                {
1207
                    double x = 0;
1208
                    double y = 0;
1209
                    lineStringGeometry.GetVertex(i, ref x, ref y);
1210
                    vertices.Add(new double[] { x, y });
1211
                }
1212

    
1213
                for (int i = 0; i < vertices.Count; i++)
1214
                {
1215
                    double[] points = vertices[i];
1216
                    if (i == 0)
1217
                    {
1218
                        if (targetLMConnector.ConnectItem1SymbolObject != null)
1219
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1220
                        else
1221
                            placeRunInputs.AddPoint(points[0], points[1]);
1222
                    }
1223
                    else if (i == vertices.Count - 1)
1224
                    {
1225
                        if (targetLMConnector.ConnectItem2SymbolObject != null)
1226
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1227
                        else
1228
                            placeRunInputs.AddPoint(points[0], points[1]);
1229
                    }
1230
                    else
1231
                        placeRunInputs.AddPoint(points[0], points[1]);
1232
                }
1233

    
1234
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, targetLMConnector.ModelItemID);
1235
                
1236
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1237
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1238

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

    
1242
                AutoJoinPipeRun(_LMConnector.ModelItemID);
1243
                foreach (var line in lines)
1244
                    line.SPPID.ModelItemId = _LMConnector.ModelItemID;
1245
            }
1246

    
1247

    
1248
            if (_LMLabelPersist != null)
1249
            {
1250
                _LMLabelPersist.Commit();
1251
                ReleaseCOMObjects(_LMLabelPersist);
1252
            }
1253
            else
1254
            {
1255
                
1256
            }
1257

    
1258
            ReleaseCOMObjects(_LMConnector);
1259
            ReleaseCOMObjects(placeRunInputs);
1260
            ReleaseCOMObjects(_LMAItem);
1261
        }
1262

    
1263
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
1264
        {
1265
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
1266
            _LMAItem item1 = modelItem1.AsLMAItem();
1267
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
1268
            _LMAItem item2 = modelItem2.AsLMAItem();
1269
            
1270
            // item2가 item1으로 조인
1271
            try
1272
            {
1273
                _placement.PIDJoinRuns(ref item1, ref item2);
1274
                item1.Commit();
1275
                item2.Commit();
1276

    
1277
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
1278
                foreach (var line in lines)
1279
                    line.SPPID.ModelItemId = toModelItemId;
1280
            }
1281
            catch (Exception ex)
1282
            {
1283
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1284
            }
1285
            finally
1286
            {
1287
                ReleaseCOMObjects(modelItem1);
1288
                ReleaseCOMObjects(item1);
1289
                ReleaseCOMObjects(modelItem2);
1290
                ReleaseCOMObjects(item2);
1291
            }
1292
        }
1293

    
1294
        private void AutoJoinPipeRun(string modelItemId)
1295
        {
1296
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
1297
            _LMAItem item = modelItem.AsLMAItem();
1298
            try
1299
            {
1300
                string modelitemID = item.Id;
1301
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
1302
                string afterModelItemID = item.Id;
1303
                
1304
                if (modelitemID != afterModelItemID)
1305
                {
1306
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
1307
                    foreach (var line in lines)
1308
                        line.SPPID.ModelItemId = afterModelItemID;
1309
                }
1310
                item.Commit();
1311
            }
1312
            catch (Exception ex)
1313
            {
1314
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1315
            }
1316
            finally
1317
            {
1318
                ReleaseCOMObjects(modelItem);
1319
                ReleaseCOMObjects(item);
1320
            }
1321
        }
1322

    
1323
        private void JoinRunLine(LineRun run)
1324
        {
1325
            string modelItemId = string.Empty;
1326
            foreach (var item in run.RUNITEMS)
1327
            {
1328
                if (item.GetType() == typeof(Line))
1329
                {
1330
                    Line line = item as Line;
1331
                    AutoJoinPipeRun(line.SPPID.ModelItemId);
1332
                    modelItemId = line.SPPID.ModelItemId;
1333

    
1334
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1335
                }
1336
            }
1337
        }
1338

    
1339
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
1340
        {
1341
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
1342
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
1343

    
1344
            if (modelItem != null)
1345
            {
1346
                foreach (LMRepresentation rep in modelItem.Representations)
1347
                {
1348
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1349
                    {
1350
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1351
                        connectorVertices.Add(_LMConnector, new List<double[]>());
1352
                        dynamic OID = rep.get_GraphicOID();
1353
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1354
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1355
                        int verticesCount = lineStringGeometry.VertexCount;
1356
                        double[] vertices = null;
1357
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1358
                        for (int i = 0; i < verticesCount; i++)
1359
                        {
1360
                            double x = 0;
1361
                            double y = 0;
1362
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1363
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
1364
                        }
1365
                    }
1366
                }
1367

    
1368
                ReleaseCOMObjects(modelItem);
1369
            }
1370

    
1371
            return connectorVertices;
1372
        }
1373

    
1374
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
1375
        {
1376
            double length = double.MaxValue;
1377
            LMConnector targetConnector = null;
1378
            foreach (var item in connectorVertices)
1379
            {
1380
                List<double[]> points = item.Value;
1381
                for (int i = 0; i < points.Count - 1; i++)
1382
                {
1383
                    double[] point1 = points[i];
1384
                    double[] point2 = points[i + 1];
1385

    
1386
                    double maxLineX = Math.Max(point1[0], point2[0]);
1387
                    double minLineX = Math.Min(point1[0], point2[0]);
1388
                    double maxLineY = Math.Max(point1[1], point2[1]);
1389
                    double minLineY = Math.Min(point1[1], point2[1]);
1390

    
1391
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
1392

    
1393
                    // 두직선의 교차점
1394
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
1395
                    if (crossingPoint != null)
1396
                    {
1397
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
1398
                        if (length >= distance)
1399
                        {
1400
                            if (slope == SlopeType.Slope &&
1401
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
1402
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1403
                            {
1404
                                targetConnector = item.Key;
1405
                                length = distance;
1406
                            }
1407
                            else if (slope == SlopeType.HORIZONTAL &&
1408
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
1409
                            {
1410
                                targetConnector = item.Key;
1411
                                length = distance;
1412
                            }
1413
                            else if (slope == SlopeType.VERTICAL &&
1414
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1415
                            {
1416
                                targetConnector = item.Key;
1417
                                length = distance;
1418
                            }
1419
                        }
1420
                    }
1421
                }
1422

    
1423

    
1424
            }
1425

    
1426
            if (targetConnector == null)
1427
            {
1428
                foreach (var item in connectorVertices)
1429
                {
1430
                    List<double[]> points = item.Value;
1431
                    foreach (var point in points)
1432
                    {
1433
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, point[0], point[1]);
1434
                        if (length >= distance)
1435
                        {
1436
                            targetConnector = item.Key;
1437
                            length = distance;
1438
                        }
1439
                    }
1440
                }
1441

    
1442
            }
1443

    
1444
            return targetConnector;
1445
        }
1446

    
1447
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1448
        {
1449
            double length = double.MaxValue;
1450
            LMConnector targetConnector = null;
1451
            foreach (var item in connectorVertices)
1452
            {
1453
                List<double[]> points = item.Value;
1454

    
1455
                foreach (double[] point in points)
1456
                {
1457
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1458
                    if (length >= distance)
1459
                    {
1460
                        targetConnector = item.Key;
1461
                        length = distance;
1462
                    }
1463
                }
1464
            }
1465

    
1466
            return targetConnector;
1467
        }
1468

    
1469
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1470
        {
1471
            double length = double.MaxValue;
1472
            LMConnector targetConnector = null;
1473
            foreach (var item in connectorVertices)
1474
            {
1475
                List<double[]> points = item.Value;
1476
                for (int i = 0; i < points.Count - 1; i++)
1477
                {
1478
                    double[] point1 = points[i];
1479
                    double[] point2 = points[i + 1];
1480
                    double x1 = Math.Min(point1[0], point2[0]);
1481
                    double y1 = Math.Min(point1[1], point2[1]);
1482
                    double x2 = Math.Max(point1[0], point2[0]);
1483
                    double y2 = Math.Max(point1[1], point2[1]);
1484

    
1485
                    if ((x1 <= connX && x2 >= connX) ||
1486
                        (y1 <= connY && y2 >= connY))
1487
                    {
1488
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
1489
                        if (length >= distance)
1490
                        {
1491
                            targetConnector = item.Key;
1492
                            length = distance;
1493
                        }
1494

    
1495
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
1496
                        if (length >= distance)
1497
                        {
1498
                            targetConnector = item.Key;
1499
                            length = distance;
1500
                        }
1501
                    }
1502
                }
1503
            }
1504

    
1505
            // 못찾았을때.
1506
            length = double.MaxValue;
1507
            if (targetConnector == null)
1508
            {
1509
                foreach (var item in connectorVertices)
1510
                {
1511
                    List<double[]> points = item.Value;
1512

    
1513
                    foreach (double[] point in points)
1514
                    {
1515
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1516
                        if (length >= distance)
1517
                        {
1518
                            targetConnector = item.Key;
1519
                            length = distance;
1520
                        }
1521
                    }
1522
                }
1523
            }
1524

    
1525
            return targetConnector;
1526
        }
1527

    
1528
        private void LineNumberModeling(LineNumber lineNumber)
1529
        {
1530
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
1531
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
1532
            LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
1533
            if (connectedLMConnector != null)
1534
            {
1535
                double x = 0;
1536
                double y = 0;
1537
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
1538

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

    
1542
                foreach (var item in connectorVertices)
1543
                    ReleaseCOMObjects(item.Key);
1544
                if (_LmLabelPresist != null)
1545
                {
1546
                    _LmLabelPresist.Commit();
1547
                    lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
1548
                    ReleaseCOMObjects(_LmLabelPresist);
1549
                }
1550
                else
1551
                {
1552

    
1553
                }
1554
            }
1555

    
1556
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1557
        }
1558

    
1559
        private void InputLineNumberAttribute(LineNumber lineNumber)
1560
        {
1561
            foreach (LineRun run in lineNumber.RUNS)
1562
            {
1563
                foreach (var item in run.RUNITEMS)
1564
                {
1565
                    if (item.GetType() == typeof(Symbol))
1566
                    {
1567
                        Symbol symbol = item as Symbol;
1568
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1569
                        LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1570

    
1571
                        if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
1572
                        {
1573
                            foreach (var attribute in lineNumber.ATTRIBUTES)
1574
                            {
1575
                                LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1576
                                if (mapping != null)
1577
                                {
1578
                                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1579
                                    if (_LMAAttribute != null)
1580
                                    {
1581
                                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1582
                                            _LMAAttribute.set_Value(attribute.VALUE);
1583
                                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
1584
                                            _LMAAttribute.set_Value(attribute.VALUE);
1585
                                    }
1586
                                }
1587
                            }
1588
                            _LMModelItem.Commit();
1589
                        }
1590
                        if (_LMModelItem != null)
1591
                            ReleaseCOMObjects(_LMModelItem);
1592
                        if (_LMSymbol != null)
1593
                            ReleaseCOMObjects(_LMSymbol);
1594
                    }
1595
                    else if (item.GetType() == typeof(Line))
1596
                    {
1597
                        Line line = item as Line;
1598
                        if (line != null)
1599
                        {
1600
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1601
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
1602
                            {
1603
                                foreach (var attribute in lineNumber.ATTRIBUTES)
1604
                                {
1605
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1606
                                    if (mapping != null)
1607
                                    {
1608
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1609
                                        if (_LMAAttribute != null)
1610
                                        {
1611
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1612
                                                _LMAAttribute.set_Value(attribute.VALUE);
1613
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
1614
                                                _LMAAttribute.set_Value(attribute.VALUE);
1615
                                            
1616
                                        }
1617
                                    }
1618
                                }
1619
                                _LMModelItem.Commit();
1620
                            }
1621
                            if (_LMModelItem != null)
1622
                                ReleaseCOMObjects(_LMModelItem);
1623
                        }
1624
                    }
1625
                }
1626
            }
1627

    
1628
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1629
        }
1630

    
1631
        private void InputSymbolAttribute(Symbol symbol)
1632
        {
1633
            try
1634
            {
1635
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1636
                {
1637
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1638
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1639
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
1640

    
1641
                    foreach (var item in symbol.PROPERTIES)
1642
                    {
1643

    
1644

    
1645
                    }
1646

    
1647
                    foreach (var item in symbol.ATTRIBUTES)
1648
                    {
1649
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
1650
                        if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
1651
                        {
1652
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
1653
                            if (_Attribute != null)
1654
                            {
1655
                                // 임시
1656
                                if (item.ATTRIBUTETYPE == "String")
1657
                                {
1658
                                    if (!string.IsNullOrEmpty(item.VALUE))
1659
                                    {
1660
                                        if (!DBNull.Value.Equals(_Attribute.get_Value()) && !string.IsNullOrEmpty(_Attribute.get_Value()))
1661
                                        {
1662
                                            string value = _Attribute.get_Value() + "\n" + item.VALUE;
1663
                                            _Attribute.set_Value(value);
1664
                                        }
1665
                                        else
1666
                                        {
1667
                                            _Attribute.set_Value(item.VALUE);
1668
                                        }
1669
                                    }
1670
                                }
1671
                                else
1672
                                {
1673
                                    _Attribute.set_Value(item.VALUE);
1674
                                }
1675
                            }
1676
                        }
1677
                    }
1678

    
1679
                    foreach (var item in symbol.ASSOCIATIONS)
1680
                    {
1681

    
1682
                    }
1683

    
1684
                    ReleaseCOMObjects(_LMSymbol);
1685
                    ReleaseCOMObjects(_Attributes);
1686
                    ReleaseCOMObjects(_LMModelItem);
1687
                }
1688
            }
1689
            catch (Exception ex)
1690
            {
1691
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1692
            }
1693

    
1694
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1695
        }
1696

    
1697
        private void TextModeling(Text text)
1698
        {
1699
            LMSymbol _LMSymbol = null;
1700
            try
1701
            {
1702
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
1703
                if (text.ASSOCIATION)
1704
                {
1705
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
1706
                    if (owner.GetType() == typeof(Symbol))
1707
                    {
1708
                        Symbol symbol = owner as Symbol;
1709
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1710
                        if (_LMSymbol != null)
1711
                        {
1712
                            Association association = symbol.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
1713
                            List<BaseModel.Attribute> attributes = symbol.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
1714
                            AttributeMapping mapping = null;
1715
                            foreach (var attribute in attributes)
1716
                            {
1717
                                if (string.IsNullOrEmpty(attribute.VALUE) || attribute.VALUE == "None")
1718
                                    continue;
1719

    
1720
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
1721
                                if (mapping != null)
1722
                                    break;  
1723
                            }
1724

    
1725
                            if (mapping != null)
1726
                            {
1727
                                double x = 0;
1728
                                double y = 0;
1729

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

    
1733
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
1734
                                if (_LMLabelPersist!=null)
1735
                                {
1736
                                    _LMLabelPersist.Commit();
1737
                                    ReleaseCOMObjects(_LMLabelPersist);
1738
                                }
1739
                            }
1740
                        }
1741
                    }
1742
                    else if (owner.GetType() == typeof(Line))
1743
                    {
1744

    
1745
                    }
1746
                }
1747
                else
1748
                {
1749
                    LMItemNote _LMItemNote = null;
1750
                    LMAAttribute _LMAAttribute = null;
1751

    
1752
                    double x = 0;
1753
                    double y = 0;
1754

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

    
1757
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y);
1758
                    _LMSymbol.Commit();
1759
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1760
                    _LMItemNote.Commit();
1761
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1762
                    _LMAAttribute.set_Value(text.VALUE);
1763
                    _LMItemNote.Commit();
1764

    
1765
                    if (_LMAAttribute != null)
1766
                        ReleaseCOMObjects(_LMAAttribute);
1767
                    if (_LMItemNote != null)
1768
                        ReleaseCOMObjects(_LMItemNote);
1769
                }
1770
            }
1771
            catch (Exception ex)
1772
            {
1773

    
1774
            }
1775
            finally
1776
            {
1777
                if (_LMSymbol != null)
1778
                    ReleaseCOMObjects(_LMSymbol);
1779
            }
1780

    
1781
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1782
        }
1783

    
1784
        private void NoteModeling(Note note)
1785
        {
1786
            LMSymbol _LMSymbol = null;
1787
            LMItemNote _LMItemNote = null;
1788
            LMAAttribute _LMAAttribute = null;
1789

    
1790
            try
1791
            {
1792
                double x = 0;
1793
                double y = 0;
1794

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

    
1797
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
1798
                _LMSymbol.Commit();
1799
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1800
                _LMItemNote.Commit();
1801
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1802
                _LMAAttribute.set_Value(note.VALUE);
1803
                _LMItemNote.Commit();
1804
            }
1805
            catch (Exception ex)
1806
            {
1807

    
1808
            }
1809
            finally
1810
            {
1811
                if (_LMAAttribute != null)
1812
                    ReleaseCOMObjects(_LMAAttribute);
1813
                if (_LMItemNote != null)
1814
                    ReleaseCOMObjects(_LMItemNote);
1815
                if (_LMSymbol != null)
1816
                    ReleaseCOMObjects(_LMSymbol);
1817
            }
1818

    
1819
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1820
        }
1821

    
1822
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
1823
        {
1824
            if (location == Location.None)
1825
            {
1826
                x = originX;
1827
                y = originY;
1828
            }
1829
            else
1830
            {
1831
                if (location.HasFlag(Location.Center))
1832
                {
1833
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
1834
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
1835
                }
1836

    
1837
                if (location.HasFlag(Location.Left))
1838
                    x = SPPIDLabelLocation.X1;
1839
                else if (location.HasFlag(Location.Right))
1840
                    x = SPPIDLabelLocation.X2;
1841

    
1842
                if (location.HasFlag(Location.Down))
1843
                    y = SPPIDLabelLocation.Y1;
1844
                else if (location.HasFlag(Location.Up))
1845
                    y = SPPIDLabelLocation.Y2;
1846
            }
1847
        }
1848

    
1849
        public void ReleaseCOMObjects(params object[] objVars)
1850
        {
1851
            int intNewRefCount = 0;
1852
            foreach (object obj in objVars)
1853
            {
1854
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
1855
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
1856
            }
1857
        }
1858
    }
1859
}
클립보드 이미지 추가 (최대 크기: 500 MB)