프로젝트

일반

사용자정보

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

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 1efc25a3

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

1 cfda1fed gaqhf
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using Llama;
7
using Plaice;
8 8aa6f2db gaqhf
using Ingr.RAD2D.Interop.RAD2D;
9
using Ingr.RAD2D.Internal;
10
using Ingr.RAD2D.Helper;
11 cfda1fed gaqhf
using Converter.BaseModel;
12
using Converter.SPPID.Model;
13
using Converter.SPPID.Properties;
14
using Converter.SPPID.Util;
15
using Converter.SPPID.DB;
16 5e6ecf05 gaqhf
using Ingr.RAD2D.MacroControls.CmdCtrl;
17
using Ingr.RAD2D;
18 5dfb8a24 gaqhf
using System.Windows;
19 cfda1fed gaqhf
using System.Threading;
20 5dfb8a24 gaqhf
using System.Drawing;
21 cfda1fed gaqhf
using Microsoft.VisualBasic;
22
using Newtonsoft.Json;
23
24
namespace Converter.SPPID
25
{
26
    public class AutoModeling
27
    {
28 809a7640 gaqhf
        Placement _placement;
29
        LMADataSource dataSource;
30 5e6ecf05 gaqhf
        Ingr.RAD2D.Application radApp;
31 cfda1fed gaqhf
        SPPID_Document document;
32 f1c9dbaa gaqhf
33 3165c259 gaqhf
        List<Tuple<string, Line, Line>> BranchLines = new List<Tuple<string, Line, Line>>();
34 5e6ecf05 gaqhf
        public AutoModeling(SPPID_Document document, Ingr.RAD2D.Application radApp)
35 cfda1fed gaqhf
        {
36
            this.document = document;
37 5e6ecf05 gaqhf
            this.radApp = radApp;
38 cfda1fed gaqhf
        }
39
40
        public void Run()
41
        {
42 809a7640 gaqhf
            _placement = new Placement();
43
            dataSource = _placement.PIDDataSource;
44 5dfb8a24 gaqhf
            //dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
45
            //dynamic newDrawing = application.Drawings.Add(document.Unit, document.Template, document.DrawingNumber, document.DrawingName);
46
            //application.ActiveWindow.Fit();
47
            //Thread.Sleep(100);
48
            //application.ActiveWindow.Zoom = 60;
49
            //Thread.Sleep(100);
50 5e6ecf05 gaqhf
            try
51 5dfb8a24 gaqhf
            {
52 1efc25a3 gaqhf
                // Equipment Modeling
53 5e6ecf05 gaqhf
                foreach (Equipment equipment in document.Equipments)
54 5dfb8a24 gaqhf
                {
55 5e6ecf05 gaqhf
                    SymbolModeling(equipment as Symbol, null, null);
56 8aa6f2db gaqhf
                }
57 1efc25a3 gaqhf
                // LineRun Symbol Modeling
58 5e6ecf05 gaqhf
                foreach (LineNumber lineNumber in document.LINENUMBERS)
59
                {
60
                    foreach (LineRun run in lineNumber.RUNS)
61
                    {
62
                        SymbolModelingByRun(run);
63
                    }
64
                }
65 1efc25a3 gaqhf
                // TrimLineRun Symbol Modeling
66 5e6ecf05 gaqhf
                foreach (TrimLine trimLine in document.TRIMLINES)
67 8aa6f2db gaqhf
                {
68 5e6ecf05 gaqhf
                    foreach (LineRun run in trimLine.RUNS)
69
                    {
70
                        SymbolModelingByRun(run);
71
                    }
72 8aa6f2db gaqhf
                }
73 1efc25a3 gaqhf
                // LineRun Line Modeling
74 5e6ecf05 gaqhf
                foreach (LineNumber lineNumber in document.LINENUMBERS)
75 8aa6f2db gaqhf
                {
76 5e6ecf05 gaqhf
                    foreach (LineRun run in lineNumber.RUNS)
77
                    {
78
                        LineModelingByRun(run);
79
                    }
80 8aa6f2db gaqhf
                }
81 1efc25a3 gaqhf
                // TrimLineRun Line Modeling
82 5e6ecf05 gaqhf
                foreach (TrimLine trimLine in document.TRIMLINES)
83 8aa6f2db gaqhf
                {
84 5e6ecf05 gaqhf
                    foreach (LineRun run in trimLine.RUNS)
85
                    {
86
                        LineModelingByRun(run);
87
                    }
88 809a7640 gaqhf
                }
89 1efc25a3 gaqhf
                // Branch Line Modeling
90 3165c259 gaqhf
                foreach (var item in BranchLines)
91 335b7a24 gaqhf
                {
92
                    BranchLineModeling(item);
93
                }
94 1efc25a3 gaqhf
                // EndBreak Modeling
95 3165c259 gaqhf
                foreach (var item in document.EndBreaks)
96
                {
97
                    EndBreakModeling(item);
98
                }
99 1efc25a3 gaqhf
                // LineNumber Modeling
100 10872260 gaqhf
                foreach (var item in document.LINENUMBERS)
101
                {
102
                    LineNumberModeling(item);
103
                }
104 1efc25a3 gaqhf
                // Input Symbol Attribute
105
                foreach (var item in document.SYMBOLS)
106
                {
107
                    InputSymbolAttribute(item);
108
                }
109
                // Input Line Attribute
110 10872260 gaqhf
111 1efc25a3 gaqhf
                // Input LineNumber Attribute
112 335b7a24 gaqhf
113 1efc25a3 gaqhf
                // Note Modeling
114 8b085570 gaqhf
115 1efc25a3 gaqhf
                // Text Modeling
116
117
                // LineRun Line Join
118 3165c259 gaqhf
                foreach (LineNumber lineNumber in document.LINENUMBERS)
119
                {
120
121
                    foreach (LineRun run in lineNumber.RUNS)
122
                    {
123
                        JoinRunLine(run);
124
                    }
125
                }
126 1efc25a3 gaqhf
                // TrimLineRun Line Join
127 3165c259 gaqhf
                foreach (TrimLine trimLine in document.TRIMLINES)
128
                {
129
                    foreach (LineRun run in trimLine.RUNS)
130
                    {
131
                        JoinRunLine(run);
132
                    }
133
                }
134 809a7640 gaqhf
            }
135 5e6ecf05 gaqhf
            catch (Exception ex)
136
            {
137
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
138
            }
139
            finally
140
            {
141
                ReleaseCOMObjects(dataSource);
142
                ReleaseCOMObjects(_placement);
143
            }
144
           
145 809a7640 gaqhf
            System.Windows.Forms.MessageBox.Show("end");
146
        }
147 f1c9dbaa gaqhf
148 8aa6f2db gaqhf
        private void LineModelingByRun(LineRun run)
149 809a7640 gaqhf
        {
150
            Line prevLine = null;
151
            List<Line> lines = new List<Line>();
152
            foreach (var item in run.RUNITEMS)
153
            {
154
                // Line일 경우
155
                if (item.GetType() == typeof(Line))
156
                {
157
                    Line line = item as Line;
158
                    if (prevLine == null)
159
                        lines.Add(line);
160
                    else if (prevLine != null)
161
                    {
162
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
163
                            lines.Add(line);
164
                        else
165 f1c9dbaa gaqhf
                        {
166 809a7640 gaqhf
                            LineModeling(lines);
167
                            lines.Clear();
168
                            lines.Add(line);
169 5dfb8a24 gaqhf
                        }
170
                    }
171
172 809a7640 gaqhf
                    prevLine = line;
173
                }
174
                // Symbol 일 경우
175
                else if (item.GetType() == typeof(Symbol))
176
                {
177 f1c9dbaa gaqhf
                    if (lines.Count > 0)
178 809a7640 gaqhf
                    {
179 f1c9dbaa gaqhf
                        LineModeling(lines);
180 809a7640 gaqhf
                        lines.Clear();
181
                    }
182 5dfb8a24 gaqhf
                }
183
            }
184
185 809a7640 gaqhf
            if (lines.Count > 0)
186
                LineModeling(lines);
187
        }
188 5dfb8a24 gaqhf
189 8aa6f2db gaqhf
        private void SymbolModelingByRun(LineRun run)
190 809a7640 gaqhf
        {
191
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
192
            if (run.RUNITEMS.Count > 0)
193
            {
194
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
195
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
196
197
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
198
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
199
            }
200
201
            Symbol prevSymbol = null;
202
            Symbol targetSymbol = null;
203
            foreach (var item in run.RUNITEMS)
204
            {
205
                if (item.GetType() == typeof(Symbol))
206
                {
207
                    Symbol symbol = item as Symbol;
208
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
209
                    prevSymbol = symbol;
210
                    targetSymbol = symbol;
211
                }
212
                else
213
                {
214
                    targetSymbol = null;
215
                }
216
            }
217
        }
218
219 8aa6f2db gaqhf
        private void SymbolModelingByRunStart(Symbol symbol, LineRun run)
220 809a7640 gaqhf
        {
221
            foreach (var connector in symbol.CONNECTORS)
222
            {
223
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
224
                if (targetItem != null &&
225
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
226
                    !IsSameLineNumber(symbol, targetItem))
227
                {
228
                    SymbolModeling(symbol, targetItem as Symbol, null);
229
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
230
                    {
231
                        object item = run.RUNITEMS[i];
232
                        if (item.GetType() == typeof(Symbol))
233
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol, null);
234
                        else
235
                            break;
236
                    }
237
                    break;
238
                }
239
            }
240
241
242
        }
243
244 8aa6f2db gaqhf
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
245 809a7640 gaqhf
        {
246
            foreach (var connector in symbol.CONNECTORS)
247
            {
248
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
249
                if (targetItem != null &&
250
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
251
                    !IsSameLineNumber(symbol, targetItem))
252
                {
253
                    SymbolModeling(symbol, targetItem as Symbol, null);
254
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
255
                    {
256
                        object item = run.RUNITEMS[i];
257
                        if (item.GetType() == typeof(Symbol))
258
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol, null);
259
                        else
260
                            break;
261
                    }
262
                    break;
263
                }
264
            }
265 5dfb8a24 gaqhf
        }
