프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 0a1f51fb

이력 | 보기 | 이력해설 | 다운로드 (59.9 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 26c6f818 gaqhf
                        string symbolUID = item.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL").VALUE;
124
                        object objectItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
125
                        if (objectItem != null)
126 310aeb31 gaqhf
                        {
127 8b069d9f gaqhf
128 26c6f818 gaqhf
                            string sRep = null;
129
                            if (objectItem.GetType() == typeof(Symbol))
130
                                sRep = ((Symbol)objectItem).SPPID.RepresentationId;
131
                            else if (objectItem.GetType() == typeof(Equipment))
132
                                sRep = ((Equipment)objectItem).SPPID.RepresentationId;
133
134
                            if (!string.IsNullOrEmpty(sRep))
135 310aeb31 gaqhf
                            {
136 26c6f818 gaqhf
                                LMSymbol _TargetItem = dataSource.GetSymbol(sRep);
137
                                LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(item.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: _TargetItem.AsLMRepresentation(), IsLeaderVisible: true);
138
                                LMModelItem _LMModelItem = _TargetItem.ModelItemObject;
139
                                LMAAttributes _Attributes = _LMModelItem.Attributes;
140
141
                                foreach (var attribute in item.ATTRIBUTES)
142 8b069d9f gaqhf
                                {
143 26c6f818 gaqhf
                                    if (!string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
144
                                    {
145
                                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
146
                                        if (mapping != null)
147
                                        {
148
                                            LMAAttribute _LMAAttribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
149
                                            if (mapping.SPPIDATTRIBUTENAME.Contains("Description"))
150
                                            {
151
                                                if (!DBNull.Value.Equals(_LMAAttribute.get_Value()) && !string.IsNullOrEmpty(_LMAAttribute.get_Value()))
152
                                                {
153
                                                    string value = _LMAAttribute.get_Value() + "\n" + attribute.VALUE;
154
                                                    _LMAAttribute.set_Value(value);
155
                                                }
156
                                                else
157
                                                {
158
                                                    _LMAAttribute.set_Value(attribute.VALUE);
159
                                                }
160
                                            }
161
                                            else if (_LMAAttribute != null)
162
                                                _LMAAttribute.set_Value(attribute.VALUE);
163
                                        }
164
                                    }
165 8b069d9f gaqhf
                                }
166 f9125a54 gaqhf
167
                                
168
169 26c6f818 gaqhf
                                _LMModelItem.Commit();
170
                                _LMLabelPresist.Commit();
171
                                ReleaseCOMObjects(_TargetItem);
172
                                ReleaseCOMObjects(_LMLabelPresist);
173
                                ReleaseCOMObjects(_LMModelItem);
174
                                ReleaseCOMObjects(_Attributes);
175 310aeb31 gaqhf
                            }
176
                        }
177
                    }
178
                }
179
                #endregion
180 809a7640 gaqhf
            }
181 5e6ecf05 gaqhf
            catch (Exception ex)
182
            {
183
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
184
            }
185
            finally
186
            {
187
                ReleaseCOMObjects(dataSource);
188
                ReleaseCOMObjects(_placement);
189
            }
190 65a1ed4b gaqhf
191
            //System.Windows.Forms.MessageBox.Show("end");
192
        }
193
194
        public void Test()
195
        {
196
            _placement = new Placement();
197
            dataSource = _placement.PIDDataSource;
198
199
            DependencyObject drawingObject = radApp.ActiveDocument.SelectSet[0] as DependencyObject;
200
            if (drawingObject != null)
201
            {
202
                string modelitemID = drawingObject.AttributeSets[0][5].GetValue().ToString();
203
                radApp.ActiveDocument.SelectSet.RemoveAll();
204
                LMPipeRun run = dataSource.GetPipeRun(modelitemID);
205
                _LMAItem item = run.AsLMAItem();
206
                string modelitemID2 = item.Id;
207
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
208
                string afterModelItemID = item.Id;
209
                if (modelitemID2 != afterModelItemID)
210
                {
211
212
                }
213
            }
214 809a7640 gaqhf
        }
215 f1c9dbaa gaqhf
216 8aa6f2db gaqhf
        private void LineModelingByRun(LineRun run)
217 809a7640 gaqhf
        {
218
            Line prevLine = null;
219
            List<Line> lines = new List<Line>();
220
            foreach (var item in run.RUNITEMS)
221
            {
222
                // Line일 경우
223
                if (item.GetType() == typeof(Line))
224
                {
225
                    Line line = item as Line;
226
                    if (prevLine == null)
227
                        lines.Add(line);
228
                    else if (prevLine != null)
229
                    {
230
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
231
                            lines.Add(line);
232
                        else
233 f1c9dbaa gaqhf
                        {
234 f2baa6a3 gaqhf
                            if (lines.Count > 0)
235
                            {
236
                                LineModeling(lines);
237
                                lines.Clear();
238
                            }
239 809a7640 gaqhf
                            lines.Add(line);
240 5dfb8a24 gaqhf
                        }
241
                    }
242
243 809a7640 gaqhf
                    prevLine = line;
244
                }
245
                // Symbol 일 경우
246
                else if (item.GetType() == typeof(Symbol))
247
                {
248 f1c9dbaa gaqhf
                    if (lines.Count > 0)
249 809a7640 gaqhf
                    {
250 f1c9dbaa gaqhf
                        LineModeling(lines);
251 809a7640 gaqhf
                        lines.Clear();
252
                    }
253 5dfb8a24 gaqhf
                }
254
            }
255
256 809a7640 gaqhf
            if (lines.Count > 0)
257
                LineModeling(lines);
258
        }
259 5dfb8a24 gaqhf
260 8aa6f2db gaqhf
        private void SymbolModelingByRun(LineRun run)
