프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 4abf114c

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

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