266 cfda1fed gaqhf
267 5dfb8a24 gaqhf
        private void TestSource()
268
        {
269 cfda1fed gaqhf
270
        }
271
272 809a7640 gaqhf
        private void SymbolModeling(Symbol symbol, Symbol prevSymbol, object prevItem, bool IsFirst)
273 cfda1fed gaqhf
        {
274 809a7640 gaqhf
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
275
                return;
276
277 5dfb8a24 gaqhf
            LMSymbol _LMSymbol = null;
278
279
            string mappingPath = symbol.SPPID.MAPPINGNAME;
280 1b261371 gaqhf
            double x = symbol.SPPID.ORIGINAL_X;
281
            double y = symbol.SPPID.ORIGINAL_Y;
282 5dfb8a24 gaqhf
            int mirror = 0;
283
            double angle = symbol.ANGLE;
284
            LMSymbol _TargetItem = null;
285
286 809a7640 gaqhf
            if (IsFirst)
287
            {
288
                foreach (var connector in symbol.CONNECTORS)
289
                {
290
                    object item = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
291
                    if (item != null && 
292
                        (item.GetType() == typeof(Symbol) || item.GetType() == typeof(Equipment)) && 
293
                        !IsSameLineNumber(symbol, item))
294
                    {
295
                        _TargetItem = dataSource.GetSymbol(((Symbol)item).SPPID.RepresentationId);
296
                        break;
297
                    }
298
                }
299
            }
300
            else if (prevItem != null && prevItem.GetType() == typeof(Symbol))
301 f1c9dbaa gaqhf
                _TargetItem = dataSource.GetSymbol(((Symbol)prevItem).SPPID.RepresentationId);
302 8aa6f2db gaqhf
            
303 5dfb8a24 gaqhf
            if (prevSymbol != null)
304
            {
305 1b261371 gaqhf
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
306 f1c9dbaa gaqhf
                LMSymbol prevLMSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
307 1b261371 gaqhf
                double prevX = prevLMSymbol.get_XCoordinate();
308
                double prevY = prevLMSymbol.get_YCoordinate();
309 5dfb8a24 gaqhf
                if (slopeType == SlopeType.HORIZONTAL)
310 1b261371 gaqhf
                    y = prevY;
311 5dfb8a24 gaqhf
                else if (slopeType == SlopeType.VERTICAL)
312 1b261371 gaqhf
                    x = prevX;
313 8aa6f2db gaqhf
                
314 f1c9dbaa gaqhf
                ReleaseCOMObjects(prevLMSymbol);
315 5dfb8a24 gaqhf
            }
316
317
            if (_TargetItem == null)
318
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
319
            else
320
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
321
322
            if (_LMSymbol != null)
323
            {
324 f1c9dbaa gaqhf
                _LMSymbol.Commit();
325
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
326 5dfb8a24 gaqhf
            }
327
328 f1c9dbaa gaqhf
            if (_TargetItem != null)
329
                ReleaseCOMObjects(_TargetItem);
330 809a7640 gaqhf
            
331 f1c9dbaa gaqhf
            ReleaseCOMObjects(_LMSymbol);
332 cfda1fed gaqhf
        }