261 809a7640 gaqhf
        {
262
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
263
            if (run.RUNITEMS.Count > 0)
264
            {
265
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
266
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
267
268
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
269
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
270
            }
271
272
            Symbol prevSymbol = null;
273
            Symbol targetSymbol = null;
274
            foreach (var item in run.RUNITEMS)
275
            {
276
                if (item.GetType() == typeof(Symbol))
277
                {
278
                    Symbol symbol = item as Symbol;
279
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
280
                    prevSymbol = symbol;
281
                    targetSymbol = symbol;
282
                }
283
                else
284
                {
285
                    targetSymbol = null;
286
                }
287
            }
288
        }
289
290 8aa6f2db gaqhf
        private void SymbolModelingByRunStart(Symbol symbol, LineRun run)
291 809a7640 gaqhf
        {
292
            foreach (var connector in symbol.CONNECTORS)
293
            {
294
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
295
                if (targetItem != null &&
296
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
297
                    !IsSameLineNumber(symbol, targetItem))
298
                {
299
                    SymbolModeling(symbol, targetItem as Symbol, null);
300
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
301
                    {
302
                        object item = run.RUNITEMS[i];
303
                        if (item.GetType() == typeof(Symbol))
304
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol, null);
305
                        else
306
                            break;
307
                    }
308
                    break;
309
                }
310
            }
311
312
313
        }
314
315 8aa6f2db gaqhf
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
316 809a7640 gaqhf
        {
317
            foreach (var connector in symbol.CONNECTORS)
318
            {
319
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
320
                if (targetItem != null &&
321
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
322
                    !IsSameLineNumber(symbol, targetItem))
323
                {
324
                    SymbolModeling(symbol, targetItem as Symbol, null);
325
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
326
                    {
327
                        object item = run.RUNITEMS[i];
328
                        if (item.GetType() == typeof(Symbol))
329
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol, null);
330
                        else
331
                            break;
332
                    }
333
                    break;
334
                }
335
            }
336 5dfb8a24 gaqhf
        }
337 cfda1fed gaqhf
338 809a7640 gaqhf
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
339
        {
340
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
341
                return;
342
343
            LMSymbol _LMSymbol = null;
344
345
            string mappingPath = symbol.SPPID.MAPPINGNAME;
346
            double x = symbol.SPPID.ORIGINAL_X;
347
            double y = symbol.SPPID.ORIGINAL_Y;
348
            int mirror = 0;
349
            double angle = symbol.ANGLE;
350
351
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
352
            {
353
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
354
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
355
                ReleaseCOMObjects(_TargetItem);
356
            }
357
            else if (prevSymbol != null)
358
            {
359
                LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
360
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
361
                double prevX = _PrevSymbol.get_XCoordinate();
362
                double prevY = _PrevSymbol.get_YCoordinate();
363
                if (slopeType == SlopeType.HORIZONTAL)
364
                    y = prevY;
365
                else if (slopeType == SlopeType.VERTICAL)
366
                    x = prevX;
367
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
368
            }
369
            else
370
            {
371
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
372
            }
373
374
375
            if (_LMSymbol != null)
376
            {
377
                _LMSymbol.Commit();
378
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
379 ac78b508 gaqhf
380 4b4dbca9 gaqhf
                foreach (var item in symbol.ChildSymbols)
381
                    CreateChildSymbol(item, _LMSymbol);
382 809a7640 gaqhf
            }
383
384
            ReleaseCOMObjects(_LMSymbol);
385
        }
386
387 4b4dbca9 gaqhf
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
388 ac78b508 gaqhf
        {
389 4b4dbca9 gaqhf
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
390
            double x1 = 0;
391
            double x2 = 0;
392
            double y1 = 0;
393
            double y2 = 0;
394
            symbol2d.Range(out x1, out y1, out x2, out y2);
395
396
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
397
            if (_LMSymbol != null)
398
            {
399
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
400
                foreach (var item in childSymbol.ChildSymbols)
401
                    CreateChildSymbol(item, _LMSymbol);
402
            }
403
            
404 ac78b508 gaqhf
405
            ReleaseCOMObjects(_LMSymbol);
406
        }
407
408 809a7640 gaqhf
        private bool IsSameLineNumber(object item, object targetItem)
409
        {
410
            foreach (var lineNumber in document.LINENUMBERS)
411
            {
412
                foreach (var run in lineNumber.RUNS)
413
                {
414
                    foreach (var runItem in run.RUNITEMS)
415
                    {
416
                        if (runItem == item)
417
                        {
418
                            foreach (var findItem in run.RUNITEMS)
419
                            {
420
                                if (findItem == targetItem)
421
                                {
422
                                    return true;
423
                                }
424
                            }
425
426
                            return false;
427
428
                        }
429
                    }
430
                }
431
            }
432
433
            return false;
434
        }
435
436 1b261371 gaqhf
        private void LineModeling(List<Line> lines)
