프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ de8b8993

이력 | 보기 | 이력해설 | 다운로드 (53 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 65a1ed4b gaqhf
51
52 5e6ecf05 gaqhf
            try
53 5dfb8a24 gaqhf
            {
54 1efc25a3 gaqhf
                // Equipment Modeling
55 5e6ecf05 gaqhf
                foreach (Equipment equipment in document.Equipments)
56
                    SymbolModeling(equipment as Symbol, null, null);
57 65a1ed4b gaqhf
                
58 1efc25a3 gaqhf
                // LineRun Symbol Modeling
59 5e6ecf05 gaqhf
                foreach (LineNumber lineNumber in document.LINENUMBERS)
60
                    foreach (LineRun run in lineNumber.RUNS)
61
                        SymbolModelingByRun(run);
62 65a1ed4b gaqhf
                
63 1efc25a3 gaqhf
                // TrimLineRun Symbol Modeling
64 5e6ecf05 gaqhf
                foreach (TrimLine trimLine in document.TRIMLINES)
65
                    foreach (LineRun run in trimLine.RUNS)
66
                        SymbolModelingByRun(run);
67 65a1ed4b gaqhf
                
68 1efc25a3 gaqhf
                // LineRun Line Modeling
69 5e6ecf05 gaqhf
                foreach (LineNumber lineNumber in document.LINENUMBERS)
70
                    foreach (LineRun run in lineNumber.RUNS)
71
                        LineModelingByRun(run);
72 65a1ed4b gaqhf
                
73 1efc25a3 gaqhf
                // TrimLineRun Line Modeling
74 5e6ecf05 gaqhf
                foreach (TrimLine trimLine in document.TRIMLINES)
75
                    foreach (LineRun run in trimLine.RUNS)
76
                        LineModelingByRun(run);
77 65a1ed4b gaqhf
                
78 1efc25a3 gaqhf
                // Branch Line Modeling
79 3165c259 gaqhf
                foreach (var item in BranchLines)
80 335b7a24 gaqhf
                    BranchLineModeling(item);
81 65a1ed4b gaqhf
                
82 1efc25a3 gaqhf
                // EndBreak Modeling
83 3165c259 gaqhf
                foreach (var item in document.EndBreaks)
84
                    EndBreakModeling(item);
85 65a1ed4b gaqhf
                
86 1efc25a3 gaqhf
                // LineNumber Modeling
87 10872260 gaqhf
                foreach (var item in document.LINENUMBERS)
88
                    LineNumberModeling(item);
89 65a1ed4b gaqhf
                
90 1efc25a3 gaqhf
                // LineRun Line Join
91 3165c259 gaqhf
                foreach (LineNumber lineNumber in document.LINENUMBERS)
92
                    foreach (LineRun run in lineNumber.RUNS)
93
                        JoinRunLine(run);
94 65a1ed4b gaqhf
                
95 1efc25a3 gaqhf
                // TrimLineRun Line Join
96 3165c259 gaqhf
                foreach (TrimLine trimLine in document.TRIMLINES)
97
                    foreach (LineRun run in trimLine.RUNS)
98
                        JoinRunLine(run);
99 65a1ed4b gaqhf
100
                // Note Modeling
101
                foreach (var item in document.NOTES)
102
                    NoteModeling(item);
103
                
104
                // Text Modeling
105
                foreach (var item in document.TEXTINFOS)
106
                    TextModeling(item);
107
108
                // Input LineNumber Attribute
109
                foreach (var item in document.LINENUMBERS)
110
                    InputLineNumberAttribute(item);
111
112
                // Input Symbol Attribute
113
                foreach (var item in document.SYMBOLS)
114
                    InputSymbolAttribute(item);
115 310aeb31 gaqhf
116
                #region 임시 Label
117
                foreach (var item in document.SYMBOLS)
118
                {
119 8b069d9f gaqhf
                    if (item.SPPID.MAPPINGNAME.Contains("Labels - "))
120 310aeb31 gaqhf
                    {
121
                        SPPIDSymbolInfo info = item.SPPID;
122
                        Array points = new double[] { 0, item.SPPID.ORIGINAL_X, item.SPPID.ORIGINAL_Y };
123
                        string symbolUID = item.ATTRIBUTES.Find(x => x.ATTRIBUTE == "SYMBOLOWNER").VALUE;
124
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, symbolUID) as Symbol;
125 8b069d9f gaqhf
                        if (symbol != null)
126 310aeb31 gaqhf
                        {
127 8b069d9f gaqhf
                            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
128
                            LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(item.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: _TargetItem.AsLMRepresentation(), IsLeaderVisible: true);
129
                            LMModelItem _LMModelItem = _TargetItem.ModelItemObject;
130
                            LMAAttributes _Attributes = _LMModelItem.Attributes;
131
132
                            foreach (var attribute in item.ATTRIBUTES)
133 310aeb31 gaqhf
                            {
134 8b069d9f gaqhf
                                AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
135
                                if (mapping != null)
136
                                {
137
                                    LMAAttribute _LMAAttribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
138
                                    if (_LMAAttribute != null)
139
                                        _LMAAttribute.set_Value(attribute.VALUE);
140
                                }
141 310aeb31 gaqhf
                            }
142 8b069d9f gaqhf
                            _LMModelItem.Commit();
143
                            _LMLabelPresist.Commit();
144
                            ReleaseCOMObjects(_TargetItem);
145
                            ReleaseCOMObjects(_LMLabelPresist);
146
                            ReleaseCOMObjects(_LMModelItem);
147
                            ReleaseCOMObjects(_Attributes);
148 310aeb31 gaqhf
                        }
149
                    }
150
                }
151
                #endregion
152 809a7640 gaqhf
            }
