프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ c3d2e266

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