333
334 809a7640 gaqhf
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
335
        {
336
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
337
                return;
338
339
            LMSymbol _LMSymbol = null;
340
341
            string mappingPath = symbol.SPPID.MAPPINGNAME;
342
            double x = symbol.SPPID.ORIGINAL_X;
343
            double y = symbol.SPPID.ORIGINAL_Y;
344
            int mirror = 0;
345
            double angle = symbol.ANGLE;
346
347
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
348
            {
349
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
350
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
351
                ReleaseCOMObjects(_TargetItem);
352
            }
353
            else if (prevSymbol != null)
354
            {
355
                LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
356
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
357
                double prevX = _PrevSymbol.get_XCoordinate();
358
                double prevY = _PrevSymbol.get_YCoordinate();
359
                if (slopeType == SlopeType.HORIZONTAL)
360
                    y = prevY;
361
                else if (slopeType == SlopeType.VERTICAL)
362
                    x = prevX;
363
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
364
            }
365
            else
366
            {
367
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
368
            }
369
370
371
            if (_LMSymbol != null)
372
            {
373
                _LMSymbol.Commit();
374
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
375
            }
376
377
            ReleaseCOMObjects(_LMSymbol);
378
        }
379
380
        private bool IsSameLineNumber(object item, object targetItem)
381
        {
382
            foreach (var lineNumber in document.LINENUMBERS)
383
            {
384
                foreach (var run in lineNumber.RUNS)
385
                {
386
                    foreach (var runItem in run.RUNITEMS)
387
                    {
388
                        if (runItem == item)
389
                        {
390
                            foreach (var findItem in run.RUNITEMS)
391
                            {
392
                                if (findItem == targetItem)
393
                                {
394
                                    return true;
395
                                }
396
                            }
397
398
                            return false;
399
400
                        }
401
                    }
402
                }
403
            }
404
405
            return false;
406
        }
407
408 1b261371 gaqhf
        private void LineModeling(List<Line> lines)
