프로젝트

일반

사용자정보

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

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ d19ae675

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

1 cfda1fed gaqhf
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 8aa6f2db gaqhf
using Ingr.RAD2D.Interop.RAD2D;
9
using Ingr.RAD2D.Internal;
10
using Ingr.RAD2D.Helper;
11 cfda1fed gaqhf
using Converter.BaseModel;
12
using Converter.SPPID.Model;
13
using Converter.SPPID.Properties;
14
using Converter.SPPID.Util;
15
using Converter.SPPID.DB;
16 5e6ecf05 gaqhf
using Ingr.RAD2D.MacroControls.CmdCtrl;
17
using Ingr.RAD2D;
18 5dfb8a24 gaqhf
using System.Windows;
19 cfda1fed gaqhf
using System.Threading;
20 5dfb8a24 gaqhf
using System.Drawing;
21 cfda1fed gaqhf
using Microsoft.VisualBasic;
22
using Newtonsoft.Json;
23
24
namespace Converter.SPPID
25
{
26
    public class AutoModeling
27
    {
28 809a7640 gaqhf
        Placement _placement;
29
        LMADataSource dataSource;
30 d19ae675 gaqhf
        dynamic application;
31 5e6ecf05 gaqhf
        Ingr.RAD2D.Application radApp;
32 cfda1fed gaqhf
        SPPID_Document document;
33 b65a7e32 gaqhf
        ETCSetting _ETCSetting;
34 f1c9dbaa gaqhf
35 3165c259 gaqhf
        List<Tuple<string, Line, Line>> BranchLines = new List<Tuple<string, Line, Line>>();
36 d19ae675 gaqhf
        public AutoModeling(SPPID_Document document, dynamic application, Ingr.RAD2D.Application radApp)
37 cfda1fed gaqhf
        {
38
            this.document = document;
39 d19ae675 gaqhf
            this.application = application;
40 5e6ecf05 gaqhf
            this.radApp = radApp;
41 b65a7e32 gaqhf
            this._ETCSetting = ETCSetting.GetInstance();
42 cfda1fed gaqhf
        }
43
44 c2fef4ca gaqhf
        public void Test()
45
        {
46
            _placement = new Placement();
47
            dataSource = _placement.PIDDataSource;
48
49
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveSelectSet[0] as Ingr.RAD2D.Symbol2d;
50
            if (symbol2d != null)
51
            {
52
                string modelID = symbol2d.AttributeSets[0][5].GetValue().ToString();
53
54
                LMModelItem modelItem = dataSource.GetModelItem(modelID);
55
                foreach (LMRepresentation rep in modelItem.Representations)
56
                {
57
                    if (rep.Attributes["RepresentationType"].get_Value() == "Symbol")
58
                    {
59
                        LMSymbol symbol = dataSource.GetSymbol(rep.Id);
60
61
                        int rotateIndex = symbol.get_IsRotatableIndex();
62
                        int mirrorIndex = symbol.get_IsMirroredIndex();
63
                        
64
                    }
65
                }
66
                    
67
                
68
                //symbol2d.AttributeSets[]
69
                //dataSource.GetSymbol()
70
            }
71
        }
72
73 cfda1fed gaqhf
        public void Run()
74
        {
75 8634af60 gaqhf
            CloseOPCForm.Run();
76 809a7640 gaqhf
            _placement = new Placement();
77
            dataSource = _placement.PIDDataSource;
78 65a1ed4b gaqhf
79 d19ae675 gaqhf
            dynamic newDrawing = application.Drawings.Add(document.Unit, document.Template, document.DrawingNumber, document.DrawingName);
80
            application.ActiveWindow.Fit();
81
            Thread.Sleep(100);
82
            application.ActiveWindow.Zoom = 60;
83
            Thread.Sleep(100);
84 65a1ed4b gaqhf
85 5e6ecf05 gaqhf
            try
86 5dfb8a24 gaqhf
            {
87 1efc25a3 gaqhf
                // Equipment Modeling
88 5e6ecf05 gaqhf
                foreach (Equipment equipment in document.Equipments)
89 b9e9f4c8 gaqhf
                    EquipmentModeling(equipment);
90 65a1ed4b gaqhf
                
91 1efc25a3 gaqhf
                // LineRun Symbol Modeling
92 5e6ecf05 gaqhf
                foreach (LineNumber lineNumber in document.LINENUMBERS)
93
                    foreach (LineRun run in lineNumber.RUNS)
94
                        SymbolModelingByRun(run);
95 65a1ed4b gaqhf
                
96 1efc25a3 gaqhf
                // TrimLineRun Symbol Modeling
97 5e6ecf05 gaqhf
                foreach (TrimLine trimLine in document.TRIMLINES)
98
                    foreach (LineRun run in trimLine.RUNS)
99
                        SymbolModelingByRun(run);
100 65a1ed4b gaqhf
                
101 1efc25a3 gaqhf
                // LineRun Line Modeling
102 5e6ecf05 gaqhf
                foreach (LineNumber lineNumber in document.LINENUMBERS)
103
                    foreach (LineRun run in lineNumber.RUNS)
104
                        LineModelingByRun(run);
105 65a1ed4b gaqhf
                
106 1efc25a3 gaqhf
                // TrimLineRun Line Modeling
107 5e6ecf05 gaqhf
                foreach (TrimLine trimLine in document.TRIMLINES)
108
                    foreach (LineRun run in trimLine.RUNS)
109
                        LineModelingByRun(run);
110 65a1ed4b gaqhf
                
111 1efc25a3 gaqhf
                // Branch Line Modeling
112 3165c259 gaqhf
                foreach (var item in BranchLines)
113 335b7a24 gaqhf
                    BranchLineModeling(item);
114 65a1ed4b gaqhf
                
115 1efc25a3 gaqhf
                // EndBreak Modeling
116 3165c259 gaqhf
                foreach (var item in document.EndBreaks)
117
                    EndBreakModeling(item);
118 65a1ed4b gaqhf
                
119 1efc25a3 gaqhf
                // LineNumber Modeling
120 10872260 gaqhf
                foreach (var item in document.LINENUMBERS)
121
                    LineNumberModeling(item);
122 b9e9f4c8 gaqhf
123 65a1ed4b gaqhf
                // Note Modeling
124
                foreach (var item in document.NOTES)
125
                    NoteModeling(item);
126 5a83fda2 gaqhf
127 65a1ed4b gaqhf
                // Text Modeling
128
                foreach (var item in document.TEXTINFOS)
129
                    TextModeling(item);
130
131
                // Input LineNumber Attribute
132
                foreach (var item in document.LINENUMBERS)
133
                    InputLineNumberAttribute(item);
134
135
                // Input Symbol Attribute
136
                foreach (var item in document.SYMBOLS)
137
                    InputSymbolAttribute(item);
138 310aeb31 gaqhf
139
                #region 임시 Label
140
                foreach (var item in document.SYMBOLS)
141
                {
142 8b069d9f gaqhf
                    if (item.SPPID.MAPPINGNAME.Contains("Labels - "))
143 310aeb31 gaqhf
                    {
144
                        SPPIDSymbolInfo info = item.SPPID;
145
                        Array points = new double[] { 0, item.SPPID.ORIGINAL_X, item.SPPID.ORIGINAL_Y };
146 c3d2e266 gaqhf
                        BaseModel.Attribute itemAttribute = item.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
147
                        if (itemAttribute == null)
148
                            continue;
149 26c6f818 gaqhf
                        string symbolUID = item.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL").VALUE;
150
                        object objectItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
151
                        if (objectItem != null)
152 310aeb31 gaqhf
                        {
153 8b069d9f gaqhf
154 26c6f818 gaqhf
                            string sRep = null;
155
                            if (objectItem.GetType() == typeof(Symbol))
156
                                sRep = ((Symbol)objectItem).SPPID.RepresentationId;
157
                            else if (objectItem.GetType() == typeof(Equipment))
158
                                sRep = ((Equipment)objectItem).SPPID.RepresentationId;
159
160
                            if (!string.IsNullOrEmpty(sRep))
161 310aeb31 gaqhf
                            {
162 26c6f818 gaqhf
                                LMSymbol _TargetItem = dataSource.GetSymbol(sRep);
163
                                LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(item.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: _TargetItem.AsLMRepresentation(), IsLeaderVisible: true);
164
                                LMModelItem _LMModelItem = _TargetItem.ModelItemObject;
165
                                LMAAttributes _Attributes = _LMModelItem.Attributes;
166
167
                                foreach (var attribute in item.ATTRIBUTES)
168 8b069d9f gaqhf
                                {
169 26c6f818 gaqhf
                                    if (!string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
170
                                    {
171
                                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
172
                                        if (mapping != null)
173
                                        {
174
                                            LMAAttribute _LMAAttribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
175
                                            if (mapping.SPPIDATTRIBUTENAME.Contains("Description"))
176
                                            {
177
                                                if (!DBNull.Value.Equals(_LMAAttribute.get_Value()) && !string.IsNullOrEmpty(_LMAAttribute.get_Value()))
178
                                                {
179
                                                    string value = _LMAAttribute.get_Value() + "\n" + attribute.VALUE;
180
                                                    _LMAAttribute.set_Value(value);
181
                                                }
182
                                                else
183
                                                {
184
                                                    _LMAAttribute.set_Value(attribute.VALUE);
185
                                                }
186
                                            }
187
                                            else if (_LMAAttribute != null)
188
                                                _LMAAttribute.set_Value(attribute.VALUE);
189
                                        }
190
                                    }
191 8b069d9f gaqhf
                                }
192 f9125a54 gaqhf
193
                                
194
195 26c6f818 gaqhf
                                _LMModelItem.Commit();
196
                                _LMLabelPresist.Commit();
197
                                ReleaseCOMObjects(_TargetItem);
198
                                ReleaseCOMObjects(_LMLabelPresist);
199
                                ReleaseCOMObjects(_LMModelItem);
200
                                ReleaseCOMObjects(_Attributes);
201 310aeb31 gaqhf
                            }
202
                        }
203
                    }
204
                }
205
                #endregion
206 8634af60 gaqhf
207
                // LineRun Line Join
208
                foreach (LineNumber lineNumber in document.LINENUMBERS)
209
                    foreach (LineRun run in lineNumber.RUNS)
210
                        JoinRunLine(run);
211
212
                // TrimLineRun Line Join
213
                foreach (TrimLine trimLine in document.TRIMLINES)
214
                    foreach (LineRun run in trimLine.RUNS)
215
                        JoinRunLine(run);
216 809a7640 gaqhf
            }
217 5e6ecf05 gaqhf
            catch (Exception ex)
218
            {
219
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
220
            }
221
            finally
222
            {
223
                ReleaseCOMObjects(dataSource);
224
                ReleaseCOMObjects(_placement);
225
            }
226 65a1ed4b gaqhf
227 8634af60 gaqhf
            CloseOPCForm.Stop();
228 65a1ed4b gaqhf
            //System.Windows.Forms.MessageBox.Show("end");
229
        }
230
231 8aa6f2db gaqhf
        private void LineModelingByRun(LineRun run)
232 809a7640 gaqhf
        {
233
            Line prevLine = null;
234
            List<Line> lines = new List<Line>();
235
            foreach (var item in run.RUNITEMS)
236
            {
237
                // Line일 경우
238
                if (item.GetType() == typeof(Line))
239
                {
240
                    Line line = item as Line;
241
                    if (prevLine == null)
242
                        lines.Add(line);
243
                    else if (prevLine != null)
244
                    {
245
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
246
                            lines.Add(line);
247
                        else
248 f1c9dbaa gaqhf
                        {
249 f2baa6a3 gaqhf
                            if (lines.Count > 0)
250
                            {
251
                                LineModeling(lines);
252
                                lines.Clear();
253
                            }
254 809a7640 gaqhf
                            lines.Add(line);
255 5dfb8a24 gaqhf
                        }
256
                    }
257
258 809a7640 gaqhf
                    prevLine = line;
259
                }
260
                // Symbol 일 경우
261
                else if (item.GetType() == typeof(Symbol))
262
                {
263 f1c9dbaa gaqhf
                    if (lines.Count > 0)
264 809a7640 gaqhf
                    {
265 f1c9dbaa gaqhf
                        LineModeling(lines);
266 809a7640 gaqhf
                        lines.Clear();
267
                    }
268 5dfb8a24 gaqhf
                }
269
            }
270
271 809a7640 gaqhf
            if (lines.Count > 0)
272
                LineModeling(lines);
273
        }
274 5dfb8a24 gaqhf
275 8aa6f2db gaqhf
        private void SymbolModelingByRun(LineRun run)
276 809a7640 gaqhf
        {
277
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
278
            if (run.RUNITEMS.Count > 0)
279
            {
280
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
281
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
282
283
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
284
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
285
            }
286
287
            Symbol prevSymbol = null;
288
            Symbol targetSymbol = null;
289
            foreach (var item in run.RUNITEMS)
290
            {
291
                if (item.GetType() == typeof(Symbol))
292
                {
293
                    Symbol symbol = item as Symbol;
294
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
295
                    prevSymbol = symbol;
296
                    targetSymbol = symbol;
297
                }
298
                else
299
                {
300
                    targetSymbol = null;
301
                }
302
            }
303
        }
304
305 8aa6f2db gaqhf
        private void SymbolModelingByRunStart(Symbol symbol, LineRun run)
306 809a7640 gaqhf
        {
307
            foreach (var connector in symbol.CONNECTORS)
308
            {
309
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
310
                if (targetItem != null &&
311
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
312
                    !IsSameLineNumber(symbol, targetItem))
313
                {
314
                    SymbolModeling(symbol, targetItem as Symbol, null);
315
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
316
                    {
317
                        object item = run.RUNITEMS[i];
318
                        if (item.GetType() == typeof(Symbol))
319
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol, null);
320
                        else
321
                            break;
322
                    }
323
                    break;
324
                }
325
            }
326
327
328
        }
329
330 8aa6f2db gaqhf
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
331 809a7640 gaqhf
        {
332
            foreach (var connector in symbol.CONNECTORS)
333
            {
334
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
335
                if (targetItem != null &&
336
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
337
                    !IsSameLineNumber(symbol, targetItem))
338
                {
339
                    SymbolModeling(symbol, targetItem as Symbol, null);
340
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
341
                    {
342
                        object item = run.RUNITEMS[i];
343
                        if (item.GetType() == typeof(Symbol))
344
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol, null);
345
                        else
346
                            break;
347
                    }
348
                    break;
349
                }
350
            }
351 5dfb8a24 gaqhf
        }
352 cfda1fed gaqhf
353 809a7640 gaqhf
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
354
        {
355 c2fef4ca gaqhf
            // 임시
356 c3d2e266 gaqhf
            if (symbol.SPPID.MAPPINGNAME.Contains("Labels - "))
357
                return;
358 c2fef4ca gaqhf
359 809a7640 gaqhf
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
360
                return;
361
362
            LMSymbol _LMSymbol = null;
363
364
            string mappingPath = symbol.SPPID.MAPPINGNAME;
365
            double x = symbol.SPPID.ORIGINAL_X;
366
            double y = symbol.SPPID.ORIGINAL_Y;
367
            int mirror = 0;
368
            double angle = symbol.ANGLE;
369 c2fef4ca gaqhf
            // 임시
370
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
371
            {
372
                mirror = 1;
373
            }
374
                
375 809a7640 gaqhf
376
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
377
            {
378
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
379 b9e9f4c8 gaqhf
380
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
381
                double x1 = 0;
382
                double x2 = 0;
383
                double y1 = 0;
384
                double y2 = 0;
385
                symbol2d.Range(out x1, out y1, out x2, out y2);
386
387
                if (y2 < y)
388
                    y = y2;
389
                else if (y1 > y)
390
                    y = y1;
391
392
                if (x2 < x)
393
                    x = x2;
394
                else if (x1 > x)
395
                    x = x1;
396
397 809a7640 gaqhf
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
398
                ReleaseCOMObjects(_TargetItem);
399
            }
400
            else if (prevSymbol != null)
401
            {
402
                LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
403
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
404
                double prevX = _PrevSymbol.get_XCoordinate();
405
                double prevY = _PrevSymbol.get_YCoordinate();
406
                if (slopeType == SlopeType.HORIZONTAL)
407
                    y = prevY;
408
                else if (slopeType == SlopeType.VERTICAL)
409
                    x = prevX;
410
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
411
            }
412
            else
413
            {
414
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
415
            }
416
417
418
            if (_LMSymbol != null)
419
            {
420
                _LMSymbol.Commit();
421
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
422 ac78b508 gaqhf
423 4b4dbca9 gaqhf
                foreach (var item in symbol.ChildSymbols)
424
                    CreateChildSymbol(item, _LMSymbol);
425 809a7640 gaqhf
            }
426
427
            ReleaseCOMObjects(_LMSymbol);
428
        }
429
430 b9e9f4c8 gaqhf
        private void EquipmentModeling(Equipment equipment)
431
        {
432
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
433
                return;
434
435
            LMSymbol _LMSymbol = null;
436
            LMSymbol targetItem = null;
437
            string mappingPath = equipment.SPPID.MAPPINGNAME;
438
            double x = equipment.SPPID.ORIGINAL_X;
439
            double y = equipment.SPPID.ORIGINAL_Y;
440
            int mirror = 0;
441
            double angle = equipment.ANGLE;
442
443
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
444
            if (connector != null)
445
            {
446
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
447
                if (connEquipment != null)
448
                {
449
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
450
                        EquipmentModeling(connEquipment);
451
452
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
453
                    {
454
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
455
                        if (targetItem != null)
456
                        {
457
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
458
                        }
459
                        else
460
                        {
461
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
462
                        }
463
                    }
464
                    else
465
                    {
466
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
467
                    }
468
                }
469
                else
470
                {
471
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
472
                }
473
            }
474
            else
475
            {
476
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
477
            }
478
479
            if (_LMSymbol != null)
480
            {
481
                _LMSymbol.Commit();
482
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
483
                ReleaseCOMObjects(_LMSymbol);
484
            }
485
486
            if (targetItem != null)
487
            {
488
                ReleaseCOMObjects(targetItem);
489
            }
490
            
491
            //if (equipment.CONNECTORS)
492
            //{
493
494
            //}
495
496
            //if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
497
            //{
498
            //    LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
499
            //    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
500
            //    ReleaseCOMObjects(_TargetItem);
501
            //}
502
            //else if (prevSymbol != null)
503
            //{
504
            //    LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
505
            //    SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
506
            //    double prevX = _PrevSymbol.get_XCoordinate();
507
            //    double prevY = _PrevSymbol.get_YCoordinate();
508
            //    if (slopeType == SlopeType.HORIZONTAL)
509
            //        y = prevY;
510
            //    else if (slopeType == SlopeType.VERTICAL)
511
            //        x = prevX;
512
            //    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
513
            //}
514
            //else
515
            //{
516
            //    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
517
            //}
518
519
520
            //if (_LMSymbol != null)
521
            //{
522
            //    _LMSymbol.Commit();
523
            //    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
524
525
            //    foreach (var item in symbol.ChildSymbols)
526
            //        CreateChildSymbol(item, _LMSymbol);
527
            //}
528
529
            ReleaseCOMObjects(_LMSymbol);
530
        }
531
532 4b4dbca9 gaqhf
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
533 ac78b508 gaqhf
        {
534 4b4dbca9 gaqhf
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
535
            double x1 = 0;
536
            double x2 = 0;
537
            double y1 = 0;
538
            double y2 = 0;
539
            symbol2d.Range(out x1, out y1, out x2, out y2);
540
541
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
542
            if (_LMSymbol != null)
543
            {
544
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
545
                foreach (var item in childSymbol.ChildSymbols)
546
                    CreateChildSymbol(item, _LMSymbol);
547
            }
548
            
549 ac78b508 gaqhf
550
            ReleaseCOMObjects(_LMSymbol);
551
        }
552
553 809a7640 gaqhf
        private bool IsSameLineNumber(object item, object targetItem)
554
        {
555
            foreach (var lineNumber in document.LINENUMBERS)
556
            {
557
                foreach (var run in lineNumber.RUNS)
558
                {
559
                    foreach (var runItem in run.RUNITEMS)
560
                    {
561
                        if (runItem == item)
562
                        {
563
                            foreach (var findItem in run.RUNITEMS)
564
                            {
565
                                if (findItem == targetItem)
566
                                {
567
                                    return true;
568
                                }
569
                            }
570
571
                            return false;
572
573
                        }
574
                    }
575
                }
576
            }
577
578
            return false;
579
        }
580
581 1b261371 gaqhf
        private void LineModeling(List<Line> lines)
582
        {
583 335b7a24 gaqhf
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
584 1b261371 gaqhf
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
585 f1c9dbaa gaqhf
            LMSymbol _LMSymbol1 = null;
586
            LMSymbol _LMSymbol2 = null;
587 5e6ecf05 gaqhf
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
588
            LMConnector targetConnector1 = null;
589
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
590
            LMConnector targetConnector2 = null;
591 335b7a24 gaqhf
592
            Line startBranchLine = null;
593
            Line endBranchLine = null;
594
595 1b261371 gaqhf
            for (int i = 0; i < lines.Count; i++)
596
            {
597
                Line line = lines[i];
598 f1c9dbaa gaqhf
                if (i == 0 || i + 1 != lines.Count)
599
                {
600 809a7640 gaqhf
                    // 시작점에 연결된 Symbol 찾기
601 f1c9dbaa gaqhf
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
602
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
603
                    {
604 f2baa6a3 gaqhf
                        _LMSymbol1 = GetTargetSymbol(connItem as Symbol, line);
605 f1c9dbaa gaqhf
                        if (_LMSymbol1 != null)
606
                            placeRunInputs.AddSymbolTarget(_LMSymbol1, line.SPPID.START_X, line.SPPID.START_Y);
607
                        else
608
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
609
                    }
610 335b7a24 gaqhf
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
611 5e6ecf05 gaqhf
                    {
612
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
613 56bc67e1 gaqhf
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
614 5e6ecf05 gaqhf
615
                        if (targetConnector1 != null)
616
                            placeRunInputs.AddConnectorTarget(targetConnector1, line.SPPID.START_X, line.SPPID.START_Y);
617
                        else
618 335b7a24 gaqhf
                        {
619
                            startBranchLine = connItem as Line;
620 5e6ecf05 gaqhf
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
621 335b7a24 gaqhf
                        }
622 5e6ecf05 gaqhf
                    }
623 f1c9dbaa gaqhf
                    else
624
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
625
                }
626
627
                if (i + 1 == lines.Count)
628
                {
629 809a7640 gaqhf
                    // 끝점에 연결된 Symbol 찾기
630 f1c9dbaa gaqhf
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
631 5e6ecf05 gaqhf
632
                    if (i != 0)
633
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
634
635 f1c9dbaa gaqhf
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
636
                    {
637 f2baa6a3 gaqhf
                        _LMSymbol2 = GetTargetSymbol(connItem as Symbol, line);
638 f1c9dbaa gaqhf
                        if (_LMSymbol2 != null)
639
                            placeRunInputs.AddSymbolTarget(_LMSymbol2, line.SPPID.END_X, line.SPPID.END_Y);
640
                        else
641
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
642
                            
643
                    }
644 335b7a24 gaqhf
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
645 5e6ecf05 gaqhf
                    {
646
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
647 56bc67e1 gaqhf
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
648 5e6ecf05 gaqhf
649
                        if (targetConnector2 != null)
650
                            placeRunInputs.AddConnectorTarget(targetConnector2, line.SPPID.END_X, line.SPPID.END_Y);
651
                        else
652 335b7a24 gaqhf
                        {
653
                            endBranchLine = connItem as Line;
654 5e6ecf05 gaqhf
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
655 335b7a24 gaqhf
                        }
656 5e6ecf05 gaqhf
                    }
657 f1c9dbaa gaqhf
                    else
658
                    {
659
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
660
                    }
661
                }
662
            }
663
664
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
665 335b7a24 gaqhf
666 f1c9dbaa gaqhf
            if (_lMConnector != null)
667
            {
668
                foreach (var line in lines)
669 5e6ecf05 gaqhf
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
670 f1c9dbaa gaqhf
                _lMConnector.Commit();
671 335b7a24 gaqhf
                if (startBranchLine != null || endBranchLine != null)
672
                {
673 3165c259 gaqhf
                    BranchLines.Add(new Tuple<string, Line, Line>(_lMConnector.ModelItemID, startBranchLine, endBranchLine));
674 335b7a24 gaqhf
                }
675 1b261371 gaqhf
            }
676 5e6ecf05 gaqhf
677 1b261371 gaqhf
678 f1c9dbaa gaqhf
            if (_LMSymbol1 != null)
679
                ReleaseCOMObjects(_LMSymbol1);
680
            if (_LMSymbol2 != null)
681
                ReleaseCOMObjects(_LMSymbol2);
682 5e6ecf05 gaqhf
            if (targetConnector1 != null)
683
                ReleaseCOMObjects(targetConnector1);
684
            if (targetConnector2 != null)
685
                ReleaseCOMObjects(targetConnector2);
686
            foreach (var item in connectorVertices1)
687
                ReleaseCOMObjects(item.Key);
688
            foreach (var item in connectorVertices2)
689
                ReleaseCOMObjects(item.Key);
690 1b261371 gaqhf
691 f1c9dbaa gaqhf
            ReleaseCOMObjects(_lMConnector);
692 1b261371 gaqhf
            ReleaseCOMObjects(placeRunInputs);
693
            ReleaseCOMObjects(_LMAItem);
694
        }
695
696 f2baa6a3 gaqhf
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
697
        {
698
            LMSymbol _LMSymbol = null;
699
            foreach (var connector in symbol.CONNECTORS)
700
            {
701
                if (connector.CONNECTEDITEM == line.UID)
702
                {
703
                    if (connector.Index == 0)
704
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
705
                    else
706
                    {
707
                        ChildSymbol child = null;
708
                        foreach (var childSymbol in symbol.ChildSymbols)
709
                        {
710
                            if (childSymbol.Connectors.Contains(connector))
711
                                child = childSymbol;
712
                            else
713
                                child = GetChildSymbolByConnector(childSymbol, connector);
714
715
                            if (child != null)
716
                                break;
717
                        }
718
719
                        if (child != null)
720
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
721
                    }
722
723
                    break;  
724
                }
725
            }
726
727
            return _LMSymbol;
728
        }
729
730
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
731
        {
732
            foreach (var childSymbol in item.ChildSymbols)
733
            {
734
                if (childSymbol.Connectors.Contains(connector))
735
                    return childSymbol;
736
                else
737
                    return GetChildSymbolByConnector(childSymbol, connector);
738
            }
739
740
            return null;
741
        }
742
743 335b7a24 gaqhf
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
744
        {
745
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
746
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
747
748 8b085570 gaqhf
            LMConnector _StartConnector = null;
749
            LMConnector _EndConnector = null;
750 335b7a24 gaqhf
            double lengthStart = double.MaxValue;
751
            double lengthEnd = double.MaxValue;
752
            List<double[]> startPoints = new List<double[]>();
753
            List<double[]> endPoints = new List<double[]>();
754
755
            foreach (var item in connectorVertices)
756
            {
757
                foreach (var point in item.Value)
758
                {
759
                    // Start Point가 Branch
760
                    if (branch.Item2 != null)
761
                    {
762
                        Line targetLine = branch.Item2;
763
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
764
                        if (lengthStart > distance)
765
                        {
766 8b085570 gaqhf
                            _StartConnector = item.Key;
767 335b7a24 gaqhf
                            lengthStart = distance;
768
                            startPoints = item.Value;
769
                        }
770
                    }
771
                    // End Point가 Branch
772
                    if (branch.Item3 != null)
773
                    {
774
                        Line targetLine = branch.Item3;
775
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
776
                        if (lengthEnd > distance)
777
                        {
778 8b085570 gaqhf
                            _EndConnector = item.Key;
779 335b7a24 gaqhf
                            lengthEnd = distance;
780
                            endPoints = item.Value;
781
                        }
782
                    }
783
                }
784
            }
785
            #region Branch가 양쪽 전부일 때
786 8b085570 gaqhf
            if (_StartConnector != null && _StartConnector == _EndConnector)
787 335b7a24 gaqhf
            {
788 8b085570 gaqhf
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
789 335b7a24 gaqhf
790
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
791
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
792
793
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
794 8b085570 gaqhf
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
795 335b7a24 gaqhf
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
796 8b085570 gaqhf
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
797 335b7a24 gaqhf
                   startPoints[startPoints.Count - 1][0],
798
                   startPoints[startPoints.Count - 1][1],
799
                   startPoints[startPoints.Count - 2][0],
800
                   startPoints[startPoints.Count - 2][1]);
801
802
                for (int i = 0; i < startPoints.Count; i++)
803
                {
804
                    double[] point = startPoints[i];
805
                    if (i == 0)
806 8b085570 gaqhf
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
807 335b7a24 gaqhf
                    else if (i == startPoints.Count - 1)
808 8b085570 gaqhf
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
809 335b7a24 gaqhf
                    else
810
                        placeRunInputs.AddPoint(point[0], point[1]);
811
                }
812
813
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
814
                if (_LMConnector != null)
815
                {
816
                    _LMConnector.Commit();
817
                    foreach (var item in lines)
818
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
819
                }
820
821
                foreach (var item in startConnectorVertices)
822
                    ReleaseCOMObjects(item.Key);
823
                foreach (var item in endConnectorVertices)
824
                    ReleaseCOMObjects(item.Key);
825
                ReleaseCOMObjects(placeRunInputs);
826
                ReleaseCOMObjects(_LMAItem);
827
                ReleaseCOMObjects(_LMConnector);
828
            }
829
            #endregion
830 8b085570 gaqhf
            #region 양쪽이 다른 Branch 
831 335b7a24 gaqhf
            else
832
            {
833
                // Branch 시작 Connector
834 8b085570 gaqhf
                if (_StartConnector != null)
835 6b298450 gaqhf
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
836 335b7a24 gaqhf
837 6b298450 gaqhf
                // Branch 끝 Connector
838
                if (_EndConnector != null)
839
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
840
            }
841
            #endregion
842 335b7a24 gaqhf
843 6b298450 gaqhf
            if (_StartConnector != null)
844
                ReleaseCOMObjects(_StartConnector);
845
            if (_EndConnector != null)
846
                ReleaseCOMObjects(_EndConnector);
847
            foreach (var item in connectorVertices)
848
                ReleaseCOMObjects(item.Key);
849
        }