153 5e6ecf05 gaqhf
            catch (Exception ex)
154
            {
155
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
156
            }
157
            finally
158
            {
159
                ReleaseCOMObjects(dataSource);
160
                ReleaseCOMObjects(_placement);
161
            }
162 65a1ed4b gaqhf
163
            //System.Windows.Forms.MessageBox.Show("end");
164
        }
165
166
        public void Test()
167
        {
168
            _placement = new Placement();
169
            dataSource = _placement.PIDDataSource;
170
171
            DependencyObject drawingObject = radApp.ActiveDocument.SelectSet[0] as DependencyObject;
172
            if (drawingObject != null)
173
            {
174
                string modelitemID = drawingObject.AttributeSets[0][5].GetValue().ToString();
175
                radApp.ActiveDocument.SelectSet.RemoveAll();
176
                LMPipeRun run = dataSource.GetPipeRun(modelitemID);
177
                _LMAItem item = run.AsLMAItem();
178
                string modelitemID2 = item.Id;
179
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
180
                string afterModelItemID = item.Id;
181
                if (modelitemID2 != afterModelItemID)
182
                {
183
184
                }
185
            }
186 809a7640 gaqhf
        }
187 f1c9dbaa gaqhf
188 8aa6f2db gaqhf
        private void LineModelingByRun(LineRun run)
189 809a7640 gaqhf
        {
190
            Line prevLine = null;
191
            List<Line> lines = new List<Line>();
192
            foreach (var item in run.RUNITEMS)
193
            {
194
                // Line일 경우
195
                if (item.GetType() == typeof(Line))
196
                {
197
                    Line line = item as Line;
198
                    if (prevLine == null)
199
                        lines.Add(line);
200
                    else if (prevLine != null)
201
                    {
202
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
203
                            lines.Add(line);
204
                        else
205 f1c9dbaa gaqhf
                        {
206 f2baa6a3 gaqhf
                            if (lines.Count > 0)
207
                            {
208
                                LineModeling(lines);
209
                                lines.Clear();
210
                            }
211 809a7640 gaqhf
                            lines.Add(line);
212 5dfb8a24 gaqhf
                        }
213
                    }
214
215 809a7640 gaqhf
                    prevLine = line;
216
                }
217
                // Symbol 일 경우
218
                else if (item.GetType() == typeof(Symbol))
219
                {
220 f1c9dbaa gaqhf
                    if (lines.Count > 0)
221 809a7640 gaqhf
                    {
222 f1c9dbaa gaqhf
                        LineModeling(lines);
223 809a7640 gaqhf
                        lines.Clear();
224
                    }
225 5dfb8a24 gaqhf
                }
226
            }
227
228 809a7640 gaqhf
            if (lines.Count > 0)
229
                LineModeling(lines);
230
        }
231 5dfb8a24 gaqhf
232 8aa6f2db gaqhf
        private void SymbolModelingByRun(LineRun run)
233 809a7640 gaqhf
        {
234
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
235
            if (run.RUNITEMS.Count > 0)
236
            {
237
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
238
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
239
240
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
241
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
242
            }
243
244
            Symbol prevSymbol = null;
245
            Symbol targetSymbol = null;
246
            foreach (var item in run.RUNITEMS)
247
            {
248
                if (item.GetType() == typeof(Symbol))
249
                {
250
                    Symbol symbol = item as Symbol;
251
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
252
                    prevSymbol = symbol;
253
                    targetSymbol = symbol;
254
                }
255
                else
256
                {
257
                    targetSymbol = null;
258
                }
259
            }
260
        }
261
262 8aa6f2db gaqhf
        private void SymbolModelingByRunStart(Symbol symbol, LineRun run)
263 809a7640 gaqhf
        {
264
            foreach (var connector in symbol.CONNECTORS)
265
            {
266
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
267
                if (targetItem != null &&
268
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
269
                    !IsSameLineNumber(symbol, targetItem))
270
                {
271
                    SymbolModeling(symbol, targetItem as Symbol, null);
272
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
273
                    {
274
                        object item = run.RUNITEMS[i];
275
                        if (item.GetType() == typeof(Symbol))
276
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol, null);
277
                        else
278
                            break;
279
                    }
280
                    break;
281
                }
282
            }