409
        {
410 335b7a24 gaqhf
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
411 1b261371 gaqhf
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
412 f1c9dbaa gaqhf
            LMSymbol _LMSymbol1 = null;
413
            LMSymbol _LMSymbol2 = null;
414 5e6ecf05 gaqhf
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
415
            LMConnector targetConnector1 = null;
416
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
417
            LMConnector targetConnector2 = null;
418 335b7a24 gaqhf
419
            Line startBranchLine = null;
420
            Line endBranchLine = null;
421
422 1b261371 gaqhf
            for (int i = 0; i < lines.Count; i++)
423
            {
424
                Line line = lines[i];
425 f1c9dbaa gaqhf
                if (i == 0 || i + 1 != lines.Count)
426
                {
427 809a7640 gaqhf
                    // 시작점에 연결된 Symbol 찾기
428 f1c9dbaa gaqhf
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
429
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
430
                    {
431
                        _LMSymbol1 = dataSource.GetSymbol(((Symbol)connItem).SPPID.RepresentationId);
432
                        if (_LMSymbol1 != null)
433
                            placeRunInputs.AddSymbolTarget(_LMSymbol1, line.SPPID.START_X, line.SPPID.START_Y);
434
                        else
435
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
436
                    }
437 335b7a24 gaqhf
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
438 5e6ecf05 gaqhf
                    {
439
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
440 56bc67e1 gaqhf
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
441 5e6ecf05 gaqhf
442
                        if (targetConnector1 != null)
443
                            placeRunInputs.AddConnectorTarget(targetConnector1, line.SPPID.START_X, line.SPPID.START_Y);
444
                        else
445 335b7a24 gaqhf
                        {
446
                            startBranchLine = connItem as Line;
447 5e6ecf05 gaqhf
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
448 335b7a24 gaqhf
                        }
449 5e6ecf05 gaqhf
                    }
450 f1c9dbaa gaqhf
                    else
451
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
452
                }
453
454
                if (i + 1 == lines.Count)
455
                {
456 809a7640 gaqhf
                    // 끝점에 연결된 Symbol 찾기
457 f1c9dbaa gaqhf
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
458 5e6ecf05 gaqhf
459
                    if (i != 0)
460
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
461
462 f1c9dbaa gaqhf
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
463
                    {
464
                        _LMSymbol2 = dataSource.GetSymbol(((Symbol)connItem).SPPID.RepresentationId);
465
                        if (_LMSymbol2 != null)
466
                            placeRunInputs.AddSymbolTarget(_LMSymbol2, line.SPPID.END_X, line.SPPID.END_Y);
467
                        else
468
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
469
                            
470
                    }
471 335b7a24 gaqhf
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
472 5e6ecf05 gaqhf
                    {
473
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
474 56bc67e1 gaqhf
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
475 5e6ecf05 gaqhf
476
                        if (targetConnector2 != null)
477
                            placeRunInputs.AddConnectorTarget(targetConnector2, line.SPPID.END_X, line.SPPID.END_Y);
478
                        else
479 335b7a24 gaqhf
                        {
480
                            endBranchLine = connItem as Line;
481 5e6ecf05 gaqhf
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
482 335b7a24 gaqhf
                        }
483 5e6ecf05 gaqhf
                    }
484 f1c9dbaa gaqhf
                    else
485
                    {
486
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
487
                    }
488
                }
489
            }
490
491
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
492 335b7a24 gaqhf
493 f1c9dbaa gaqhf
            if (_lMConnector != null)
494
            {
495
                foreach (var line in lines)
496 5e6ecf05 gaqhf
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
497 f1c9dbaa gaqhf
                _lMConnector.Commit();
498 335b7a24 gaqhf
                if (startBranchLine != null || endBranchLine != null)
499
                {
500 3165c259 gaqhf
                    BranchLines.Add(new Tuple<string, Line, Line>(_lMConnector.ModelItemID, startBranchLine, endBranchLine));
501 335b7a24 gaqhf
                }
502 1b261371 gaqhf
            }
503 5e6ecf05 gaqhf
504 1b261371 gaqhf
505 f1c9dbaa gaqhf
            if (_LMSymbol1 != null)
506
                ReleaseCOMObjects(_LMSymbol1);
507
            if (_LMSymbol2 != null)
508
                ReleaseCOMObjects(_LMSymbol2);
509 5e6ecf05 gaqhf
            if (targetConnector1 != null)
510
                ReleaseCOMObjects(targetConnector1);
511
            if (targetConnector2 != null)
512
                ReleaseCOMObjects(targetConnector2);
513
            foreach (var item in connectorVertices1)
514
                ReleaseCOMObjects(item.Key);
515
            foreach (var item in connectorVertices2)
516
                ReleaseCOMObjects(item.Key);
517 1b261371 gaqhf
518 f1c9dbaa gaqhf
            ReleaseCOMObjects(_lMConnector);
519 1b261371 gaqhf
            ReleaseCOMObjects(placeRunInputs);
520
            ReleaseCOMObjects(_LMAItem);
521
        }
522
523 335b7a24 gaqhf
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
524
        {
525
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
526
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
527
528 8b085570 gaqhf
            LMConnector _StartConnector = null;
529
            LMConnector _EndConnector = null;
530 335b7a24 gaqhf
            double lengthStart = double.MaxValue;
531
            double lengthEnd = double.MaxValue;
532
            List<double[]> startPoints = new List<double[]>();
533
            List<double[]> endPoints = new List<double[]>();
534
535
            foreach (var item in connectorVertices)
536
            {
537
                foreach (var point in item.Value)
538
                {
539
                    // Start Point가 Branch
540
                    if (branch.Item2 != null)
541
                    {
542
                        Line targetLine = branch.Item2;
543
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
544
                        if (lengthStart > distance)
545
                        {
546 8b085570 gaqhf
                            _StartConnector = item.Key;
547 335b7a24 gaqhf
                            lengthStart = distance;
548
                            startPoints = item.Value;
549
                        }
550
                    }
551
                    // End Point가 Branch
552
                    if (branch.Item3 != null)
553
                    {
554
                        Line targetLine = branch.Item3;
555
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
556
                        if (lengthEnd > distance)
557
                        {
558 8b085570 gaqhf
                            _EndConnector = item.Key;
559 335b7a24 gaqhf
                            lengthEnd = distance;
560
                            endPoints = item.Value;
561
                        }
562
                    }
563
                }
564
            }
565
            #region Branch가 양쪽 전부일 때
566 8b085570 gaqhf
            if (_StartConnector != null && _StartConnector == _EndConnector)
567 335b7a24 gaqhf
            {
568 8b085570 gaqhf
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
569 335b7a24 gaqhf
570
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
571
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
572
573
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
574 8b085570 gaqhf
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
575 335b7a24 gaqhf
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
576 8b085570 gaqhf
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
577 335b7a24 gaqhf
                   startPoints[startPoints.Count - 1][0],
578
                   startPoints[startPoints.Count - 1][1],
579
                   startPoints[startPoints.Count - 2][0],
580
                   startPoints[startPoints.Count - 2][1]);
581
582
                for (int i = 0; i < startPoints.Count; i++)
583
                {
584
                    double[] point = startPoints[i];
585
                    if (i == 0)
586 8b085570 gaqhf
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
587 335b7a24 gaqhf
                    else if (i == startPoints.Count - 1)
588 8b085570 gaqhf
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
589 335b7a24 gaqhf
                    else
590
                        placeRunInputs.AddPoint(point[0], point[1]);
591
                }
592
593
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
594
                if (_LMConnector != null)
595
                {
596
                    _LMConnector.Commit();
597
                    foreach (var item in lines)
598
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
599
                }
600
601
                foreach (var item in startConnectorVertices)
602
                    ReleaseCOMObjects(item.Key);
603
                foreach (var item in endConnectorVertices)
604
                    ReleaseCOMObjects(item.Key);
605
                ReleaseCOMObjects(placeRunInputs);
606
                ReleaseCOMObjects(_LMAItem);
607
                ReleaseCOMObjects(_LMConnector);
608
            }