850 335b7a24 gaqhf
851 6b298450 gaqhf
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
852
        {
853
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
854
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
855
            LMConnector _SameRunTargetConnector = null;
856
            LMSymbol _SameRunTargetSymbol = null;
857
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
858
            LMConnector _BranchTargetConnector = null;
859
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
860 335b7a24 gaqhf
861 6b298450 gaqhf
            // 같은 Line Run의 Connector 찾기
862
            foreach (var item in connectorVertices)
863
            {
864
                if (item.Key == _Connector)
865
                    continue;
866 335b7a24 gaqhf
867 6b298450 gaqhf
                if (IsStart &&
868
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
869
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
870
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
871
                {
872
                    _SameRunTargetConnector = item.Key;
873
                    break;
874
                }
875
                else if (!IsStart &&
876
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
877
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
878
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
879
                {
880
                    _SameRunTargetConnector = item.Key;
881
                    break;
882
                }
883
            }
884
885
            // Branch 반대편이 Symbol
886
            if (_SameRunTargetConnector == null)
887
            {
888
                foreach (var line in lines)
889
                {
890
                    foreach (var connector in line.CONNECTORS)
891 335b7a24 gaqhf
                    {
892 6b298450 gaqhf
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
893
                        if (symbol != null)
894 335b7a24 gaqhf
                        {
895 6b298450 gaqhf
                            _SameRunTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
896
                            break;
897 335b7a24 gaqhf
                        }
898
                    }
899 6b298450 gaqhf
                }
900
            }
901 335b7a24 gaqhf
902 6b298450 gaqhf
            // 기존 Connector 제거
903
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
904
            
905
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
906
            if (IsStart)
907
            {
908
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
909
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
910
            }
911
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
912
            else
913
            {
914
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
915
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
916
                    points[points.Count - 1][0],
917
                    points[points.Count - 1][1],
918
                    points[points.Count - 2][0],
919
                    points[points.Count - 2][1]);
920
            }
921 335b7a24 gaqhf
922 6b298450 gaqhf
            for (int i = 0; i < points.Count; i++)
923
            {
924
                double[] point = points[i];
925
                if (i == 0)
926 335b7a24 gaqhf
                {
927 6b298450 gaqhf
                    if (IsStart)
928 335b7a24 gaqhf
                    {
929 6b298450 gaqhf
                        placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
930 335b7a24 gaqhf
                    }
931 6b298450 gaqhf
                    else
932 335b7a24 gaqhf
                    {
933 6b298450 gaqhf
                        if (_SameRunTargetConnector != null)
934
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
935 65a1ed4b gaqhf
                        else if (_SameRunTargetSymbol != null)
936 6b298450 gaqhf
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
937
                        else
938
                            placeRunInputs.AddPoint(point[0], point[1]);
939 335b7a24 gaqhf
                    }
940 6b298450 gaqhf
                }
941
                else if (i == points.Count - 1)
942
                {
943
                    if (IsStart)
944 335b7a24 gaqhf
                    {
945 6b298450 gaqhf
                        if (_SameRunTargetConnector != null)
946
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
947
                        else if (_SameRunTargetSymbol != null)
948
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
949 335b7a24 gaqhf
                        else
950
                            placeRunInputs.AddPoint(point[0], point[1]);
951
                    }
952 6b298450 gaqhf
                    else
953 335b7a24 gaqhf
                    {
954 f9125a54 gaqhf
                        if (_BranchTargetConnector != null)
955
                        {
956
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
957
                        }
958 335b7a24 gaqhf
                    }
959 6b298450 gaqhf
                }
960
                else
961
                    placeRunInputs.AddPoint(point[0], point[1]);
962
            }
963
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
964
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
965 335b7a24 gaqhf
966 6b298450 gaqhf
            if (_LMConnector != null)
967
            {
968
                if (_SameRunTargetConnector != null)
969
                {
970
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
971
                }
972
                else
973
                {
974
                    foreach (var item in lines)
975
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
976 335b7a24 gaqhf
                }
977
978 6b298450 gaqhf
                _LMConnector.Commit();
979
                ReleaseCOMObjects(_LMConnector);
980 335b7a24 gaqhf
            }
981
982 6b298450 gaqhf
            ReleaseCOMObjects(placeRunInputs);
983
            ReleaseCOMObjects(_LMAItem);
984
            if (_BranchTargetConnector != null)
985
                ReleaseCOMObjects(_BranchTargetConnector);
986
            if (_SameRunTargetConnector != null)
987
                ReleaseCOMObjects(_SameRunTargetConnector);
988
            if (_SameRunTargetSymbol != null)
989
                ReleaseCOMObjects(_SameRunTargetSymbol);
990 335b7a24 gaqhf
            foreach (var item in connectorVertices)
991
                ReleaseCOMObjects(item.Key);
992 6b298450 gaqhf
            foreach (var item in branchConnectorVertices)
993
                ReleaseCOMObjects(item.Key);
994 335b7a24 gaqhf
        }
995
996 3165c259 gaqhf
        private void EndBreakModeling(EndBreak endBreak)
997 335b7a24 gaqhf
        {
998 10c7195c gaqhf
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
999 2a4872ec gaqhf
            LMConnector targetLMConnector = null;
1000 10c7195c gaqhf
            if (ownerObj !=null && ownerObj.GetType() == typeof(Line))
1001 335b7a24 gaqhf
            {
1002 10c7195c gaqhf
                Line ownerLine = ownerObj as Line;
1003 3165c259 gaqhf
                LMLabelPersist _LmLabelPersist = null;
1004 ac78b508 gaqhf
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(ownerLine.SPPID.ModelItemId);
1005 3165c259 gaqhf
1006 2a4872ec gaqhf
                targetLMConnector = FindTargetLMConnectorByPoint(connectorVertices, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1007 b9e9f4c8 gaqhf
                
1008 2a4872ec gaqhf
                if (targetLMConnector != null)
1009 335b7a24 gaqhf
                {
1010 c3d2e266 gaqhf
                    //double[] point = connectorVertices[targetLMConnector][connectorVertices[targetLMConnector].Count - 1];
1011
                    //Array array = new double[] { 0, point[0], point[1] };
1012 ac78b508 gaqhf
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1013 2a4872ec gaqhf
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1014 ac78b508 gaqhf
                }
1015 3165c259 gaqhf
1016 ac78b508 gaqhf
                if (_LmLabelPersist != null)
1017
                {
1018
                    _LmLabelPersist.Commit();
1019
                    ReleaseCOMObjects(_LmLabelPersist);
1020 335b7a24 gaqhf
                }
1021 b9e9f4c8 gaqhf
                else
1022 2a4872ec gaqhf
                    RetryEndBreakModeling(endBreak, targetLMConnector);
1023 b9e9f4c8 gaqhf
1024 ac78b508 gaqhf
                foreach (var item in connectorVertices)
1025
                    ReleaseCOMObjects(item.Key);
1026 2a4872ec gaqhf
1027 335b7a24 gaqhf
            }
1028 10c7195c gaqhf
            else if (ownerObj != null && ownerObj.GetType() == typeof(Symbol))
1029
            {
1030
                Symbol ownerSymbol = ownerObj as Symbol;
1031
                LMSymbol _LMSymbol = dataSource.GetSymbol(ownerSymbol.SPPID.RepresentationId);
1032
1033 2a4872ec gaqhf
                targetLMConnector = null;
1034 10c7195c gaqhf
                double distance = double.MaxValue;
1035 b9e9f4c8 gaqhf
1036 10c7195c gaqhf
                foreach (LMConnector connector in _LMSymbol.Avoid1Connectors)
1037
                {
1038 26c6f818 gaqhf
                    if (connector.get_ItemStatus() == "Active")
1039 10c7195c gaqhf
                    {
1040 26c6f818 gaqhf
                        dynamic OID = connector.get_GraphicOID();
1041
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1042
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1043
                        int verticesCount = lineStringGeometry.VertexCount;
1044
                        double[] vertices = null;
1045
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1046
                        for (int i = 0; i < verticesCount; i++)
1047 10c7195c gaqhf
                        {
1048 26c6f818 gaqhf
                            double x = 0;
1049
                            double y = 0;
1050
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1051
1052 b9e9f4c8 gaqhf
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1053 26c6f818 gaqhf
                            if (result < distance)
1054
                            {
1055
                                targetLMConnector = connector;
1056
                                distance = result;
1057
                            }
1058 10c7195c gaqhf
                        }
1059
                    }
1060
                }
1061
1062
                foreach (LMConnector connector in _LMSymbol.Avoid2Connectors)
1063
                {
1064 b9e9f4c8 gaqhf
                    if (connector.get_ItemStatus() == "Active")
1065 10c7195c gaqhf
                    {
1066 b9e9f4c8 gaqhf
                        dynamic OID = connector.get_GraphicOID();
1067
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1068
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1069
                        int verticesCount = lineStringGeometry.VertexCount;
1070
                        double[] vertices = null;
1071
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1072
                        for (int i = 0; i < verticesCount; i++)
1073 10c7195c gaqhf
                        {
1074 b9e9f4c8 gaqhf
                            double x = 0;
1075
                            double y = 0;
1076
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1077
1078
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1079
                            if (result < distance)
1080
                            {
1081
                                targetLMConnector = connector;
1082
                                distance = result;
1083
                            }
1084 10c7195c gaqhf
                        }
1085
                    }
1086
                }
1087
1088
                if (targetLMConnector != null)
1089
                {
1090
                    LMLabelPersist _LmLabelPersist = null;
1091
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1092
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1093
                    if (_LmLabelPersist != null)
1094
                    {
1095
                        _LmLabelPersist.Commit();
1096
                        ReleaseCOMObjects(_LmLabelPersist);
1097
                    }
1098 b9e9f4c8 gaqhf
                    else
1099 2a4872ec gaqhf
                        RetryEndBreakModeling(endBreak, targetLMConnector);
1100
                }
1101
                
1102
                ReleaseCOMObjects(_LMSymbol);
1103
            }
1104
        }
1105 b9e9f4c8 gaqhf
1106 2a4872ec gaqhf
        private void RetryEndBreakModeling(EndBreak endBreak, LMConnector targetLMConnector)
1107
        {
1108
            bool isZeroLength = Convert.ToBoolean(targetLMConnector.get_IsZeroLength());
1109
            Array array = null;
1110
            LMLabelPersist _LMLabelPersist = null;
1111
            LMConnector _LMConnector = null;
1112
            dynamic OID = targetLMConnector.get_GraphicOID();
1113
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1114
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1115
            int verticesCount = lineStringGeometry.VertexCount;
1116
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1117
            _LMAItem _LMAItem = _placement.PIDCreateItem(@"\Piping\Routing\Process Lines\Primary Piping.sym");
1118 b9e9f4c8 gaqhf
1119 2a4872ec gaqhf
            if (isZeroLength)
1120
            {
1121
                double[] vertices = null;
1122
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1123
                double x = 0;
1124
                double y = 0;
1125
                lineStringGeometry.GetVertex(1, ref x, ref y);
1126 b9e9f4c8 gaqhf
1127 2a4872ec gaqhf
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, x, y);
1128
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, x, y);
1129 b9e9f4c8 gaqhf
1130 2a4872ec gaqhf
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1131
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1132
1133
                array = new double[] { 0, x, y };
1134
                _LMLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: _LMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1135 5a83fda2 gaqhf
1136
                AutoJoinPipeRun(_LMConnector.ModelItemID);
1137 2a4872ec gaqhf
            }
1138
            else
1139
            {
1140
                List<double[]> vertices = new List<double[]>();
1141
                for (int i = 1; i <= verticesCount; i++)
1142
                {
1143
                    double x = 0;
1144
                    double y = 0;
1145
                    lineStringGeometry.GetVertex(i, ref x, ref y);
1146
                    vertices.Add(new double[] { x, y });
1147
                }
1148
1149
                for (int i = 0; i < vertices.Count; i++)
1150
                {
1151
                    double[] points = vertices[i];
1152
                    if (i == 0)
1153
                    {
1154
                        if (targetLMConnector.ConnectItem1SymbolObject != null)
1155
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1156
                        else
1157
                            placeRunInputs.AddPoint(points[0], points[1]);
1158 b9e9f4c8 gaqhf
                    }
1159 2a4872ec gaqhf
                    else if (i == vertices.Count - 1)
1160
                    {
1161
                        if (targetLMConnector.ConnectItem2SymbolObject != null)
1162
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1163
                        else
1164
                            placeRunInputs.AddPoint(points[0], points[1]);
1165
                    }
1166
                    else
1167
                        placeRunInputs.AddPoint(points[0], points[1]);
1168 10c7195c gaqhf
                }
1169 2a4872ec gaqhf
1170
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, targetLMConnector.ModelItemID);
1171 b9e9f4c8 gaqhf
                
1172 2a4872ec gaqhf
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1173
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1174
1175
                foreach (var line in lines)
1176
                    line.SPPID.ModelItemId = _LMConnector.ModelItemID;
1177
1178
                array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1179
                _LMLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: _LMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1180 10c7195c gaqhf
            }