437
        {
438 335b7a24 gaqhf
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
439 1b261371 gaqhf
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
440 f1c9dbaa gaqhf
            LMSymbol _LMSymbol1 = null;
441
            LMSymbol _LMSymbol2 = null;
442 5e6ecf05 gaqhf
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
443
            LMConnector targetConnector1 = null;
444
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
445
            LMConnector targetConnector2 = null;
446 335b7a24 gaqhf
447
            Line startBranchLine = null;
448
            Line endBranchLine = null;
449
450 1b261371 gaqhf
            for (int i = 0; i < lines.Count; i++)
451
            {
452
                Line line = lines[i];
453 f1c9dbaa gaqhf
                if (i == 0 || i + 1 != lines.Count)
454
                {
455 809a7640 gaqhf
                    // 시작점에 연결된 Symbol 찾기
456 f1c9dbaa gaqhf
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
457
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
458
                    {
459 f2baa6a3 gaqhf
                        _LMSymbol1 = GetTargetSymbol(connItem as Symbol, line);
460 f1c9dbaa gaqhf
                        if (_LMSymbol1 != null)
461
                            placeRunInputs.AddSymbolTarget(_LMSymbol1, line.SPPID.START_X, line.SPPID.START_Y);
462
                        else
463
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
464
                    }
465 335b7a24 gaqhf
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
466 5e6ecf05 gaqhf
                    {
467
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
468 56bc67e1 gaqhf
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
469 5e6ecf05 gaqhf
470
                        if (targetConnector1 != null)
471
                            placeRunInputs.AddConnectorTarget(targetConnector1, line.SPPID.START_X, line.SPPID.START_Y);
472
                        else
473 335b7a24 gaqhf
                        {
474
                            startBranchLine = connItem as Line;
475 5e6ecf05 gaqhf
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
476 335b7a24 gaqhf
                        }
477 5e6ecf05 gaqhf
                    }
478 f1c9dbaa gaqhf
                    else
479
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
480
                }
481
482
                if (i + 1 == lines.Count)
483
                {
484 809a7640 gaqhf
                    // 끝점에 연결된 Symbol 찾기
485 f1c9dbaa gaqhf
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
486 5e6ecf05 gaqhf
487
                    if (i != 0)
488
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
489
490 f1c9dbaa gaqhf
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
491
                    {
492 f2baa6a3 gaqhf
                        _LMSymbol2 = GetTargetSymbol(connItem as Symbol, line);
493 f1c9dbaa gaqhf
                        if (_LMSymbol2 != null)
494
                            placeRunInputs.AddSymbolTarget(_LMSymbol2, line.SPPID.END_X, line.SPPID.END_Y);
495
                        else
496
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
497
                            
498
                    }
499 335b7a24 gaqhf
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
500 5e6ecf05 gaqhf
                    {
501
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
502 56bc67e1 gaqhf
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
503 5e6ecf05 gaqhf
504
                        if (targetConnector2 != null)
505
                            placeRunInputs.AddConnectorTarget(targetConnector2, line.SPPID.END_X, line.SPPID.END_Y);
506
                        else
507 335b7a24 gaqhf
                        {
508
                            endBranchLine = connItem as Line;
509 5e6ecf05 gaqhf
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
510 335b7a24 gaqhf
                        }
511 5e6ecf05 gaqhf
                    }
512 f1c9dbaa gaqhf
                    else
513
                    {
514
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
515
                    }
516
                }
517
            }
518
519
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
520 335b7a24 gaqhf
521 f1c9dbaa gaqhf
            if (_lMConnector != null)
522
            {
523
                foreach (var line in lines)
524 5e6ecf05 gaqhf
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
525 f1c9dbaa gaqhf
                _lMConnector.Commit();
526 335b7a24 gaqhf
                if (startBranchLine != null || endBranchLine != null)
527
                {
528 3165c259 gaqhf
                    BranchLines.Add(new Tuple<string, Line, Line>(_lMConnector.ModelItemID, startBranchLine, endBranchLine));
529 335b7a24 gaqhf
                }
530 1b261371 gaqhf
            }
531 5e6ecf05 gaqhf
532 1b261371 gaqhf
533 f1c9dbaa gaqhf
            if (_LMSymbol1 != null)
534
                ReleaseCOMObjects(_LMSymbol1);
535
            if (_LMSymbol2 != null)
536
                ReleaseCOMObjects(_LMSymbol2);
537 5e6ecf05 gaqhf
            if (targetConnector1 != null)
538
                ReleaseCOMObjects(targetConnector1);
539
            if (targetConnector2 != null)
540
                ReleaseCOMObjects(targetConnector2);
541
            foreach (var item in connectorVertices1)
542
                ReleaseCOMObjects(item.Key);
543
            foreach (var item in connectorVertices2)
544
                ReleaseCOMObjects(item.Key);
545 1b261371 gaqhf
546 f1c9dbaa gaqhf
            ReleaseCOMObjects(_lMConnector);
547 1b261371 gaqhf
            ReleaseCOMObjects(placeRunInputs);
548
            ReleaseCOMObjects(_LMAItem);
549
        }
550
551 f2baa6a3 gaqhf
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
552
        {
553
            LMSymbol _LMSymbol = null;
554
            foreach (var connector in symbol.CONNECTORS)
555
            {
556
                if (connector.CONNECTEDITEM == line.UID)
557
                {
558
                    if (connector.Index == 0)
559
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
560
                    else
561
                    {
562
                        ChildSymbol child = null;
563
                        foreach (var childSymbol in symbol.ChildSymbols)
564
                        {
565
                            if (childSymbol.Connectors.Contains(connector))
566
                                child = childSymbol;
567
                            else
568
                                child = GetChildSymbolByConnector(childSymbol, connector);
569
570
                            if (child != null)
571
                                break;
572
                        }
573
574
                        if (child != null)
575
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
576
                    }
577
578
                    break;  
579
                }
580
            }
581
582
            return _LMSymbol;
583
        }
584
585
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
586
        {
587
            foreach (var childSymbol in item.ChildSymbols)
588
            {
589
                if (childSymbol.Connectors.Contains(connector))
590
                    return childSymbol;
591
                else
592
                    return GetChildSymbolByConnector(childSymbol, connector);
593
            }
594
595
            return null;
596
        }