609
            #endregion
610 8b085570 gaqhf
            #region 양쪽이 다른 Branch 
611 335b7a24 gaqhf
            else
612
            {
613
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
614
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
615
616
                // Branch 시작 Connector
617 8b085570 gaqhf
                if (_StartConnector != null)
618 335b7a24 gaqhf
                {
619 8b085570 gaqhf
                    LMConnector _EndTargetConnector = null;
620
                    LMSymbol _EndTargetSymbol = null;
621 335b7a24 gaqhf
                    // Branch 반대편이 Line
622
                    foreach (var item in connectorVertices)
623
                    {
624 8b085570 gaqhf
                        if (item.Key == _StartConnector)
625 335b7a24 gaqhf
                            continue;
626
627 8b085570 gaqhf
                        if (item.Key.ConnectItem1SymbolID == _StartConnector.ConnectItem2SymbolID)
628 335b7a24 gaqhf
                        {
629 8b085570 gaqhf
                            _EndTargetConnector = item.Key;
630 335b7a24 gaqhf
                            break;
631
                        }
632
                    }
633
                    // Branch 반대편이 Symbol
634 8b085570 gaqhf
                    if (_EndTargetConnector == null)
635 335b7a24 gaqhf
                    {
636
                        foreach (var line in lines)
637
                        {
638
                            foreach (var connector in line.CONNECTORS)
639
                            {
640
                                Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
641
                                if (symbol != null)
642
                                {
643 8b085570 gaqhf
                                    _EndTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
644 335b7a24 gaqhf
                                    break;
645
                                }
646
                            }
647
                        }
648
                    }
649
650 8b085570 gaqhf
                    _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
651 335b7a24 gaqhf
652
                    Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
653 8b085570 gaqhf
                    LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
654 335b7a24 gaqhf
655
                    for (int i = 0; i < startPoints.Count; i++)
656
                    {
657
                        double[] point = startPoints[i];
658
                        if (i == 0)
659 8b085570 gaqhf
                            placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
660 335b7a24 gaqhf
                        else if (i == startPoints.Count - 1)
661
                        {
662 8b085570 gaqhf
                            if (_EndTargetConnector != null)
663
                                placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
664
                            else if (_EndTargetSymbol != null)
665
                                placeRunInputs.AddSymbolTarget(_EndTargetSymbol, point[0], point[1]);
666 335b7a24 gaqhf
                            else
667
                                placeRunInputs.AddPoint(point[0], point[1]);
668
                        }
669
                        else
670
                            placeRunInputs.AddPoint(point[0], point[1]);
671
                    }
672
673
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
674
                    if (_LMConnector != null )
675
                    {
676 8b085570 gaqhf
                        if (_EndTargetConnector != null)
677 335b7a24 gaqhf
                        {
678 8b085570 gaqhf
                            JoinPipeRun(_LMConnector.ModelItemID, _EndTargetConnector.ModelItemID);
679 335b7a24 gaqhf
                        }
680
                        else
681
                        {
682
                            foreach (var item in lines)
683
                                item.SPPID.ModelItemId = _LMConnector.ModelItemID;
684
                        }
685
686
                        _LMConnector.Commit();
687
                        ReleaseCOMObjects(_LMConnector);
688
                    }
689
690 8b085570 gaqhf
                    if (_EndTargetConnector != null)
691
                        ReleaseCOMObjects(_EndTargetConnector);
692
                    if (_EndTargetSymbol != null)
693
                        ReleaseCOMObjects(_EndTargetSymbol);
694 335b7a24 gaqhf
                    foreach (var item in startConnectorVertices)
695
                        ReleaseCOMObjects(item.Key);
696
697
                }
698
                // Branch 끝 Connector
699 8b085570 gaqhf
                if (_EndConnector != null)
700 335b7a24 gaqhf
                {
701 8b085570 gaqhf
                    LMConnector _StartTargetConnector = null;
702
                    LMSymbol _StartTargetSymbol = null;
703 335b7a24 gaqhf
                    // Branch 반대편이 Line
704
                    foreach (var item in connectorVertices)
705
                    {
706 8b085570 gaqhf
                        if (item.Key == _EndConnector)
707 335b7a24 gaqhf
                            continue;
708
709 8b085570 gaqhf
                        if (item.Key.ConnectItem2SymbolID == _EndConnector.ConnectItem1SymbolID)
710 335b7a24 gaqhf
                        {
711 8b085570 gaqhf
                            _StartTargetConnector = item.Key;
712 335b7a24 gaqhf
                            break;
713
                        }
714
                    }
715
                    // Branch 반대편이 Symbol
716 8b085570 gaqhf
                    if (_StartTargetConnector == null)
717 335b7a24 gaqhf
                    {
718
                        foreach (var line in lines)
719
                        {
720
                            foreach (var connector in line.CONNECTORS)
721
                            {
722
                                Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
723
                                if (symbol != null)
724
                                {
725 8b085570 gaqhf
                                    _StartTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
726 335b7a24 gaqhf
                                    break;
727
                                }
728
                            }
729
                        }
730
                    }
731
732 8b085570 gaqhf
                    _placement.PIDRemovePlacement(_EndConnector.AsLMRepresentation());
733 335b7a24 gaqhf
734
                    Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
735 8b085570 gaqhf
                    LMConnector _EndTargetConnector = FindTargetLMConnector(startConnectorVertices, 
736 335b7a24 gaqhf
                        endPoints[endPoints.Count - 1][0], 
737
                        endPoints[endPoints.Count - 1][1], 
738
                        endPoints[endPoints.Count - 2][0], 
739
                        endPoints[endPoints.Count - 2][1]);
740
741
                    for (int i = 0; i < endPoints.Count; i++)
742
                    {
743
                        double[] point = endPoints[i];
744
                        if (i == 0)
745
                        {
746 8b085570 gaqhf
                            if (_StartTargetConnector != null)
747
                                placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
748
                            else if (_StartTargetSymbol != null)
749
                                placeRunInputs.AddSymbolTarget(_StartTargetSymbol, point[0], point[1]);
750 335b7a24 gaqhf
                            else
751
                                placeRunInputs.AddPoint(point[0], point[1]);
752
                        }
753
                        else if (i == endPoints.Count - 1)
754
                        {
755 8b085570 gaqhf
                            placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
756 335b7a24 gaqhf
                        }
757
                        else
758
                            placeRunInputs.AddPoint(point[0], point[1]);
759
                    }
760
761
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
762
                    if (_LMConnector != null)
763
                    {
764 8b085570 gaqhf
                        if (_StartTargetConnector != null)
765 335b7a24 gaqhf
                        {
766 8b085570 gaqhf
                            JoinPipeRun(_LMConnector.ModelItemID, _StartTargetConnector.ModelItemID);
767 335b7a24 gaqhf
                        }
768
                        else
769
                        {
770
                            foreach (var item in lines)
771
                                item.SPPID.ModelItemId = _LMConnector.ModelItemID;
772
                        }
773
774
                        _LMConnector.Commit();
775
                        ReleaseCOMObjects(_LMConnector);
776
                    }
777
778 8b085570 gaqhf
                    if (_StartTargetConnector != null)
779
                        ReleaseCOMObjects(_StartTargetConnector);
780
                    if (_StartTargetSymbol != null)
781
                        ReleaseCOMObjects(_StartTargetSymbol);
782 335b7a24 gaqhf
                    foreach (var item in startConnectorVertices)
783
                        ReleaseCOMObjects(item.Key);
784
                }
785
786
                ReleaseCOMObjects(_LMAItem);
787
                ReleaseCOMObjects(placeRunInputs);
788
            }