1181 2a4872ec gaqhf
1182
1183
            if (_LMLabelPersist != null)
1184
            {
1185
                _LMLabelPersist.Commit();
1186
                ReleaseCOMObjects(_LMLabelPersist);
1187
            }
1188
            else
1189
            {
1190
                
1191
            }
1192
1193
            ReleaseCOMObjects(_LMConnector);
1194
            ReleaseCOMObjects(placeRunInputs);
1195
            ReleaseCOMObjects(_LMAItem);
1196 3165c259 gaqhf
        }
1197 10872260 gaqhf
1198 335b7a24 gaqhf
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
1199
        {
1200 310aeb31 gaqhf
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
1201
            _LMAItem item1 = modelItem1.AsLMAItem();
1202
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
1203
            _LMAItem item2 = modelItem2.AsLMAItem();
1204
            
1205 335b7a24 gaqhf
            // item2가 item1으로 조인
1206
            try
1207
            {
1208
                _placement.PIDJoinRuns(ref item1, ref item2);
1209 65a1ed4b gaqhf
                item1.Commit();
1210
                item2.Commit();
1211 335b7a24 gaqhf
            }
1212
            catch (Exception ex)
1213
            {
1214
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1215
            }
1216
            finally
1217
            {
1218 310aeb31 gaqhf
                ReleaseCOMObjects(modelItem1);
1219
                ReleaseCOMObjects(item1);
1220
                ReleaseCOMObjects(modelItem2);
1221
                ReleaseCOMObjects(item2);
1222 335b7a24 gaqhf
            }
1223
        }