283
284
285
        }
286
287 8aa6f2db gaqhf
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
288 809a7640 gaqhf
        {
289
            foreach (var connector in symbol.CONNECTORS)
290
            {
291
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
292
                if (targetItem != null &&
293
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
294
                    !IsSameLineNumber(symbol, targetItem))
295
                {
296
                    SymbolModeling(symbol, targetItem as Symbol, null);
297
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
298
                    {
299
                        object item = run.RUNITEMS[i];
300
                        if (item.GetType() == typeof(Symbol))
301
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol, null);
302
                        else
303
                            break;
304
                    }
305
                    break;
306
                }
307
            }
308 5dfb8a24 gaqhf
        }
309 cfda1fed gaqhf
310 809a7640 gaqhf
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
311
        {
312
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
313
                return;
314
315
            LMSymbol _LMSymbol = null;
316
317
            string mappingPath = symbol.SPPID.MAPPINGNAME;
318
            double x = symbol.SPPID.ORIGINAL_X;
319
            double y = symbol.SPPID.ORIGINAL_Y;
320
            int mirror = 0;
321
            double angle = symbol.ANGLE;
322
323
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
324
            {
325
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
326
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
327
                ReleaseCOMObjects(_TargetItem);
328
            }
329
            else if (prevSymbol != null)
330
            {
331
                LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
332
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
333
                double prevX = _PrevSymbol.get_XCoordinate();
334
                double prevY = _PrevSymbol.get_YCoordinate();
335
                if (slopeType == SlopeType.HORIZONTAL)
336
                    y = prevY;
337
                else if (slopeType == SlopeType.VERTICAL)
338
                    x = prevX;
339
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
340
            }
341
            else
342
            {
343
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
344
            }
345
346
347
            if (_LMSymbol != null)
348
            {
349
                _LMSymbol.Commit();
350
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
351 ac78b508 gaqhf
352 4b4dbca9 gaqhf
                foreach (var item in symbol.ChildSymbols)
353
                    CreateChildSymbol(item, _LMSymbol);
354 809a7640 gaqhf
            }
355
356
            ReleaseCOMObjects(_LMSymbol);
357
        }
358
359 4b4dbca9 gaqhf
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
360 ac78b508 gaqhf
        {
361 4b4dbca9 gaqhf
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
362
            double x1 = 0;
363
            double x2 = 0;
364
            double y1 = 0;
365
            double y2 = 0;
366
            symbol2d.Range(out x1, out y1, out x2, out y2);
367
368
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
369
            if (_LMSymbol != null)
370
            {
371
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
372
                foreach (var item in childSymbol.ChildSymbols)
373
                    CreateChildSymbol(item, _LMSymbol);
374
            }
375
            
376 ac78b508 gaqhf
377
            ReleaseCOMObjects(_LMSymbol);
378
        }
379
380 809a7640 gaqhf
        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 f2baa6a3 gaqhf
                        _LMSymbol1 = GetTargetSymbol(connItem as Symbol, line);
432 f1c9dbaa gaqhf
                        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 f2baa6a3 gaqhf
                        _LMSymbol2 = GetTargetSymbol(connItem as Symbol, line);
465 f1c9dbaa gaqhf
                        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 f2baa6a3 gaqhf
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
524
        {
525
            LMSymbol _LMSymbol = null;
526
            foreach (var connector in symbol.CONNECTORS)
527
            {
528
                if (connector.CONNECTEDITEM == line.UID)
529
                {
530
                    if (connector.Index == 0)
531
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
532
                    else
533
                    {
534
                        ChildSymbol child = null;
535
                        foreach (var childSymbol in symbol.ChildSymbols)
536
                        {
537
                            if (childSymbol.Connectors.Contains(connector))
538
                                child = childSymbol;
539
                            else
540
                                child = GetChildSymbolByConnector(childSymbol, connector);
541
542
                            if (child != null)
543
                                break;
544
                        }
545
546
                        if (child != null)
547
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
548
                    }
549
550
                    break;  
551
                }
552
            }
553
554
            return _LMSymbol;
555
        }
556
557
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
558
        {
559
            foreach (var childSymbol in item.ChildSymbols)
560
            {
561
                if (childSymbol.Connectors.Contains(connector))
562
                    return childSymbol;
563
                else
564
                    return GetChildSymbolByConnector(childSymbol, connector);
565
            }
566
567
            return null;
568
        }