789
            #endregion
790
791 8b085570 gaqhf
            if (_StartConnector != null)
792
                ReleaseCOMObjects(_StartConnector);
793
            if (_EndConnector != null)
794
                ReleaseCOMObjects(_EndConnector);
795 335b7a24 gaqhf
            foreach (var item in connectorVertices)
796
                ReleaseCOMObjects(item.Key);
797
        }
798
799 3165c259 gaqhf
        private void EndBreakModeling(EndBreak endBreak)
800 335b7a24 gaqhf
        {
801 3165c259 gaqhf
            Line ownerLine = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER) as Line;
802
            Line connLine = null;   
803
            Property property = endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item");
804
            if (property != null)
805
                connLine = SPPIDUtil.FindObjectByUID(document, property.VALUE) as Line;
806 335b7a24 gaqhf
807 3165c259 gaqhf
            if (ownerLine != null && connLine != null)
808 335b7a24 gaqhf
            {
809 3165c259 gaqhf
                LMLabelPersist _LmLabelPersist = null;
810
811
                string lineModelId = ownerLine.SPPID.ModelItemId;
812
                LMPipeRun _LMPipeRun = _placement.PIDDataSource.GetPipeRun(lineModelId);
813
                LMPipeRun _ConnLMPipeRun = _placement.PIDDataSource.GetPipeRun(connLine.SPPID.ModelItemId);
814
                LMConnector connectedLMConnector = null;
815
816
                if (_LMPipeRun != null && _ConnLMPipeRun != null)
817 335b7a24 gaqhf
                {
818 3165c259 gaqhf
                    foreach (LMRepresentation rep in _LMPipeRun.Representations)
819 335b7a24 gaqhf
                    {
820 3165c259 gaqhf
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
821
                        {
822
                            LMConnector _LMConnector = _placement.PIDDataSource.GetConnector(rep.Id);
823
                            if (_LMConnector.ConnectItem1SymbolID != null && !DBNull.Value.Equals(_LMConnector.ConnectItem1SymbolID) && ExistConnItem(_ConnLMPipeRun, _LMConnector.ConnectItem1SymbolID))
824
                            {
825
                                connectedLMConnector = _LMConnector;
826
                                break;
827
                            }
828
                            else if (_LMConnector.ConnectItem2SymbolID != null && !DBNull.Value.Equals(_LMConnector.ConnectItem2SymbolID) && ExistConnItem(_ConnLMPipeRun, _LMConnector.ConnectItem2SymbolID))
829
                            {
830
                                connectedLMConnector = _LMConnector;
831
                                break;
832
                            }
833
                            else
834
                            {
835
                                ReleaseCOMObjects(_LMConnector);
836
                            }
837
                        }
838 335b7a24 gaqhf
                    }
839 3165c259 gaqhf
                    if (connectedLMConnector != null)
840 335b7a24 gaqhf
                    {
841 3165c259 gaqhf
                        Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
842
                        _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
843
                        ReleaseCOMObjects(connectedLMConnector);
844 335b7a24 gaqhf
                    }
845 3165c259 gaqhf
846
                    ReleaseCOMObjects(_LMPipeRun);
847
                    ReleaseCOMObjects(_ConnLMPipeRun);
848
                    if (_LmLabelPersist != null)
849
                        ReleaseCOMObjects(_LmLabelPersist);
850 335b7a24 gaqhf
                }
851
            }