1224
1225
        private void AutoJoinPipeRun(string modelItemId)
1226
        {
1227 310aeb31 gaqhf
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
1228
            _LMAItem item = modelItem.AsLMAItem();
1229 65a1ed4b gaqhf
            try
1230
            {
1231
                string modelitemID = item.Id;
1232
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
1233
                string afterModelItemID = item.Id;
1234 5a83fda2 gaqhf
                
1235 65a1ed4b gaqhf
                if (modelitemID != afterModelItemID)
1236
                {
1237
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
1238
                    foreach (var line in lines)
1239
                        line.SPPID.ModelItemId = afterModelItemID;
1240
                }
1241
                item.Commit();
1242
            }
1243
            catch (Exception ex)
1244
            {
1245
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1246
            }
1247
            finally
1248
            {
1249 310aeb31 gaqhf
                ReleaseCOMObjects(modelItem);
1250
                ReleaseCOMObjects(item);
1251 65a1ed4b gaqhf
            }
1252 335b7a24 gaqhf
        }
1253
1254
        private void JoinRunLine(LineRun run)
1255
        {
1256
            string modelItemId = string.Empty;
1257
            foreach (var item in run.RUNITEMS)
1258
            {
1259
                if (item.GetType() == typeof(Line))
1260
                {
1261
                    Line line = item as Line;
1262
                    if (modelItemId != line.SPPID.ModelItemId)
1263
                    {
1264
                        AutoJoinPipeRun(line.SPPID.ModelItemId);
1265
                        modelItemId = line.SPPID.ModelItemId;
1266
                    }
1267
                }
1268
            }
1269
        }
