프로젝트

일반

사용자정보

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

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 229cfccb

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