852 3165c259 gaqhf
        }
853 10872260 gaqhf
854 3165c259 gaqhf
        private bool ExistConnItem(LMPipeRun _LMPipeRun, string ConnItem)
855
        {
856
            foreach (LMRepresentation rep in _LMPipeRun.Representations)
857 335b7a24 gaqhf
            {
858 3165c259 gaqhf
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
859 335b7a24 gaqhf
                {
860 3165c259 gaqhf
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
861
                    if (_LMConnector.ConnectItem1SymbolID != null && !DBNull.Value.Equals(_LMConnector.ConnectItem1SymbolID) && _LMConnector.ConnectItem1SymbolID == ConnItem)
862 335b7a24 gaqhf
                    {
863 3165c259 gaqhf
                        ReleaseCOMObjects(_LMConnector);
864
                        return true;
865 335b7a24 gaqhf
                    }
866 3165c259 gaqhf
                    else if (_LMConnector.ConnectItem2SymbolID != null && !DBNull.Value.Equals(_LMConnector.ConnectItem2SymbolID) && _LMConnector.ConnectItem2SymbolID == ConnItem)
867 335b7a24 gaqhf
                    {
868 3165c259 gaqhf
                        ReleaseCOMObjects(_LMConnector);
869
                        return true;
870 335b7a24 gaqhf
                    }
871
                    else
872
                    {
873 3165c259 gaqhf
                        ReleaseCOMObjects(_LMConnector);
874 335b7a24 gaqhf
                    }
875
                }
876
            }
877
878 3165c259 gaqhf
            return false;
879 335b7a24 gaqhf
        }
880
881
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
882
        {
883
            LMPipeRun run1 = dataSource.GetPipeRun(toModelItemId);
884
            LMPipeRun run2 = dataSource.GetPipeRun(fromModelItemId);
885
            // item2가 item1으로 조인
886
            try
887
            {
888
                _LMAItem item1 = run1.AsLMAItem();
889
                _LMAItem item2 = run2.AsLMAItem();
890
891
                _placement.PIDJoinRuns(ref item1, ref item2);
892
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
893
                foreach (var line in lines)
894
                    line.SPPID.ModelItemId = toModelItemId;
895
896
            }
897
            catch (Exception ex)
898
            {
899
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
900
            }
901
            finally
902
            {
903
                ReleaseCOMObjects(run1);
904
                ReleaseCOMObjects(run2);
905
            }
906
        }
907
908
        private void AutoJoinPipeRun(string modelItemId)
909
        {
910
            LMPipeRun run = dataSource.GetPipeRun(modelItemId);
911
            _LMAItem item = run.AsLMAItem();
912
            _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
913
        }
914
915
        private void JoinRunLine(LineRun run)
916
        {
917
            string modelItemId = string.Empty;
918
            foreach (var item in run.RUNITEMS)
919
            {
920
                if (item.GetType() == typeof(Line))
921
                {
922
                    Line line = item as Line;
923
                    if (modelItemId != line.SPPID.ModelItemId)
924
                    {
925
                        AutoJoinPipeRun(line.SPPID.ModelItemId);
926
                        modelItemId = line.SPPID.ModelItemId;
927
                    }
928
                    //if (string.IsNullOrEmpty(modelItemId))
929
                    //{
930
                    //    modelItemId = line.SPPID.ModelItemId;
931
                    //}
932
                    //else if(modelItemId != line.SPPID.ModelItemId)
933
                    //{
934
                    //    JoinPipeRun(line.SPPID.ModelItemId, modelItemId);
935
                    //}
936
                }
937
            }
938
        }