1270
1271 5e6ecf05 gaqhf
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
1272
        {
1273
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
1274 310aeb31 gaqhf
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
1275
1276
            if (modelItem != null)
1277 5e6ecf05 gaqhf
            {
1278 310aeb31 gaqhf
                foreach (LMRepresentation rep in modelItem.Representations)
1279 5e6ecf05 gaqhf
                {
1280
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1281
                    {
1282
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1283
                        connectorVertices.Add(_LMConnector, new List<double[]>());
1284
                        dynamic OID = rep.get_GraphicOID();
1285 335b7a24 gaqhf
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1286 5e6ecf05 gaqhf
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1287
                        int verticesCount = lineStringGeometry.VertexCount;
1288
                        double[] vertices = null;
1289
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1290
                        for (int i = 0; i < verticesCount; i++)
1291
                        {
1292
                            double x = 0;
1293
                            double y = 0;
1294
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1295 335b7a24 gaqhf
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
1296 5e6ecf05 gaqhf
                        }
1297
                    }
1298
                }
1299
1300 310aeb31 gaqhf
                ReleaseCOMObjects(modelItem);
1301 5e6ecf05 gaqhf
            }
1302
1303
            return connectorVertices;
1304
        }
1305
1306 56bc67e1 gaqhf
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
1307 5e6ecf05 gaqhf
        {
1308
            double length = double.MaxValue;
1309
            LMConnector targetConnector = null;
1310
            foreach (var item in connectorVertices)
1311
            {
1312
                List<double[]> points = item.Value;
1313
                for (int i = 0; i < points.Count - 1; i++)
1314
                {
1315
                    double[] point1 = points[i];
1316
                    double[] point2 = points[i + 1];
1317
1318 335b7a24 gaqhf
                    double maxLineX = Math.Max(point1[0], point2[0]);
1319
                    double minLineX = Math.Min(point1[0], point2[0]);
1320
                    double maxLineY = Math.Max(point1[1], point2[1]);
1321
                    double minLineY = Math.Min(point1[1], point2[1]);
1322
1323
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
1324
1325 56bc67e1 gaqhf
                    // 두직선의 교차점
1326
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
1327
                    if (crossingPoint != null)
1328 5e6ecf05 gaqhf
                    {
1329 30a9ffce gaqhf
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
1330 335b7a24 gaqhf
                        if (length >= distance)
1331 30a9ffce gaqhf
                        {
1332 335b7a24 gaqhf
                            if (slope == SlopeType.Slope &&
1333
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
1334
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1335
                            {
1336
                                targetConnector = item.Key;
1337
                                length = distance;
1338
                            }
1339
                            else if (slope == SlopeType.HORIZONTAL &&
1340
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
1341
                            {
1342
                                targetConnector = item.Key;
1343
                                length = distance;
1344
                            }
1345
                            else if (slope == SlopeType.VERTICAL &&
1346
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1347
                            {
1348
                                targetConnector = item.Key;
1349
                                length = distance;
1350
                            }
1351 30a9ffce gaqhf
                        }
1352 5e6ecf05 gaqhf
                    }
1353
                }
1354 c3d2e266 gaqhf
1355
1356
            }
1357
1358
            if (targetConnector == null)
1359
            {
1360
                foreach (var item in connectorVertices)
1361
                {
1362
                    List<double[]> points = item.Value;
1363
                    foreach (var point in points)
1364
                    {
1365
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, point[0], point[1]);
1366
                        if (length >= distance)
1367
                        {
1368
                            targetConnector = item.Key;
1369
                            length = distance;
1370
                        }
1371
                    }
1372
                }
1373
1374 5e6ecf05 gaqhf
            }
1375
1376
            return targetConnector;
1377
        }
1378
1379 ac78b508 gaqhf
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1380
        {
1381
            double length = double.MaxValue;
1382
            LMConnector targetConnector = null;
1383
            foreach (var item in connectorVertices)
1384
            {
1385
                List<double[]> points = item.Value;
1386
1387
                foreach (double[] point in points)
1388
                {
1389
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1390
                    if (length >= distance)
1391
                    {
1392
                        targetConnector = item.Key;
1393
                        length = distance;
1394
                    }
1395
                }
1396
            }
1397
1398
            return targetConnector;
1399
        }
1400
1401 68464385 gaqhf
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1402
        {
1403
            double length = double.MaxValue;
1404
            LMConnector targetConnector = null;
1405
            foreach (var item in connectorVertices)
1406
            {
1407
                List<double[]> points = item.Value;
1408
                for (int i = 0; i < points.Count - 1; i++)
1409
                {
1410
                    double[] point1 = points[i];
1411
                    double[] point2 = points[i + 1];
1412
                    double x1 = Math.Min(point1[0], point2[0]);
1413
                    double y1 = Math.Min(point1[1], point2[1]);
1414
                    double x2 = Math.Max(point1[0], point2[0]);
1415
                    double y2 = Math.Max(point1[1], point2[1]);
1416
1417
                    if ((x1 <= connX && x2 >= connX) ||
1418
                        (y1 <= connY && y2 >= connY))
1419
                    {
1420
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
1421
                        if (length >= distance)
1422
                        {
1423
                            targetConnector = item.Key;
1424
                            length = distance;
1425
                        }
1426
1427
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
1428
                        if (length >= distance)
1429
                        {
1430
                            targetConnector = item.Key;
1431
                            length = distance;
1432
                        }
1433
                    }
1434
                }
1435
            }
1436
1437
            // 못찾았을때.
1438
            length = double.MaxValue;
1439
            if (targetConnector == null)
1440
            {
1441
                foreach (var item in connectorVertices)
1442
                {
1443
                    List<double[]> points = item.Value;
1444
1445
                    foreach (double[] point in points)
1446
                    {
1447
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1448
                        if (length >= distance)
1449
                        {
1450
                            targetConnector = item.Key;
1451
                            length = distance;
1452
                        }
1453
                    }
1454
                }
1455
            }
1456
1457
            return targetConnector;
1458
        }
1459
1460 cfda1fed gaqhf
        private void LineNumberModeling(LineNumber lineNumber)
1461
        {
1462 f4880c6a gaqhf
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
1463
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
1464 68464385 gaqhf
            LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
1465 f4880c6a gaqhf
            if (connectedLMConnector != null)
1466 10872260 gaqhf
            {
1467 b65a7e32 gaqhf
                double x = 0;
1468
                double y = 0;
1469
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
1470
1471
                Array points = new double[] { 0, x, y };
1472 f4880c6a gaqhf
                LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: false);
1473 c3d2e266 gaqhf
                
1474 cfda1fed gaqhf
1475 f4880c6a gaqhf
                foreach (var item in connectorVertices)
1476
                    ReleaseCOMObjects(item.Key);
1477
                if (_LmLabelPresist != null)
1478
                {
1479 c3d2e266 gaqhf
                    _LmLabelPresist.Commit();
1480 f4880c6a gaqhf
                    lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
1481
                    ReleaseCOMObjects(_LmLabelPresist);
1482 10872260 gaqhf
                }
1483 c3d2e266 gaqhf
                else
1484
                {
1485
1486
                }
1487 10872260 gaqhf
            }
1488 cfda1fed gaqhf
        }
1489
1490 a7e9beec gaqhf
        private void InputLineNumberAttribute(LineNumber lineNumber)
1491
        {
1492 8634af60 gaqhf
            foreach (LineRun run in lineNumber.RUNS)
1493 a7e9beec gaqhf
            {
1494 8634af60 gaqhf
                foreach (var item in run.RUNITEMS)
1495 a7e9beec gaqhf
                {
1496 8634af60 gaqhf
                    if (item.GetType() == typeof(Symbol))
1497 a7e9beec gaqhf
                    {
1498 8634af60 gaqhf
                        Symbol symbol = item as Symbol;
1499
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1500
                        LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1501
1502
                        if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
1503
                        {
1504
                            foreach (var attribute in lineNumber.ATTRIBUTES)
1505
                            {
1506
                                LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1507
                                if (mapping != null)
1508
                                {
1509
                                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1510
                                    if (_LMAAttribute != null)
1511
                                    {
1512
                                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1513
                                            _LMAAttribute.set_Value(attribute.VALUE);
1514
                                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
1515
                                            _LMAAttribute.set_Value(attribute.VALUE);
1516
                                    }
1517
                                }
1518
                            }
1519
                            _LMModelItem.Commit();
1520
                        }
1521
                        if (_LMModelItem != null)
1522
                            ReleaseCOMObjects(_LMModelItem);
1523
                        if (_LMSymbol != null)
1524
                            ReleaseCOMObjects(_LMSymbol);
1525
                    }
1526
                    else if (item.GetType() == typeof(Line))
1527
                    {
1528
                        Line line = item as Line;
1529
                        if (line != null)
1530 a7e9beec gaqhf
                        {
1531 8634af60 gaqhf
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1532
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
1533 a7e9beec gaqhf
                            {
1534 8634af60 gaqhf
                                foreach (var attribute in lineNumber.ATTRIBUTES)
1535
                                {
1536
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1537
                                    if (mapping != null)
1538
                                    {
1539
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1540
                                        if (_LMAAttribute != null)
1541
                                        {
1542
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1543
                                                _LMAAttribute.set_Value(attribute.VALUE);
1544
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
1545
                                                _LMAAttribute.set_Value(attribute.VALUE);
1546
                                            
1547
                                        }
1548
                                    }
1549
                                }
1550 68464385 gaqhf
                                _LMModelItem.Commit();
1551 a7e9beec gaqhf
                            }
1552 8634af60 gaqhf
                            if (_LMModelItem != null)
1553
                                ReleaseCOMObjects(_LMModelItem);
1554 a7e9beec gaqhf
                        }
1555
                    }
1556
                }
1557
            }
1558
        }
1559
1560 1efc25a3 gaqhf
        private void InputSymbolAttribute(Symbol symbol)
1561
        {
1562 ac78b508 gaqhf
            try
1563 1efc25a3 gaqhf
            {
1564 ac78b508 gaqhf
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1565 402ef5b1 gaqhf
                {
1566 ac78b508 gaqhf
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1567 310aeb31 gaqhf
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1568 ea80efaa gaqhf
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
1569 402ef5b1 gaqhf
1570 ac78b508 gaqhf
                    foreach (var item in symbol.PROPERTIES)
1571
                    {
1572 1efc25a3 gaqhf
1573
1574 ac78b508 gaqhf
                    }
1575
1576
                    foreach (var item in symbol.ATTRIBUTES)
1577 65a1ed4b gaqhf
                    {
1578 ac78b508 gaqhf
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
1579 26c6f818 gaqhf
                        if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
1580 ac78b508 gaqhf
                        {
1581
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
1582
                            if (_Attribute != null)
1583 8b069d9f gaqhf
                            {
1584
                                // 임시
1585
                                if (item.ATTRIBUTETYPE == "String")
1586
                                {
1587
                                    if (!string.IsNullOrEmpty(item.VALUE))
1588
                                    {
1589
                                        if (!DBNull.Value.Equals(_Attribute.get_Value()) && !string.IsNullOrEmpty(_Attribute.get_Value()))
1590
                                        {
1591
                                            string value = _Attribute.get_Value() + "\n" + item.VALUE;
1592
                                            _Attribute.set_Value(value);
1593
                                        }
1594
                                        else
1595
                                        {
1596
                                            _Attribute.set_Value(item.VALUE);
1597
                                        }
1598
                                    }
1599
                                }
1600
                                else
1601
                                {
1602
                                    _Attribute.set_Value(item.VALUE);
1603
                                }
1604
                            }
1605 ac78b508 gaqhf
                        }
1606 65a1ed4b gaqhf
                    }
1607 1efc25a3 gaqhf
1608 ac78b508 gaqhf
                    foreach (var item in symbol.ASSOCIATIONS)
1609
                    {
1610 1efc25a3 gaqhf
1611 ac78b508 gaqhf
                    }
1612 402ef5b1 gaqhf
1613 ac78b508 gaqhf
                    ReleaseCOMObjects(_LMSymbol);
1614 ea80efaa gaqhf
                    ReleaseCOMObjects(_Attributes);
1615
                    ReleaseCOMObjects(_LMModelItem);
1616 ac78b508 gaqhf
                }
1617
            }
1618
            catch (Exception ex)
1619
            {
1620
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1621 1efc25a3 gaqhf
            }
1622
        }
1623
1624 cfda1fed gaqhf
        private void TextModeling(Text text)
1625
        {
1626 6b298450 gaqhf
            LMSymbol _LMSymbol = null;
1627
            try
1628
            {
1629 8b069d9f gaqhf
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
1630
                if (text.ASSOCIATION)
1631 ea80efaa gaqhf
                {
1632
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
1633
                    if (owner.GetType() == typeof(Symbol))
1634
                    {
1635
                        Symbol symbol = owner as Symbol;
1636
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1637
                        if (_LMSymbol != null)
1638
                        {
1639
                            Association association = symbol.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
1640
                            List<BaseModel.Attribute> attributes = symbol.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
1641
                            AttributeMapping mapping = null;
1642
                            foreach (var attribute in attributes)
1643
                            {
1644 26c6f818 gaqhf
                                if (string.IsNullOrEmpty(attribute.VALUE) || attribute.VALUE == "None")
1645
                                    continue;
1646
1647 ea80efaa gaqhf
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
1648
                                if (mapping != null)
1649
                                    break;  
1650
                            }
1651
1652
                            if (mapping != null)
1653
                            {
1654 1a3a74a8 gaqhf
                                double x = 0;
1655
                                double y = 0;
1656
1657 b65a7e32 gaqhf
                                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, mapping.Location);
1658
                                Array array = new double[] { 0, x, y };
1659 1a3a74a8 gaqhf
1660 ea80efaa gaqhf
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: true);
1661
                                if (_LMLabelPersist!=null)
1662
                                {
1663 f9125a54 gaqhf
                                    _LMLabelPersist.Commit();
1664 ea80efaa gaqhf
                                    ReleaseCOMObjects(_LMLabelPersist);
1665
                                }
1666
                            }
1667
                        }
1668
                    }
1669
                    else if (owner.GetType() == typeof(Line))
1670
                    {
1671
1672
                    }
1673
                }
1674
                else
1675
                {
1676
                    LMItemNote _LMItemNote = null;
1677
                    LMAAttribute _LMAAttribute = null;
1678
1679 b65a7e32 gaqhf
                    double x = 0;
1680
                    double y = 0;
1681
1682
                    CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
1683
1684
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y);
1685 ea80efaa gaqhf
                    _LMSymbol.Commit();
1686
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1687
                    _LMItemNote.Commit();
1688
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1689
                    _LMAAttribute.set_Value(text.VALUE);
1690
                    _LMItemNote.Commit();
1691
1692
                    if (_LMAAttribute != null)
1693
                        ReleaseCOMObjects(_LMAAttribute);
1694
                    if (_LMItemNote != null)
1695
                        ReleaseCOMObjects(_LMItemNote);
1696
                }