597
598 335b7a24 gaqhf
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
599
        {
600
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
601
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
602
603 8b085570 gaqhf
            LMConnector _StartConnector = null;
604
            LMConnector _EndConnector = null;
605 335b7a24 gaqhf
            double lengthStart = double.MaxValue;
606
            double lengthEnd = double.MaxValue;
607
            List<double[]> startPoints = new List<double[]>();
608
            List<double[]> endPoints = new List<double[]>();
609
610
            foreach (var item in connectorVertices)
611
            {
612
                foreach (var point in item.Value)
613
                {
614
                    // Start Point가 Branch
615
                    if (branch.Item2 != null)
616
                    {
617
                        Line targetLine = branch.Item2;
618
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
619
                        if (lengthStart > distance)
620
                        {
621 8b085570 gaqhf
                            _StartConnector = item.Key;
622 335b7a24 gaqhf
                            lengthStart = distance;
623
                            startPoints = item.Value;
624
                        }
625
                    }
626
                    // End Point가 Branch
627
                    if (branch.Item3 != null)
628
                    {
629
                        Line targetLine = branch.Item3;
630
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
631
                        if (lengthEnd > distance)
632
                        {
633 8b085570 gaqhf
                            _EndConnector = item.Key;
634 335b7a24 gaqhf
                            lengthEnd = distance;
635
                            endPoints = item.Value;
636
                        }
637
                    }
638
                }
639
            }
640
            #region Branch가 양쪽 전부일 때
641 8b085570 gaqhf
            if (_StartConnector != null && _StartConnector == _EndConnector)
642 335b7a24 gaqhf
            {
643 8b085570 gaqhf
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
644 335b7a24 gaqhf
645
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
646
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
647
648
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
649 8b085570 gaqhf
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
650 335b7a24 gaqhf
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
651 8b085570 gaqhf
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
652 335b7a24 gaqhf
                   startPoints[startPoints.Count - 1][0],
653
                   startPoints[startPoints.Count - 1][1],
654
                   startPoints[startPoints.Count - 2][0],
655
                   startPoints[startPoints.Count - 2][1]);
656
657
                for (int i = 0; i < startPoints.Count; i++)
658
                {
659
                    double[] point = startPoints[i];
660
                    if (i == 0)
661 8b085570 gaqhf
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
662 335b7a24 gaqhf
                    else if (i == startPoints.Count - 1)
663 8b085570 gaqhf
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
664 335b7a24 gaqhf
                    else
665
                        placeRunInputs.AddPoint(point[0], point[1]);
666
                }
667
668
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
669
                if (_LMConnector != null)
670
                {
671
                    _LMConnector.Commit();
672
                    foreach (var item in lines)
673
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
674
                }
675
676
                foreach (var item in startConnectorVertices)
677
                    ReleaseCOMObjects(item.Key);
678
                foreach (var item in endConnectorVertices)
679
                    ReleaseCOMObjects(item.Key);
680
                ReleaseCOMObjects(placeRunInputs);
681
                ReleaseCOMObjects(_LMAItem);
682
                ReleaseCOMObjects(_LMConnector);
683
            }
684
            #endregion
685 8b085570 gaqhf
            #region 양쪽이 다른 Branch 
686 335b7a24 gaqhf
            else
687
            {
688
                // Branch 시작 Connector
689 8b085570 gaqhf
                if (_StartConnector != null)
690 6b298450 gaqhf
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
691 335b7a24 gaqhf
692 6b298450 gaqhf
                // Branch 끝 Connector
693
                if (_EndConnector != null)
694
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
695
            }
696
            #endregion
697 335b7a24 gaqhf
698 6b298450 gaqhf
            if (_StartConnector != null)
699
                ReleaseCOMObjects(_StartConnector);
700
            if (_EndConnector != null)
701
                ReleaseCOMObjects(_EndConnector);
702
            foreach (var item in connectorVertices)
703
                ReleaseCOMObjects(item.Key);
704
        }