939
940 5e6ecf05 gaqhf
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
941
        {
942
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
943
            LMPipeRun _LMPipeRun = dataSource.GetPipeRun(modelId);
944
            if (_LMPipeRun != null)
945
            {
946
                foreach (LMRepresentation rep in _LMPipeRun.Representations)
947
                {
948
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
949
                    {
950
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
951
                        connectorVertices.Add(_LMConnector, new List<double[]>());
952
                        dynamic OID = rep.get_GraphicOID();
953 335b7a24 gaqhf
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
954 5e6ecf05 gaqhf
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
955
                        int verticesCount = lineStringGeometry.VertexCount;
956
                        double[] vertices = null;
957
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
958
                        for (int i = 0; i < verticesCount; i++)
959
                        {
960
                            double x = 0;
961
                            double y = 0;
962
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
963 335b7a24 gaqhf
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
964 5e6ecf05 gaqhf
                        }
965
                    }
966
                }
967
968
                ReleaseCOMObjects(_LMPipeRun);
969
            }
970
971
            return connectorVertices;
972
        }
973
974 56bc67e1 gaqhf
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
975 5e6ecf05 gaqhf
        {
976
            double length = double.MaxValue;
977
            LMConnector targetConnector = null;
978
            foreach (var item in connectorVertices)
979
            {
980
                List<double[]> points = item.Value;
981
                for (int i = 0; i < points.Count - 1; i++)
982
                {
983
                    double[] point1 = points[i];
984
                    double[] point2 = points[i + 1];
985
986 335b7a24 gaqhf
                    double maxLineX = Math.Max(point1[0], point2[0]);
987
                    double minLineX = Math.Min(point1[0], point2[0]);
988
                    double maxLineY = Math.Max(point1[1], point2[1]);
989
                    double minLineY = Math.Min(point1[1], point2[1]);
990
991
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
992
993 56bc67e1 gaqhf
                    // 두직선의 교차점
994
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
995
                    if (crossingPoint != null)
996 5e6ecf05 gaqhf
                    {
997 30a9ffce gaqhf
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
998 335b7a24 gaqhf
                        if (length >= distance)
999 30a9ffce gaqhf
                        {
1000 335b7a24 gaqhf
                            if (slope == SlopeType.Slope &&
1001
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
1002
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1003
                            {
1004
                                targetConnector = item.Key;
1005
                                length = distance;
1006
                            }
1007
                            else if (slope == SlopeType.HORIZONTAL &&
1008
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
1009
                            {
1010
                                targetConnector = item.Key;
1011
                                length = distance;
1012
                            }
1013
                            else if (slope == SlopeType.VERTICAL &&
1014
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1015
                            {
1016
                                targetConnector = item.Key;
1017
                                length = distance;
1018
                            }
1019 30a9ffce gaqhf
                        }
1020 5e6ecf05 gaqhf
                    }
1021
                }
1022
            }
1023
1024
            return targetConnector;
1025
        }
1026
1027 cfda1fed gaqhf
        private void LineNumberModeling(LineNumber lineNumber)
1028
        {
1029 10872260 gaqhf
            Line line = null;
1030
            foreach (var lineRun in lineNumber.RUNS)
1031
            {
1032
                foreach (var item in lineRun.RUNITEMS)
1033
                {
1034
                    line = item as Line;
1035
                    if (line != null && !string.IsNullOrEmpty(line.SPPID.ModelItemId))
1036
                        break;
1037
                    else
1038
                        line = null;
1039
                }
1040
                if (line != null)
1041
                    break;
1042
            }
1043
1044
            if (line != null && !string.IsNullOrEmpty(line.SPPID.ModelItemId))
1045
            {
1046
                Array points = new double[] { 0, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y };
1047
                LMPipeRun _pipeRun = dataSource.GetPipeRun(line.SPPID.ModelItemId);
1048
                if (_pipeRun != null)
1049
                {
1050
                    foreach (LMRepresentation rep in _pipeRun.Representations)
1051
                    {
1052
                        if (rep.get_RepresentationType() == "Connector" && rep.get_ItemStatus() == "Active")
1053
                        {
1054
                            LMConnector _LmConnector = dataSource.GetConnector(rep.Id);
1055
                            LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: _LmConnector.AsLMRepresentation(), IsLeaderVisible: false);
1056
                            _LmLabelPresist.Commit();
1057 cfda1fed gaqhf
1058 10872260 gaqhf
                            if (_LmConnector != null)
1059
                                ReleaseCOMObjects(_LmConnector);
1060
                            if (_LmLabelPresist != null)
1061
                            {
1062
                                lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
1063
                                ReleaseCOMObjects(_LmConnector);
1064
                            }
1065
1066
                            break;
1067
                        }
1068
                    }
1069
1070
                    ReleaseCOMObjects(_pipeRun);
1071
                }
1072
            }
1073 cfda1fed gaqhf
        }
1074
1075 1efc25a3 gaqhf
        private void InputSymbolAttribute(Symbol symbol)
1076
        {
1077
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1078
            {
1079
                foreach (var item in symbol.PROPERTIES)
1080
                {
1081
                    
1082
1083
                }
1084
1085
                foreach (var item in symbol.ATTRIBUTES)
1086
                {
1087
1088
1089
                }
1090
            }
1091
        }
1092
1093
1094
1095 cfda1fed gaqhf
        private void TextModeling(Text text)
1096
        {
1097
1098
        }
1099
1100
        private void NoteModeling(Note note)
1101
        {
1102
1103
        }
1104
1105 5dfb8a24 gaqhf
        public void ReleaseCOMObjects(params object[] objVars)
1106
        {
1107
            int intNewRefCount = 0;
1108
            foreach (object obj in objVars)
1109
            {
1110
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
1111
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
1112
            }
1113
        }
1114 cfda1fed gaqhf
    }
1115
}
클립보드 이미지 추가 (최대 크기: 500 MB)