1697 6b298450 gaqhf
            }
1698
            catch (Exception ex)
1699
            {
1700 cfda1fed gaqhf
1701 6b298450 gaqhf
            }
1702
            finally
1703
            {
1704
                if (_LMSymbol != null)
1705
                    ReleaseCOMObjects(_LMSymbol);
1706
            }
1707 cfda1fed gaqhf
        }
1708
1709
        private void NoteModeling(Note note)
1710
        {
1711 6b298450 gaqhf
            LMSymbol _LMSymbol = null;
1712
            LMItemNote _LMItemNote = null;
1713
            LMAAttribute _LMAAttribute = null;
1714
1715
            try
1716
            {
1717 b65a7e32 gaqhf
                double x = 0;
1718
                double y = 0;
1719
1720
                CalcLabelLocation(ref x, ref y, note.SPPID.ORIGINAL_X, note.SPPID.ORIGINAL_Y, note.SPPIDLabelLocation, _ETCSetting.NoteLocation);
1721
1722
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
1723 6b298450 gaqhf
                _LMSymbol.Commit();
1724
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1725
                _LMItemNote.Commit();
1726
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1727
                _LMAAttribute.set_Value(note.VALUE);
1728
                _LMItemNote.Commit();
1729
            }
1730
            catch (Exception ex)
1731
            {
1732 cfda1fed gaqhf
1733 6b298450 gaqhf
            }
1734
            finally
1735
            {
1736
                if (_LMAAttribute != null)
1737
                    ReleaseCOMObjects(_LMAAttribute);
1738
                if (_LMItemNote != null)
1739
                    ReleaseCOMObjects(_LMItemNote);
1740
                if (_LMSymbol != null)
1741
                    ReleaseCOMObjects(_LMSymbol);
1742
            }
1743
            
1744 cfda1fed gaqhf
        }
1745
1746 b65a7e32 gaqhf
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
1747
        {
1748
            if (location == Location.None)
1749
            {
1750
                x = originX;
1751
                y = originY;
1752
            }
1753
            else
1754
            {
1755
                if (location.HasFlag(Location.Center))
1756
                {
1757
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
1758
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
1759
                }
1760
1761
                if (location.HasFlag(Location.Left))
1762
                    x = SPPIDLabelLocation.X1;
1763
                else if (location.HasFlag(Location.Right))
1764
                    x = SPPIDLabelLocation.X2;
1765
1766
                if (location.HasFlag(Location.Down))
1767
                    y = SPPIDLabelLocation.Y1;
1768
                else if (location.HasFlag(Location.Up))
1769
                    y = SPPIDLabelLocation.Y2;
1770
            }
1771
        }
1772 5a4b8f32 gaqhf
1773
        public void ReleaseCOMObjects(params object[] objVars)
1774
        {
1775
            int intNewRefCount = 0;
1776
            foreach (object obj in objVars)
1777
            {
1778
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
1779
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
1780
            }
1781
        }
1782 cfda1fed gaqhf
    }
1783
}
클립보드 이미지 추가 (최대 크기: 500 MB)