569
570 335b7a24 gaqhf
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
571
        {
572
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
573
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
574
575 8b085570 gaqhf
            LMConnector _StartConnector = null;
576
            LMConnector _EndConnector = null;
577 335b7a24 gaqhf
            double lengthStart = double.MaxValue;
578
            double lengthEnd = double.MaxValue;
579
            List<double[]> startPoints = new List<double[]>();
580
            List<double[]> endPoints = new List<double[]>();
581
582
            foreach (var item in connectorVertices)
583
            {
584
                foreach (var point in item.Value)
585
                {
586
                    // Start Point가 Branch
587
                    if (branch.Item2 != null)
588
                    {
589
                        Line targetLine = branch.Item2;
590
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
591
                        if (lengthStart > distance)
592
                        {
593 8b085570 gaqhf
                            _StartConnector = item.Key;
594 335b7a24 gaqhf
                            lengthStart = distance;
595
                            startPoints = item.Value;
596
                        }
597
                    }
598
                    // End Point가 Branch
599
                    if (branch.Item3 != null)
600
                    {
601
                        Line targetLine = branch.Item3;
602
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
603
                        if (lengthEnd > distance)
604
                        {
605 8b085570 gaqhf
                            _EndConnector = item.Key;
606 335b7a24 gaqhf
                            lengthEnd = distance;
607
                            endPoints = item.Value;
608
                        }
609
                    }
610
                }
611
            }
612
            #region Branch가 양쪽 전부일 때
613 8b085570 gaqhf
            if (_StartConnector != null && _StartConnector == _EndConnector)
614 335b7a24 gaqhf
            {
615 8b085570 gaqhf
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
616 335b7a24 gaqhf
617
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
618
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
619
620
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
621 8b085570 gaqhf
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
622 335b7a24 gaqhf
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
623 8b085570 gaqhf
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
624 335b7a24 gaqhf
                   startPoints[startPoints.Count - 1][0],
625
                   startPoints[startPoints.Count - 1][1],
626
                   startPoints[startPoints.Count - 2][0],
627
                   startPoints[startPoints.Count - 2][1]);
628
629
                for (int i = 0; i < startPoints.Count; i++)
630
                {
631
                    double[] point = startPoints[i];
632
                    if (i == 0)
633 8b085570 gaqhf
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
634 335b7a24 gaqhf
                    else if (i == startPoints.Count - 1)
635 8b085570 gaqhf
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
636 335b7a24 gaqhf
                    else
637
                        placeRunInputs.AddPoint(point[0], point[1]);
638
                }
639
640
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
641
                if (_LMConnector != null)
642
                {
643
                    _LMConnector.Commit();
644
                    foreach (var item in lines)
645
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
646
                }
647
648
                foreach (var item in startConnectorVertices)
649
                    ReleaseCOMObjects(item.Key);
650
                foreach (var item in endConnectorVertices)
651
                    ReleaseCOMObjects(item.Key);
652
                ReleaseCOMObjects(placeRunInputs);
653
                ReleaseCOMObjects(_LMAItem);
654
                ReleaseCOMObjects(_LMConnector);
655
            }
656
            #endregion
657 8b085570 gaqhf
            #region 양쪽이 다른 Branch 
658 335b7a24 gaqhf
            else
659
            {
660
                // Branch 시작 Connector
661 8b085570 gaqhf
                if (_StartConnector != null)
662 6b298450 gaqhf
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
663 335b7a24 gaqhf
664 6b298450 gaqhf
                // Branch 끝 Connector
665
                if (_EndConnector != null)
666
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
667
            }
668
            #endregion
669 335b7a24 gaqhf
670 6b298450 gaqhf
            if (_StartConnector != null)
671
                ReleaseCOMObjects(_StartConnector);
672
            if (_EndConnector != null)
673
                ReleaseCOMObjects(_EndConnector);
674
            foreach (var item in connectorVertices)
675
                ReleaseCOMObjects(item.Key);
676
        }
