프로젝트

일반

사용자정보

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

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ d5ec4d0f

이력 | 보기 | 이력해설 | 다운로드 (82.2 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
        public string DocumentLabelText { get; set; }
38

    
39
        int EquipCount;
40
        int SymbolCount;
41
        int LineCount;
42
        int NoteCount;
43
        int TextCount;
44
        int EndBreakCount;
45
        int LineNumberCount;
46

    
47
        int CurrentCount;
48
        int AllCount;
49

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

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

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

    
83
            AllCount = EquipCount + SymbolCount + LineCount + NoteCount + TextCount + EndBreakCount;
84
        }
85

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

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

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

    
120
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
121
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetParent, (IntPtr)radApp.HWnd);
122
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllStep, AllCount);
123
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetDocumentName, DocumentLabelText);
124

    
125
                    // Equipment Modeling
126
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
127
                    foreach (Equipment equipment in document.Equipments)
128
                        EquipmentModeling(equipment);
129

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

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

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

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

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

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

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

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

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

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

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

    
211
                                string sRep = null;
212
                                if (objectItem.GetType() == typeof(Symbol))
213
                                    sRep = ((Symbol)objectItem).SPPID.RepresentationId;
214
                                else if (objectItem.GetType() == typeof(Equipment))
215
                                    sRep = ((Equipment)objectItem).SPPID.RepresentationId;
216

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

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

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

    
278
                                                if (result)
279
                                                    break;
280
                                            }
281

    
282
                                            if (result)
283
                                                break;
284
                                        }
285
                                    }
286

    
287

    
288
                                    _LMModelItem.Commit();
289
                                    _LMLabelPresist.Commit();
290
                                    ReleaseCOMObjects(_TargetItem);
291
                                    ReleaseCOMObjects(_LMLabelPresist);
292
                                    ReleaseCOMObjects(_LMModelItem);
293
                                    ReleaseCOMObjects(_Attributes);
294
                                }
295
                            }
296
                        }
297

    
298
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
299
                    }
300
                    #endregion
301

    
302

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

    
321
                ReleaseCOMObjects(dataSource);
322
                ReleaseCOMObjects(_placement);
323

    
324
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
325
                SplashScreenManager.CloseForm(false);
326
            }
327
        }
328

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

    
356
                    prevLine = line;
357

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

    
371
            if (lines.Count > 0)
372
                LineModeling(lines);
373
        }
374

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

    
383
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
384
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
385
            }
386

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

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

    
427

    
428
        }
429

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

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

    
459
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
460
                return;
461

    
462
            LMSymbol _LMSymbol = null;
463

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

    
476
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
477
            {
478
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
479

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

    
487
                if (y2 < y)
488
                    y = y2;
489
                else if (y1 > y)
490
                    y = y1;
491

    
492
                if (x2 < x)
493
                    x = x2;
494
                else if (x1 > x)
495
                    x = x1;
496

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

    
517

    
518
            if (_LMSymbol != null)
519
            {
520
                _LMSymbol.Commit();
521
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
522

    
523
                foreach (var item in symbol.ChildSymbols)
524
                    CreateChildSymbol(item, _LMSymbol);
525
            }
526

    
527
            ReleaseCOMObjects(_LMSymbol);
528
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
529
        }
530

    
531
        private void EquipmentModeling(Equipment equipment)
532
        {
533
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
534
                return;
535

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

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

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

    
580
            if (_LMSymbol != null)
581
            {
582
                _LMSymbol.Commit();
583
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
584
                ReleaseCOMObjects(_LMSymbol);
585
            }
586

    
587
            if (targetItem != null)
588
            {
589
                ReleaseCOMObjects(targetItem);
590
            }
591
            
592
            ReleaseCOMObjects(_LMSymbol);
593

    
594
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
595
        }
596

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

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

    
615
            ReleaseCOMObjects(_LMSymbol);
616
        }
617

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

    
636
                            return false;
637

    
638
                        }
639
                    }
640
                }
641
            }
642

    
643
            return false;
644
        }
645

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

    
657
            Line startBranchLine = null;
658
            Line endBranchLine = null;
659

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

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

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

    
697
                    if (i != 0)
698
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
699

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

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

    
729
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
730

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

    
742

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

    
756
            ReleaseCOMObjects(_lMConnector);
757
            ReleaseCOMObjects(placeRunInputs);
758
            ReleaseCOMObjects(_LMAItem);
759
        }
760

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

    
780
                            if (child != null)
781
                                break;
782
                        }
783

    
784
                        if (child != null)
785
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
786
                    }
787

    
788
                    break;  
789
                }
790
            }
791

    
792
            return _LMSymbol;
793
        }
794

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

    
805
            return null;
806
        }
807

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

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

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

    
855
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
856
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
857

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

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

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

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

    
902
                // Branch 끝 Connector
903
                if (_EndConnector != null)
904
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
905
            }
906
            #endregion
907

    
908
            if (_StartConnector != null)
909
                ReleaseCOMObjects(_StartConnector);
910
            if (_EndConnector != null)
911
                ReleaseCOMObjects(_EndConnector);
912
            foreach (var item in connectorVertices)
913
                ReleaseCOMObjects(item.Key);
914
        }
