프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 3939eebf

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