705 335b7a24 gaqhf
706 6b298450 gaqhf
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
707
        {
708
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
709
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
710
            LMConnector _SameRunTargetConnector = null;
711
            LMSymbol _SameRunTargetSymbol = null;
712
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
713
            LMConnector _BranchTargetConnector = null;
714
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
715 335b7a24 gaqhf
716 6b298450 gaqhf
            // 같은 Line Run의 Connector 찾기
717
            foreach (var item in connectorVertices)
718
            {
719
                if (item.Key == _Connector)
720
                    continue;
721 335b7a24 gaqhf
722 6b298450 gaqhf
                if (IsStart &&
723
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
724
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
725
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
726
                {
727
                    _SameRunTargetConnector = item.Key;
728
                    break;
729
                }
730
                else if (!IsStart &&
731
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
732
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
733
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
734
                {
735
                    _SameRunTargetConnector = item.Key;
736
                    break;
737
                }
738
            }
739
740
            // Branch 반대편이 Symbol
741
            if (_SameRunTargetConnector == null)
742
            {
743
                foreach (var line in lines)
744
                {
745
                    foreach (var connector in line.CONNECTORS)
746 335b7a24 gaqhf
                    {
747 6b298450 gaqhf
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
748
                        if (symbol != null)
749 335b7a24 gaqhf
                        {
750 6b298450 gaqhf
                            _SameRunTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
751
                            break;
752 335b7a24 gaqhf
                        }
753
                    }
754 6b298450 gaqhf
                }
755
            }
756 335b7a24 gaqhf
757 6b298450 gaqhf
            // 기존 Connector 제거
758
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
759
            
760
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
761
            if (IsStart)
762
            {
763
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
764
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
765
            }
766
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
767
            else
768
            {
769
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
770
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
771
                    points[points.Count - 1][0],
772
                    points[points.Count - 1][1],
773
                    points[points.Count - 2][0],
774
                    points[points.Count - 2][1]);
775
            }
776 335b7a24 gaqhf
777 6b298450 gaqhf
            for (int i = 0; i < points.Count; i++)
778
            {
779
                double[] point = points[i];
780
                if (i == 0)
781 335b7a24 gaqhf
                {
782 6b298450 gaqhf
                    if (IsStart)
783 335b7a24 gaqhf
                    {
784 6b298450 gaqhf
                        placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
785 335b7a24 gaqhf
                    }
786 6b298450 gaqhf
                    else
787 335b7a24 gaqhf
                    {
788 6b298450 gaqhf
                        if (_SameRunTargetConnector != null)
789
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
790 65a1ed4b gaqhf
                        else if (_SameRunTargetSymbol != null)
791 6b298450 gaqhf
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
792
                        else
793
                            placeRunInputs.AddPoint(point[0], point[1]);
794 335b7a24 gaqhf
                    }
795 6b298450 gaqhf
                }
796
                else if (i == points.Count - 1)
797
                {
798
                    if (IsStart)
799 335b7a24 gaqhf
                    {
800 6b298450 gaqhf
                        if (_SameRunTargetConnector != null)
801
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
802
                        else if (_SameRunTargetSymbol != null)
803
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
804 335b7a24 gaqhf
                        else
805
                            placeRunInputs.AddPoint(point[0], point[1]);
806
                    }
807 6b298450 gaqhf
                    else
808 335b7a24 gaqhf
                    {
809 f9125a54 gaqhf
                        if (_BranchTargetConnector != null)
810
                        {
811
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
812
                        }
813 335b7a24 gaqhf
                    }
814 6b298450 gaqhf
                }
815
                else
816
                    placeRunInputs.AddPoint(point[0], point[1]);
817
            }
818
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
819
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
820 335b7a24 gaqhf
821 6b298450 gaqhf
            if (_LMConnector != null)
822
            {
823
                if (_SameRunTargetConnector != null)
824
                {
825
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
826
                }
827
                else
828
                {
829
                    foreach (var item in lines)
830
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
831 335b7a24 gaqhf
                }
832
833 6b298450 gaqhf
                _LMConnector.Commit();
834
                ReleaseCOMObjects(_LMConnector);
835 335b7a24 gaqhf
            }
836
837 6b298450 gaqhf
            ReleaseCOMObjects(placeRunInputs);
838
            ReleaseCOMObjects(_LMAItem);
839
            if (_BranchTargetConnector != null)
840
                ReleaseCOMObjects(_BranchTargetConnector);
841
            if (_SameRunTargetConnector != null)
842
                ReleaseCOMObjects(_SameRunTargetConnector);
843
            if (_SameRunTargetSymbol != null)
844
                ReleaseCOMObjects(_SameRunTargetSymbol);
845 335b7a24 gaqhf
            foreach (var item in connectorVertices)
846
                ReleaseCOMObjects(item.Key);
847 6b298450 gaqhf
            foreach (var item in branchConnectorVertices)
848
                ReleaseCOMObjects(item.Key);
849 335b7a24 gaqhf
        }
850
851 3165c259 gaqhf
        private void EndBreakModeling(EndBreak endBreak)
852 335b7a24 gaqhf
        {
853 10c7195c gaqhf
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
854
            if (ownerObj !=null && ownerObj.GetType() == typeof(Line))
855 335b7a24 gaqhf
            {
856 10c7195c gaqhf
                Line ownerLine = ownerObj as Line;
857 3165c259 gaqhf
                LMLabelPersist _LmLabelPersist = null;
858 ac78b508 gaqhf
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(ownerLine.SPPID.ModelItemId);
859 3165c259 gaqhf
860 ac78b508 gaqhf
                LMConnector connectedLMConnector = FindTargetLMConnectorByPoint(connectorVertices, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
861
                if (connectedLMConnector != null)
862 335b7a24 gaqhf
                {
863 ac78b508 gaqhf
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
864
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
865
                    ReleaseCOMObjects(connectedLMConnector);
866
                }
867 3165c259 gaqhf
868 ac78b508 gaqhf
                if (_LmLabelPersist != null)
869
                {
870
                    _LmLabelPersist.Commit();
871
                    ReleaseCOMObjects(_LmLabelPersist);
872 335b7a24 gaqhf
                }
873 ac78b508 gaqhf
                foreach (var item in connectorVertices)
874
                    ReleaseCOMObjects(item.Key);
875 335b7a24 gaqhf
            }
876 10c7195c gaqhf
            else if (ownerObj != null && ownerObj.GetType() == typeof(Symbol))
877
            {
878
                Symbol ownerSymbol = ownerObj as Symbol;
879
                LMSymbol _LMSymbol = dataSource.GetSymbol(ownerSymbol.SPPID.RepresentationId);
880
881
                LMConnector targetLMConnector = null;
882
                double distance = double.MaxValue;
883
                foreach (LMConnector connector in _LMSymbol.Avoid1Connectors)
884
                {
885 26c6f818 gaqhf
                    if (connector.get_ItemStatus() == "Active")
886 10c7195c gaqhf
                    {
887 26c6f818 gaqhf
                        dynamic OID = connector.get_GraphicOID();
888
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
889
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
890
                        int verticesCount = lineStringGeometry.VertexCount;
891
                        double[] vertices = null;
892
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
893
                        for (int i = 0; i < verticesCount; i++)
894 10c7195c gaqhf
                        {
895 26c6f818 gaqhf
                            double x = 0;
896
                            double y = 0;
897
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
898
899
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, ownerSymbol.SPPID.ORIGINAL_X, ownerSymbol.SPPID.ORIGINAL_Y);
900
                            if (result < distance)
901
                            {
902
                                targetLMConnector = connector;
903
                                distance = result;
904
                            }
905 10c7195c gaqhf
                        }
906
                    }
907
                }
908
909
                foreach (LMConnector connector in _LMSymbol.Avoid2Connectors)
910
                {
911
                    dynamic OID = connector.get_GraphicOID();
912
                    DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
913
                    Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
914
                    int verticesCount = lineStringGeometry.VertexCount;
915
                    double[] vertices = null;
916
                    lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
917
                    for (int i = 0; i < verticesCount; i++)
918
                    {
919
                        double x = 0;
920
                        double y = 0;
921
                        lineStringGeometry.GetVertex(i + 1, ref x, ref y);
922
923
                        double result = SPPIDUtil.CalcPointToPointdDistance(x, y, ownerSymbol.SPPID.ORIGINAL_X, ownerSymbol.SPPID.ORIGINAL_Y);
924
                        if (result < distance)
925
                        {
926
                            targetLMConnector = connector;
927
                            distance = result;
928
                        }
929
                    }
930
                }
931
932
                if (targetLMConnector != null)
933
                {
934
                    LMLabelPersist _LmLabelPersist = null;
935
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
936
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
937
                    if (_LmLabelPersist != null)
938
                    {
939
                        _LmLabelPersist.Commit();
940
                        ReleaseCOMObjects(_LmLabelPersist);
941
                    }
942
                }
943
                ReleaseCOMObjects(_LMSymbol);
944
            }
945 3165c259 gaqhf
        }
946 10872260 gaqhf
947 335b7a24 gaqhf
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
948
        {
949 310aeb31 gaqhf
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
950
            _LMAItem item1 = modelItem1.AsLMAItem();
951
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
952
            _LMAItem item2 = modelItem2.AsLMAItem();
953
            
954 335b7a24 gaqhf
            // item2가 item1으로 조인
955
            try
956
            {
957
                _placement.PIDJoinRuns(ref item1, ref item2);
958 65a1ed4b gaqhf
                item1.Commit();
959
                item2.Commit();
960 335b7a24 gaqhf
            }
961
            catch (Exception ex)
962
            {
963
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
964
            }
965
            finally
966
            {
967 310aeb31 gaqhf
                ReleaseCOMObjects(modelItem1);
968
                ReleaseCOMObjects(item1);
969
                ReleaseCOMObjects(modelItem2);
970
                ReleaseCOMObjects(item2);
971 335b7a24 gaqhf
            }
972
        }
973
974
        private void AutoJoinPipeRun(string modelItemId)
975
        {
976 310aeb31 gaqhf
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
977
            _LMAItem item = modelItem.AsLMAItem();
978 65a1ed4b gaqhf
            try
979
            {
980
                string modelitemID = item.Id;
981
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
982
                string afterModelItemID = item.Id;
983
                if (modelitemID != afterModelItemID)
984
                {
985
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
986
                    foreach (var line in lines)
987
                        line.SPPID.ModelItemId = afterModelItemID;
988
                }
989
                item.Commit();
990
            }
991
            catch (Exception ex)
992
            {
993
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
994
            }
995
            finally
996
            {
997 310aeb31 gaqhf
                ReleaseCOMObjects(modelItem);
998
                ReleaseCOMObjects(item);
999 65a1ed4b gaqhf
            }
1000 335b7a24 gaqhf
        }
1001
1002
        private void JoinRunLine(LineRun run)
1003
        {
1004
            string modelItemId = string.Empty;
1005
            foreach (var item in run.RUNITEMS)
1006
            {
1007
                if (item.GetType() == typeof(Line))
1008
                {
1009
                    Line line = item as Line;
1010
                    if (modelItemId != line.SPPID.ModelItemId)
1011
                    {
1012
                        AutoJoinPipeRun(line.SPPID.ModelItemId);
1013
                        modelItemId = line.SPPID.ModelItemId;
1014
                    }
1015
                }
1016
            }
1017
        }
1018
1019 5e6ecf05 gaqhf
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
1020
        {
1021
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
1022 310aeb31 gaqhf
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
1023
1024
            if (modelItem != null)
1025 5e6ecf05 gaqhf
            {
1026 310aeb31 gaqhf
                foreach (LMRepresentation rep in modelItem.Representations)
1027 5e6ecf05 gaqhf
                {
1028
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1029
                    {
1030
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1031
                        connectorVertices.Add(_LMConnector, new List<double[]>());
1032
                        dynamic OID = rep.get_GraphicOID();
1033 335b7a24 gaqhf
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1034 5e6ecf05 gaqhf
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1035
                        int verticesCount = lineStringGeometry.VertexCount;
1036
                        double[] vertices = null;
1037
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1038
                        for (int i = 0; i < verticesCount; i++)
1039
                        {
1040
                            double x = 0;
1041
                            double y = 0;
1042
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1043 335b7a24 gaqhf
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
1044 5e6ecf05 gaqhf
                        }
1045
                    }
1046
                }
1047
1048 310aeb31 gaqhf
                ReleaseCOMObjects(modelItem);
1049 5e6ecf05 gaqhf
            }
1050
1051
            return connectorVertices;
1052
        }
1053
1054 56bc67e1 gaqhf
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
1055 5e6ecf05 gaqhf
        {
1056
            double length = double.MaxValue;
1057
            LMConnector targetConnector = null;
1058
            foreach (var item in connectorVertices)
1059
            {
1060
                List<double[]> points = item.Value;
1061
                for (int i = 0; i < points.Count - 1; i++)
1062
                {
1063
                    double[] point1 = points[i];
1064
                    double[] point2 = points[i + 1];
1065
1066 335b7a24 gaqhf
                    double maxLineX = Math.Max(point1[0], point2[0]);
1067
                    double minLineX = Math.Min(point1[0], point2[0]);
1068
                    double maxLineY = Math.Max(point1[1], point2[1]);
1069
                    double minLineY = Math.Min(point1[1], point2[1]);
1070
1071
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
1072
1073 56bc67e1 gaqhf
                    // 두직선의 교차점
1074
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
1075
                    if (crossingPoint != null)
1076 5e6ecf05 gaqhf
                    {
1077 30a9ffce gaqhf
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
1078 335b7a24 gaqhf
                        if (length >= distance)
1079 30a9ffce gaqhf
                        {
1080 335b7a24 gaqhf
                            if (slope == SlopeType.Slope &&
1081
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
1082
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1083
                            {
1084
                                targetConnector = item.Key;
1085
                                length = distance;
1086
                            }
1087
                            else if (slope == SlopeType.HORIZONTAL &&
1088
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
1089
                            {
1090
                                targetConnector = item.Key;
1091
                                length = distance;
1092
                            }
1093
                            else if (slope == SlopeType.VERTICAL &&
1094
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1095
                            {
1096
                                targetConnector = item.Key;
1097
                                length = distance;
1098
                            }
1099 30a9ffce gaqhf
                        }
1100 5e6ecf05 gaqhf
                    }
1101
                }
1102
            }
1103
1104
            return targetConnector;
1105
        }
1106
1107 ac78b508 gaqhf
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1108
        {
1109
            double length = double.MaxValue;
1110
            LMConnector targetConnector = null;
1111
            foreach (var item in connectorVertices)
1112
            {
1113
                List<double[]> points = item.Value;
1114
1115
                foreach (double[] point in points)
1116
                {
1117
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1118
                    if (length >= distance)
1119
                    {
1120
                        targetConnector = item.Key;
1121
                        length = distance;
1122
                    }
1123
                }
1124
            }
1125
1126
            return targetConnector;
1127
        }
1128
1129 cfda1fed gaqhf
        private void LineNumberModeling(LineNumber lineNumber)
1130
        {
1131 f4880c6a gaqhf
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
1132
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
1133
            LMConnector connectedLMConnector = FindTargetLMConnectorByPoint(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
1134
            if (connectedLMConnector != null)
1135 10872260 gaqhf
            {
1136
                Array points = new double[] { 0, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y };
1137 f4880c6a gaqhf
                LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: false);
1138
                _LmLabelPresist.Commit();
1139 cfda1fed gaqhf
1140 f4880c6a gaqhf
                foreach (var item in connectorVertices)
1141
                    ReleaseCOMObjects(item.Key);
1142
                if (_LmLabelPresist != null)
1143
                {
1144
                    lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
1145
                    ReleaseCOMObjects(_LmLabelPresist);
1146 10872260 gaqhf
                }
1147
            }
1148 cfda1fed gaqhf
        }
1149
1150 a7e9beec gaqhf
        private void InputLineNumberAttribute(LineNumber lineNumber)
1151
        {
1152
            foreach (var run in lineNumber.RUNS)
1153
            {
1154
                foreach (var item in run.RUNITEMS)
1155
                {
1156
                    if (item.GetType() == typeof(Line))
1157
                    {
1158
                        Line line = item as Line;
1159 f9125a54 gaqhf
                        
1160
                        LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);//dataSource.GetPipeRun(line.SPPID.ModelItemId);
1161 a7e9beec gaqhf
                        foreach (var attribute in lineNumber.ATTRIBUTES)
1162
                        {
1163
                            LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1164
                            if (mapping != null)
1165
                            {
1166 f9125a54 gaqhf
                                LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1167 a7e9beec gaqhf
                                if (_LMAAttribute != null)
1168
                                {
1169
                                    if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1170
                                        _LMAAttribute.set_Value(attribute.VALUE);
1171
                                    else if (_LMAAttribute.get_Value() != attribute.VALUE)
1172
                                        _LMAAttribute.set_Value(attribute.VALUE);
1173
                                }
1174
                            }
1175
                        }
1176
1177 f9125a54 gaqhf
                        _LMModelItem.Commit();
1178
                        ReleaseCOMObjects(_LMModelItem);
1179 a7e9beec gaqhf
                        break;
1180
                    }
1181
                }
1182
            }
1183
        }
1184
1185 1efc25a3 gaqhf
        private void InputSymbolAttribute(Symbol symbol)
1186
        {
1187 ac78b508 gaqhf
            try
1188 1efc25a3 gaqhf
            {
1189 ac78b508 gaqhf
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1190 402ef5b1 gaqhf
                {
1191 ac78b508 gaqhf
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1192 310aeb31 gaqhf
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1193 ea80efaa gaqhf
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
1194 402ef5b1 gaqhf
1195 ac78b508 gaqhf
                    foreach (var item in symbol.PROPERTIES)
1196
                    {
1197 1efc25a3 gaqhf
1198
1199 ac78b508 gaqhf
                    }
1200
1201
                    foreach (var item in symbol.ATTRIBUTES)
1202 65a1ed4b gaqhf
                    {
1203 ac78b508 gaqhf
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
1204 26c6f818 gaqhf
                        if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
1205 ac78b508 gaqhf
                        {
1206
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
1207
                            if (_Attribute != null)
1208 8b069d9f gaqhf
                            {
1209
                                // 임시
1210
                                if (item.ATTRIBUTETYPE == "String")
1211
                                {
1212
                                    if (!string.IsNullOrEmpty(item.VALUE))
1213
                                    {
1214
                                        if (!DBNull.Value.Equals(_Attribute.get_Value()) && !string.IsNullOrEmpty(_Attribute.get_Value()))
1215
                                        {
1216
                                            string value = _Attribute.get_Value() + "\n" + item.VALUE;
1217
                                            _Attribute.set_Value(value);
1218
                                        }
1219
                                        else
1220
                                        {
1221
                                            _Attribute.set_Value(item.VALUE);
1222
                                        }
1223
                                    }
1224
                                }
1225
                                else
1226
                                {
1227
                                    _Attribute.set_Value(item.VALUE);
1228
                                }
1229
                            }
1230 ac78b508 gaqhf
                        }
1231 65a1ed4b gaqhf
                    }
1232 1efc25a3 gaqhf
1233 ac78b508 gaqhf
                    foreach (var item in symbol.ASSOCIATIONS)
1234
                    {
1235 1efc25a3 gaqhf
1236 ac78b508 gaqhf
                    }
1237 402ef5b1 gaqhf
1238 ac78b508 gaqhf
                    ReleaseCOMObjects(_LMSymbol);
1239 ea80efaa gaqhf
                    ReleaseCOMObjects(_Attributes);
1240
                    ReleaseCOMObjects(_LMModelItem);
1241 ac78b508 gaqhf
                }
1242
            }
1243
            catch (Exception ex)
1244
            {
1245
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1246 1efc25a3 gaqhf
            }
1247
        }
1248
1249 cfda1fed gaqhf
        private void TextModeling(Text text)
1250
        {
1251 6b298450 gaqhf
            LMSymbol _LMSymbol = null;
1252
            try
1253
            {
1254 8b069d9f gaqhf
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
1255
                if (text.ASSOCIATION)
1256 ea80efaa gaqhf
                {
1257
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
1258
                    if (owner.GetType() == typeof(Symbol))
1259
                    {
1260
                        Symbol symbol = owner as Symbol;
1261
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1262
                        if (_LMSymbol != null)
1263
                        {
1264
                            Association association = symbol.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
1265
                            List<BaseModel.Attribute> attributes = symbol.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
1266
                            AttributeMapping mapping = null;
1267
                            foreach (var attribute in attributes)
1268
                            {
1269 26c6f818 gaqhf
                                if (string.IsNullOrEmpty(attribute.VALUE) || attribute.VALUE == "None")
1270
                                    continue;
1271
1272 ea80efaa gaqhf
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
1273
                                if (mapping != null)
1274
                                    break;  
1275
                            }
1276
1277
                            if (mapping != null)
1278
                            {
1279
                                Array array = new double[] { 0, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y };
1280
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: true);
1281
                                if (_LMLabelPersist!=null)
1282
                                {
1283 f9125a54 gaqhf
                                    DependencyObject tt = radApp.ActiveDocument.ActiveSheet.DrawingObjects[((dynamic)_LMLabelPersist).GraphicOID];
1284
                                    double x1 = 0;
1285
                                    double x2 = 0;
1286
                                    double y1 = 0;
1287
                                    double y2 = 0;
1288
                                    tt.Range(out x1, out y1, out x2, out y2);
1289 26c6f818 gaqhf
1290 ea80efaa gaqhf
                                    _LMLabelPersist.Commit();
1291 f9125a54 gaqhf
                                    _placement.PIDRemovePlacement(_LMLabelPersist.AsLMRepresentation());
1292
                                    ReleaseCOMObjects(_LMLabelPersist);
1293 0a1f51fb gaqhf
                                    double newX = Math.Abs(x1 - x2) / 2;
1294 f9125a54 gaqhf
                                    double newY = Math.Abs(y1 - y2) / 2;
1295
                                    array = new double[] { 0, text.SPPID.ORIGINAL_X + newX, text.SPPID.ORIGINAL_Y - newY };
1296
                                    _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: true);
1297
                                    _LMLabelPersist.Commit();
1298 ea80efaa gaqhf
                                    ReleaseCOMObjects(_LMLabelPersist);
1299
                                }
1300
                            }
1301
                        }
1302
                    }
1303
                    else if (owner.GetType() == typeof(Line))
1304
                    {
1305
1306
                    }
1307
                }
1308
                else
1309
                {
1310
                    LMItemNote _LMItemNote = null;
1311
                    LMAAttribute _LMAAttribute = null;
1312
1313
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
1314
                    _LMSymbol.Commit();
1315
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1316
                    _LMItemNote.Commit();
1317
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1318
                    _LMAAttribute.set_Value(text.VALUE);
1319
                    _LMItemNote.Commit();
1320
1321
                    if (_LMAAttribute != null)
1322
                        ReleaseCOMObjects(_LMAAttribute);
1323
                    if (_LMItemNote != null)
1324
                        ReleaseCOMObjects(_LMItemNote);
1325
                }
1326 6b298450 gaqhf
            }
1327
            catch (Exception ex)
1328
            {
1329 cfda1fed gaqhf
1330 6b298450 gaqhf
            }
1331
            finally
1332
            {
1333
                if (_LMSymbol != null)
1334
                    ReleaseCOMObjects(_LMSymbol);
1335
            }
1336 cfda1fed gaqhf
        }
1337
1338
        private void NoteModeling(Note note)
1339
        {
1340 6b298450 gaqhf
            LMSymbol _LMSymbol = null;
1341
            LMItemNote _LMItemNote = null;
1342
            LMAAttribute _LMAAttribute = null;
1343
1344
            try
1345
            {
1346
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, note.SPPID.ORIGINAL_X, note.SPPID.ORIGINAL_Y);
1347
                _LMSymbol.Commit();
1348
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1349
                _LMItemNote.Commit();
1350
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1351
                _LMAAttribute.set_Value(note.VALUE);
1352
                _LMItemNote.Commit();
1353
            }
1354
            catch (Exception ex)
1355
            {
1356 cfda1fed gaqhf
1357 6b298450 gaqhf
            }
1358
            finally
1359
            {
1360
                if (_LMAAttribute != null)
1361
                    ReleaseCOMObjects(_LMAAttribute);
1362
                if (_LMItemNote != null)
1363
                    ReleaseCOMObjects(_LMItemNote);
1364
                if (_LMSymbol != null)
1365
                    ReleaseCOMObjects(_LMSymbol);
1366
            }
1367
            
1368 cfda1fed gaqhf
        }
1369
1370 5dfb8a24 gaqhf
        public void ReleaseCOMObjects(params object[] objVars)
1371
        {
1372
            int intNewRefCount = 0;
1373
            foreach (object obj in objVars)
1374
            {
1375
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
1376
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
1377
            }
1378
        }
1379 cfda1fed gaqhf
    }
1380
}
클립보드 이미지 추가 (최대 크기: 500 MB)