677 335b7a24 gaqhf
678 6b298450 gaqhf
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
679
        {
680
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
681
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
682
            LMConnector _SameRunTargetConnector = null;
683
            LMSymbol _SameRunTargetSymbol = null;
684
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
685
            LMConnector _BranchTargetConnector = null;
686
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
687 335b7a24 gaqhf
688 6b298450 gaqhf
            // 같은 Line Run의 Connector 찾기
689
            foreach (var item in connectorVertices)
690
            {
691
                if (item.Key == _Connector)
692
                    continue;
693 335b7a24 gaqhf
694 6b298450 gaqhf
                if (IsStart &&
695
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
696
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
697
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
698
                {
699
                    _SameRunTargetConnector = item.Key;
700
                    break;
701
                }
702
                else if (!IsStart &&
703
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
704
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
705
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
706
                {
707
                    _SameRunTargetConnector = item.Key;
708
                    break;
709
                }
710
            }
711
712
            // Branch 반대편이 Symbol
713
            if (_SameRunTargetConnector == null)
714
            {
715
                foreach (var line in lines)
716
                {
717
                    foreach (var connector in line.CONNECTORS)
718 335b7a24 gaqhf
                    {
719 6b298450 gaqhf
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
720
                        if (symbol != null)
721 335b7a24 gaqhf
                        {
722 6b298450 gaqhf
                            _SameRunTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
723
                            break;
724 335b7a24 gaqhf
                        }
725
                    }
726 6b298450 gaqhf
                }
727
            }
728 335b7a24 gaqhf
729 6b298450 gaqhf
            // 기존 Connector 제거
730
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
731
            
732
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
733
            if (IsStart)
734
            {
735
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
736
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
737
            }
738
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
739
            else
740
            {
741
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
742
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
743
                    points[points.Count - 1][0],
744
                    points[points.Count - 1][1],
745
                    points[points.Count - 2][0],
746
                    points[points.Count - 2][1]);
747
            }
748 335b7a24 gaqhf
749 6b298450 gaqhf
            for (int i = 0; i < points.Count; i++)
750
            {
751
                double[] point = points[i];
752
                if (i == 0)
753 335b7a24 gaqhf
                {
754 6b298450 gaqhf
                    if (IsStart)
755 335b7a24 gaqhf
                    {
756 6b298450 gaqhf
                        placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
757 335b7a24 gaqhf
                    }
758 6b298450 gaqhf
                    else
759 335b7a24 gaqhf
                    {
760 6b298450 gaqhf
                        if (_SameRunTargetConnector != null)
761
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
762 65a1ed4b gaqhf
                        else if (_SameRunTargetSymbol != null)
763 6b298450 gaqhf
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
764
                        else
765
                            placeRunInputs.AddPoint(point[0], point[1]);
766 335b7a24 gaqhf
                    }
767 6b298450 gaqhf
                }
768
                else if (i == points.Count - 1)
769
                {
770
                    if (IsStart)
771 335b7a24 gaqhf
                    {
772 6b298450 gaqhf
                        if (_SameRunTargetConnector != null)
773
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
774
                        else if (_SameRunTargetSymbol != null)
775
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
776 335b7a24 gaqhf
                        else
777
                            placeRunInputs.AddPoint(point[0], point[1]);
778
                    }
779 6b298450 gaqhf
                    else
780 335b7a24 gaqhf
                    {
781 6b298450 gaqhf
                        placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
782 335b7a24 gaqhf
                    }
783 6b298450 gaqhf
                }
784
                else
785
                    placeRunInputs.AddPoint(point[0], point[1]);
786
            }
787
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
788
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
789 335b7a24 gaqhf
790 6b298450 gaqhf
            if (_LMConnector != null)
791
            {
792
                if (_SameRunTargetConnector != null)
793
                {
794
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
795
                }
796
                else
797
                {
798
                    foreach (var item in lines)
799
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
800 335b7a24 gaqhf
                }
801
802 6b298450 gaqhf
                _LMConnector.Commit();
803
                ReleaseCOMObjects(_LMConnector);
804 335b7a24 gaqhf
            }
805
806 6b298450 gaqhf
            ReleaseCOMObjects(placeRunInputs);
807
            ReleaseCOMObjects(_LMAItem);
808
            if (_BranchTargetConnector != null)
809
                ReleaseCOMObjects(_BranchTargetConnector);
810
            if (_SameRunTargetConnector != null)
811
                ReleaseCOMObjects(_SameRunTargetConnector);
812
            if (_SameRunTargetSymbol != null)
813
                ReleaseCOMObjects(_SameRunTargetSymbol);
814 335b7a24 gaqhf
            foreach (var item in connectorVertices)
815
                ReleaseCOMObjects(item.Key);
816 6b298450 gaqhf
            foreach (var item in branchConnectorVertices)
817
                ReleaseCOMObjects(item.Key);
818 335b7a24 gaqhf
        }
819
820 3165c259 gaqhf
        private void EndBreakModeling(EndBreak endBreak)
821 335b7a24 gaqhf
        {
822 3165c259 gaqhf
            Line ownerLine = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER) as Line;
823 ac78b508 gaqhf
            
824
            if (ownerLine != null)
825 335b7a24 gaqhf
            {
826 3165c259 gaqhf
                LMLabelPersist _LmLabelPersist = null;
827 ac78b508 gaqhf
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(ownerLine.SPPID.ModelItemId);
828 3165c259 gaqhf
829 ac78b508 gaqhf
                LMConnector connectedLMConnector = FindTargetLMConnectorByPoint(connectorVertices, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
830
                if (connectedLMConnector != null)
831 335b7a24 gaqhf
                {
832 ac78b508 gaqhf
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
833
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
834
                    ReleaseCOMObjects(connectedLMConnector);
835
                }
836 3165c259 gaqhf
837 ac78b508 gaqhf
                if (_LmLabelPersist != null)
838
                {
839
                    _LmLabelPersist.Commit();
840
                    ReleaseCOMObjects(_LmLabelPersist);
841 335b7a24 gaqhf
                }
842 ac78b508 gaqhf
                foreach (var item in connectorVertices)
843
                    ReleaseCOMObjects(item.Key);
844 335b7a24 gaqhf
            }
845 3165c259 gaqhf
        }
846 10872260 gaqhf
847 335b7a24 gaqhf
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
848
        {
849 310aeb31 gaqhf
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
850
            _LMAItem item1 = modelItem1.AsLMAItem();
851
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
852
            _LMAItem item2 = modelItem2.AsLMAItem();
853
            
854 335b7a24 gaqhf
            // item2가 item1으로 조인
855
            try
856
            {
857
                _placement.PIDJoinRuns(ref item1, ref item2);
858 65a1ed4b gaqhf
                item1.Commit();
859
                item2.Commit();
860 335b7a24 gaqhf
            }
861
            catch (Exception ex)
862
            {
863
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
864
            }
865
            finally
866
            {
867 310aeb31 gaqhf
                ReleaseCOMObjects(modelItem1);
868
                ReleaseCOMObjects(item1);
869
                ReleaseCOMObjects(modelItem2);
870
                ReleaseCOMObjects(item2);
871 335b7a24 gaqhf
            }
872
        }
873
874
        private void AutoJoinPipeRun(string modelItemId)
875
        {
876 310aeb31 gaqhf
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
877
            _LMAItem item = modelItem.AsLMAItem();
878 65a1ed4b gaqhf
            try
879
            {
880
                string modelitemID = item.Id;
881
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
882
                string afterModelItemID = item.Id;
883
                if (modelitemID != afterModelItemID)
884
                {
885
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
886
                    foreach (var line in lines)
887
                        line.SPPID.ModelItemId = afterModelItemID;
888
                }
889
                item.Commit();
890
            }
891
            catch (Exception ex)
892
            {
893
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
894
            }
895
            finally
896
            {
897 310aeb31 gaqhf
                ReleaseCOMObjects(modelItem);
898
                ReleaseCOMObjects(item);
899 65a1ed4b gaqhf
            }
900 335b7a24 gaqhf
        }
901
902
        private void JoinRunLine(LineRun run)
903
        {
904
            string modelItemId = string.Empty;
905
            foreach (var item in run.RUNITEMS)
906
            {
907
                if (item.GetType() == typeof(Line))
908
                {
909
                    Line line = item as Line;
910
                    if (modelItemId != line.SPPID.ModelItemId)
911
                    {
912
                        AutoJoinPipeRun(line.SPPID.ModelItemId);
913
                        modelItemId = line.SPPID.ModelItemId;
914
                    }
915
                }
916
            }
917
        }
918
919 5e6ecf05 gaqhf
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
920
        {
921
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
922 310aeb31 gaqhf
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
923
924
            if (modelItem != null)
925 5e6ecf05 gaqhf
            {
926 310aeb31 gaqhf
                foreach (LMRepresentation rep in modelItem.Representations)
927 5e6ecf05 gaqhf
                {
928
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
929
                    {
930
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
931
                        connectorVertices.Add(_LMConnector, new List<double[]>());
932
                        dynamic OID = rep.get_GraphicOID();
933 335b7a24 gaqhf
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
934 5e6ecf05 gaqhf
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
935
                        int verticesCount = lineStringGeometry.VertexCount;
936
                        double[] vertices = null;
937
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
938
                        for (int i = 0; i < verticesCount; i++)
939
                        {
940
                            double x = 0;
941
                            double y = 0;
942
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
943 335b7a24 gaqhf
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
944 5e6ecf05 gaqhf
                        }
945
                    }
946
                }
947
948 310aeb31 gaqhf
                ReleaseCOMObjects(modelItem);
949 5e6ecf05 gaqhf
            }
950
951
            return connectorVertices;
952
        }
953
954 56bc67e1 gaqhf
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
955 5e6ecf05 gaqhf
        {
956
            double length = double.MaxValue;
957
            LMConnector targetConnector = null;
958
            foreach (var item in connectorVertices)
959
            {
960
                List<double[]> points = item.Value;
961
                for (int i = 0; i < points.Count - 1; i++)
962
                {
963
                    double[] point1 = points[i];
964
                    double[] point2 = points[i + 1];
965
966 335b7a24 gaqhf
                    double maxLineX = Math.Max(point1[0], point2[0]);
967
                    double minLineX = Math.Min(point1[0], point2[0]);
968
                    double maxLineY = Math.Max(point1[1], point2[1]);
969
                    double minLineY = Math.Min(point1[1], point2[1]);
970
971
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
972
973 56bc67e1 gaqhf
                    // 두직선의 교차점
974
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
975
                    if (crossingPoint != null)
976 5e6ecf05 gaqhf
                    {
977 30a9ffce gaqhf
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
978 335b7a24 gaqhf
                        if (length >= distance)
979 30a9ffce gaqhf
                        {
980 335b7a24 gaqhf
                            if (slope == SlopeType.Slope &&
981
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
982
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
983
                            {
984
                                targetConnector = item.Key;
985
                                length = distance;
986
                            }
987
                            else if (slope == SlopeType.HORIZONTAL &&
988
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
989
                            {
990
                                targetConnector = item.Key;
991
                                length = distance;
992
                            }
993
                            else if (slope == SlopeType.VERTICAL &&
994
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
995
                            {
996
                                targetConnector = item.Key;
997
                                length = distance;
998
                            }
999 30a9ffce gaqhf
                        }
1000 5e6ecf05 gaqhf
                    }
1001
                }
1002
            }
1003
1004
            return targetConnector;
1005
        }
1006
1007 ac78b508 gaqhf
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1008
        {
1009
            double length = double.MaxValue;
1010
            LMConnector targetConnector = null;
1011
            foreach (var item in connectorVertices)
1012
            {
1013
                List<double[]> points = item.Value;
1014
1015
                foreach (double[] point in points)
1016
                {
1017
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1018
                    if (length >= distance)
1019
                    {
1020
                        targetConnector = item.Key;
1021
                        length = distance;
1022
                    }
1023
                }
1024
            }
1025
1026
            return targetConnector;
1027
        }
1028
1029 cfda1fed gaqhf
        private void LineNumberModeling(LineNumber lineNumber)
1030
        {
1031 f4880c6a gaqhf
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
1032
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
1033
            LMConnector connectedLMConnector = FindTargetLMConnectorByPoint(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
1034
            if (connectedLMConnector != null)
1035 10872260 gaqhf
            {
1036
                Array points = new double[] { 0, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y };
1037 f4880c6a gaqhf
                LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: false);
1038
                _LmLabelPresist.Commit();
1039 cfda1fed gaqhf
1040 f4880c6a gaqhf
                foreach (var item in connectorVertices)
1041
                    ReleaseCOMObjects(item.Key);
1042
                if (_LmLabelPresist != null)
1043
                {
1044
                    lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
1045
                    ReleaseCOMObjects(_LmLabelPresist);
1046 10872260 gaqhf
                }
1047
            }
1048 cfda1fed gaqhf
        }
1049
1050 a7e9beec gaqhf
        private void InputLineNumberAttribute(LineNumber lineNumber)
1051
        {
1052
            foreach (var run in lineNumber.RUNS)
1053
            {
1054
                foreach (var item in run.RUNITEMS)
1055
                {
1056
                    if (item.GetType() == typeof(Line))
1057
                    {
1058
                        Line line = item as Line;
1059
                        LMPipeRun _LMPipeRun = dataSource.GetPipeRun(line.SPPID.ModelItemId);
1060
                        foreach (var attribute in lineNumber.ATTRIBUTES)
1061
                        {
1062
                            LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1063
                            if (mapping != null)
1064
                            {
1065
                                LMAAttribute _LMAAttribute = _LMPipeRun.Attributes[mapping.SPPIDATTRIBUTENAME];
1066
                                if (_LMAAttribute != null)
1067
                                {
1068
                                    if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1069
                                        _LMAAttribute.set_Value(attribute.VALUE);
1070
                                    else if (_LMAAttribute.get_Value() != attribute.VALUE)
1071
                                        _LMAAttribute.set_Value(attribute.VALUE);
1072
                                }
1073
                            }
1074
                        }
1075
1076
                        _LMPipeRun.Commit();
1077
                        ReleaseCOMObjects(_LMPipeRun);
1078
                        break;
1079
                    }
1080
                }
1081
            }
1082
        }
1083
1084 1efc25a3 gaqhf
        private void InputSymbolAttribute(Symbol symbol)
1085
        {
1086 ac78b508 gaqhf
            try
1087 1efc25a3 gaqhf
            {
1088 ac78b508 gaqhf
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1089 402ef5b1 gaqhf
                {
1090 ac78b508 gaqhf
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1091 310aeb31 gaqhf
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1092 ea80efaa gaqhf
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
1093 402ef5b1 gaqhf
1094 ac78b508 gaqhf
                    foreach (var item in symbol.PROPERTIES)
1095
                    {
1096 1efc25a3 gaqhf
1097
1098 ac78b508 gaqhf
                    }
1099
1100
                    foreach (var item in symbol.ATTRIBUTES)
1101 65a1ed4b gaqhf
                    {
1102 ac78b508 gaqhf
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
1103
                        if (mapping != null)
1104
                        {
1105
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
1106
                            if (_Attribute != null)
1107 8b069d9f gaqhf
                            {
1108
                                // 임시
1109
                                if (item.ATTRIBUTETYPE == "String")
1110
                                {
1111
                                    if (!string.IsNullOrEmpty(item.VALUE))
1112
                                    {
1113
                                        if (!DBNull.Value.Equals(_Attribute.get_Value()) && !string.IsNullOrEmpty(_Attribute.get_Value()))
1114
                                        {
1115
                                            string value = _Attribute.get_Value() + "\n" + item.VALUE;
1116
                                            _Attribute.set_Value(value);
1117
                                        }
1118
                                        else
1119
                                        {
1120
                                            _Attribute.set_Value(item.VALUE);
1121
                                        }
1122
                                    }
1123
                                }
1124
                                else
1125
                                {
1126
                                    _Attribute.set_Value(item.VALUE);
1127
                                }
1128
                            }
1129 ac78b508 gaqhf
                        }
1130 65a1ed4b gaqhf
                    }
1131 1efc25a3 gaqhf
1132 ac78b508 gaqhf
                    foreach (var item in symbol.ASSOCIATIONS)
1133
                    {
1134 1efc25a3 gaqhf
1135 ac78b508 gaqhf
                    }
1136 402ef5b1 gaqhf
1137 ac78b508 gaqhf
                    ReleaseCOMObjects(_LMSymbol);
1138 ea80efaa gaqhf
                    ReleaseCOMObjects(_Attributes);
1139
                    ReleaseCOMObjects(_LMModelItem);
1140 ac78b508 gaqhf
                }
1141
            }
1142
            catch (Exception ex)
1143
            {
1144
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1145 1efc25a3 gaqhf
            }
1146
        }
1147
1148 cfda1fed gaqhf
        private void TextModeling(Text text)
1149
        {
1150 6b298450 gaqhf
            LMSymbol _LMSymbol = null;
1151
            try
1152
            {
1153 8b069d9f gaqhf
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
1154
                if (text.ASSOCIATION)
1155 ea80efaa gaqhf
                {
1156
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
1157
                    if (owner.GetType() == typeof(Symbol))
1158
                    {
1159
                        Symbol symbol = owner as Symbol;
1160
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1161
                        if (_LMSymbol != null)
1162
                        {
1163
                            Association association = symbol.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
1164
                            List<BaseModel.Attribute> attributes = symbol.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
1165
                            AttributeMapping mapping = null;
1166
                            foreach (var attribute in attributes)
1167
                            {
1168
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
1169
                                if (mapping != null)
1170
                                    break;  
1171
                            }
1172
1173
                            if (mapping != null)
1174
                            {
1175
                                Array array = new double[] { 0, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y };
1176
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: true);
1177
                                if (_LMLabelPersist!=null)
1178
                                {
1179
                                    _LMLabelPersist.Commit();
1180
                                    ReleaseCOMObjects(_LMLabelPersist);
1181
                                }
1182
                            }
1183
                        }
1184
                    }
1185
                    else if (owner.GetType() == typeof(Line))
1186
                    {
1187
1188
                    }
1189
                }
1190
                else
1191
                {
1192
                    LMItemNote _LMItemNote = null;
1193
                    LMAAttribute _LMAAttribute = null;
1194
1195
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
1196
                    _LMSymbol.Commit();
1197
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1198
                    _LMItemNote.Commit();
1199
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1200
                    _LMAAttribute.set_Value(text.VALUE);
1201
                    _LMItemNote.Commit();
1202
1203
                    if (_LMAAttribute != null)
1204
                        ReleaseCOMObjects(_LMAAttribute);
1205
                    if (_LMItemNote != null)
1206
                        ReleaseCOMObjects(_LMItemNote);
1207
                }
1208 6b298450 gaqhf
            }
1209
            catch (Exception ex)
1210
            {
1211 cfda1fed gaqhf
1212 6b298450 gaqhf
            }
1213
            finally
1214
            {
1215
                if (_LMSymbol != null)
1216
                    ReleaseCOMObjects(_LMSymbol);
1217
            }
1218 cfda1fed gaqhf
        }
1219
1220
        private void NoteModeling(Note note)
1221
        {
1222 6b298450 gaqhf
            LMSymbol _LMSymbol = null;
1223
            LMItemNote _LMItemNote = null;
1224
            LMAAttribute _LMAAttribute = null;
1225
1226
            try
1227
            {
1228
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, note.SPPID.ORIGINAL_X, note.SPPID.ORIGINAL_Y);
1229
                _LMSymbol.Commit();
1230
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1231
                _LMItemNote.Commit();
1232
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1233
                _LMAAttribute.set_Value(note.VALUE);
1234
                _LMItemNote.Commit();
1235
            }
1236
            catch (Exception ex)
1237
            {
1238 cfda1fed gaqhf
1239 6b298450 gaqhf
            }
1240
            finally
1241
            {
1242
                if (_LMAAttribute != null)
1243
                    ReleaseCOMObjects(_LMAAttribute);
1244
                if (_LMItemNote != null)
1245
                    ReleaseCOMObjects(_LMItemNote);
1246
                if (_LMSymbol != null)
1247
                    ReleaseCOMObjects(_LMSymbol);
1248
            }
1249
            
1250 cfda1fed gaqhf
        }
1251
1252 5dfb8a24 gaqhf
        public void ReleaseCOMObjects(params object[] objVars)
1253
        {
1254
            int intNewRefCount = 0;
1255
            foreach (object obj in objVars)
1256
            {
1257
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
1258
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
1259
            }
1260
        }
1261 cfda1fed gaqhf
    }
1262
}
클립보드 이미지 추가 (최대 크기: 500 MB)