915

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

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

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

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

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

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

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

    
1043
                _LMConnector.Commit();
1044
                ReleaseCOMObjects(_LMConnector);
1045
            }
1046

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

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

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

    
1081
                if (_LmLabelPersist != null)
1082
                {
1083
                    _LmLabelPersist.Commit();
1084
                    ReleaseCOMObjects(_LmLabelPersist);
1085
                }
1086
                else
1087
                    RetryEndBreakModeling(endBreak, targetLMConnector);
1088

    
1089
                foreach (var item in connectorVertices)
1090
                    ReleaseCOMObjects(item.Key);
1091

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

    
1098
                targetLMConnector = null;
1099
                double distance = double.MaxValue;
1100

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

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

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

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

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

    
1170
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1171
        }
1172

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

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

    
1194
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, x, y);
1195
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, x, y);
1196

    
1197
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1198
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1199

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

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

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

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

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

    
1245
                AutoJoinPipeRun(_LMConnector.ModelItemID);
1246
                foreach (var line in lines)
1247
                    line.SPPID.ModelItemId = _LMConnector.ModelItemID;
1248
            }
1249

    
1250

    
1251
            if (_LMLabelPersist != null)
1252
            {
1253
                _LMLabelPersist.Commit();
1254
                ReleaseCOMObjects(_LMLabelPersist);
1255
            }
1256
            else
1257
            {
1258
                
1259
            }
1260

    
1261
            ReleaseCOMObjects(_LMConnector);
1262
            ReleaseCOMObjects(placeRunInputs);
1263
            ReleaseCOMObjects(_LMAItem);
1264
        }
1265

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

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

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

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

    
1337
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1338
                }
1339
            }
1340
        }
1341

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

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

    
1371
                ReleaseCOMObjects(modelItem);
1372
            }
1373

    
1374
            return connectorVertices;
1375
        }
1376

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

    
1389
                    double maxLineX = Math.Max(point1[0], point2[0]);
1390
                    double minLineX = Math.Min(point1[0], point2[0]);
1391
                    double maxLineY = Math.Max(point1[1], point2[1]);
1392
                    double minLineY = Math.Min(point1[1], point2[1]);
1393

    
1394
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
1395

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

    
1426

    
1427
            }
1428

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

    
1445
            }
1446

    
1447
            return targetConnector;
1448
        }
1449

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

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

    
1469
            return targetConnector;
1470
        }
1471

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

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

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

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

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

    
1528
            return targetConnector;
1529
        }
1530

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

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

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

    
1556
                }
1557
            }
1558

    
1559
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1560
        }
1561

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

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

    
1631
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1632
        }
1633

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

    
1644
                    foreach (var item in symbol.PROPERTIES)
1645
                    {
1646

    
1647

    
1648
                    }
1649

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

    
1682
                    foreach (var item in symbol.ASSOCIATIONS)
1683
                    {
1684

    
1685
                    }
1686

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

    
1697
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1698
        }
1699

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

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

    
1728
                            if (mapping != null)
1729
                            {
1730
                                double x = 0;
1731
                                double y = 0;
1732

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

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

    
1748
                    }
1749
                }
1750
                else
1751
                {
1752
                    LMItemNote _LMItemNote = null;
1753
                    LMAAttribute _LMAAttribute = null;
1754

    
1755
                    double x = 0;
1756
                    double y = 0;
1757

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

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

    
1768
                    if (_LMAAttribute != null)
1769
                        ReleaseCOMObjects(_LMAAttribute);
1770
                    if (_LMItemNote != null)
1771
                        ReleaseCOMObjects(_LMItemNote);
1772
                }
1773
            }
1774
            catch (Exception ex)
1775
            {
1776

    
1777
            }
1778
            finally
1779
            {
1780
                if (_LMSymbol != null)
1781
                    ReleaseCOMObjects(_LMSymbol);
1782
            }
1783

    
1784
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1785
        }
1786

    
1787
        private void NoteModeling(Note note)
1788
        {
1789
            LMSymbol _LMSymbol = null;
1790
            LMItemNote _LMItemNote = null;
1791
            LMAAttribute _LMAAttribute = null;
1792

    
1793
            try
1794
            {
1795
                double x = 0;
1796
                double y = 0;
1797

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

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

    
1811
            }
1812
            finally
1813
            {
1814
                if (_LMAAttribute != null)
1815
                    ReleaseCOMObjects(_LMAAttribute);
1816
                if (_LMItemNote != null)
1817
                    ReleaseCOMObjects(_LMItemNote);
1818
                if (_LMSymbol != null)
1819
                    ReleaseCOMObjects(_LMSymbol);
1820
            }
1821

    
1822
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1823
        }
1824

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

    
1840
                if (location.HasFlag(Location.Left))
1841
                    x = SPPIDLabelLocation.X1;
1842
                else if (location.HasFlag(Location.Right))
1843
                    x = SPPIDLabelLocation.X2;
1844

    
1845
                if (location.HasFlag(Location.Down))
1846
                    y = SPPIDLabelLocation.Y1;
1847
                else if (location.HasFlag(Location.Up))
1848
                    y = SPPIDLabelLocation.Y2;
1849
            }
1850
        }
1851

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