프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ fb386b8c

이력 | 보기 | 이력해설 | 다운로드 (143 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 4d2571ab gaqhf
using System.Data;
7 cfda1fed gaqhf
using Llama;
8
using Plaice;
9 8aa6f2db gaqhf
using Ingr.RAD2D.Interop.RAD2D;
10
using Ingr.RAD2D.Internal;
11
using Ingr.RAD2D.Helper;
12 cfda1fed gaqhf
using Converter.BaseModel;
13
using Converter.SPPID.Model;
14
using Converter.SPPID.Properties;
15
using Converter.SPPID.Util;
16
using Converter.SPPID.DB;
17 5e6ecf05 gaqhf
using Ingr.RAD2D.MacroControls.CmdCtrl;
18
using Ingr.RAD2D;
19 5dfb8a24 gaqhf
using System.Windows;
20 cfda1fed gaqhf
using System.Threading;
21 5dfb8a24 gaqhf
using System.Drawing;
22 cfda1fed gaqhf
using Microsoft.VisualBasic;
23
using Newtonsoft.Json;
24
25 ca214bc3 gaqhf
using DevExpress.XtraSplashScreen;
26 cfda1fed gaqhf
namespace Converter.SPPID
27
{
28
    public class AutoModeling
29
    {
30 809a7640 gaqhf
        Placement _placement;
31
        LMADataSource dataSource;
32 1ba9c671 gaqhf
        dynamic newDrawing;
33 d19ae675 gaqhf
        dynamic application;
34 5e6ecf05 gaqhf
        Ingr.RAD2D.Application radApp;
35 cfda1fed gaqhf
        SPPID_Document document;
36 b65a7e32 gaqhf
        ETCSetting _ETCSetting;
37 f1c9dbaa gaqhf
38 d5ec4d0f gaqhf
        public string DocumentLabelText { get; set; }
39
40 f31645b6 gaqhf
        int CurrentCount;
41 69b7387a gaqhf
        List<Tuple<string, Line, Line>> BranchLines = new List<Tuple<string, Line, Line>>();
42 87f02fc0 gaqhf
        List<string> ZeroLengthModelItemID = new List<string>();
43 47ad9a46 gaqhf
44 d19ae675 gaqhf
        public AutoModeling(SPPID_Document document, dynamic application, Ingr.RAD2D.Application radApp)
45 cfda1fed gaqhf
        {
46
            this.document = document;
47 d19ae675 gaqhf
            this.application = application;
48 5e6ecf05 gaqhf
            this.radApp = radApp;
49 b65a7e32 gaqhf
            this._ETCSetting = ETCSetting.GetInstance();
50 cfda1fed gaqhf
        }
51
52 87f02fc0 gaqhf
        private int CalcProgressCount()
53 f31645b6 gaqhf
        {
54 b265df51 gaqhf
            int EquipCount = 0;
55
            int SymbolCount = 0;
56
            int LineCount = 0;
57
            int NoteCount = 0;
58
            int TextCount = 0;
59
            int EndBreakCount = 0;
60
            int LineNumberCount = 0;
61
62 f31645b6 gaqhf
            EquipCount = document.Equipments.Count;
63
            SymbolCount = document.SYMBOLS.Count;
64
            SymbolCount = SymbolCount * 3;
65
            
66
            foreach (LineNumber lineNumber in document.LINENUMBERS)
67
                foreach (LineRun run in lineNumber.RUNS)
68
                    foreach (var item in run.RUNITEMS)
69
                        if (item.GetType() == typeof(Line))
70
                            LineCount++;
71
            foreach (TrimLine trimLine in document.TRIMLINES)
72
                foreach (LineRun run in trimLine.RUNS)
73
                    foreach (var item in run.RUNITEMS)
74
                        if (item.GetType() == typeof(Line))
75
                            LineCount++;
76
77
            LineCount = LineCount * 2;
78
            NoteCount = document.NOTES.Count;
79
            TextCount = document.TEXTINFOS.Count;
80
            EndBreakCount = document.EndBreaks.Count;
81
            LineNumberCount = document.LINENUMBERS.Count;
82
            LineNumberCount = LineNumberCount * 2;
83
84 b265df51 gaqhf
            return EquipCount + SymbolCount + LineCount + NoteCount + TextCount + EndBreakCount;
85 f31645b6 gaqhf
        }
86
87 02480ac1 gaqhf
        private void SetSystemEditingCommand(bool value)
88
        {
89
            foreach (var item in radApp.Commands)
90
            {
91
                if (item.Argument == "SystemEditingCmd.SystemEditing")
92
                {
93
                    if (item.Checked != value)
94
                    {
95
                        radApp.RunMacro("systemeditingcmd.dll");
96
                        break;
97
                    }
98
99
                }
100
            }
101
        }
102
103 74752074 gaqhf
        /// <summary>
104
        /// 도면 단위당 실행되는 메서드
105
        /// </summary>
106 1ba9c671 gaqhf
        public void Run()
107 c2fef4ca gaqhf
        {
108 224535bb gaqhf
            string drawingNumber = document.DrawingNumber;
109
            string drawingName = document.DrawingName;
110 1ba9c671 gaqhf
            try
111 c2fef4ca gaqhf
            {
112 1ba9c671 gaqhf
                _placement = new Placement();
113
                dataSource = _placement.PIDDataSource;
114
115 224535bb gaqhf
                CreateDocument(ref drawingNumber, ref drawingName);
116 1ba9c671 gaqhf
117 0e0edfad gaqhf
                if (DocumentCoordinateCorrection())
118 310aeb31 gaqhf
                {
119 87f02fc0 gaqhf
                    int AllCount = CalcProgressCount();
120 3939eebf gaqhf
121 965eb728 gaqhf
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
122 20972c61 gaqhf
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllStep, AllCount);
123
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetDocumentName, DocumentLabelText);
124 965eb728 gaqhf
125 4d2571ab gaqhf
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Priority Symbol Modeling");
126
                    List<Symbol> prioritySymbols = GetPrioritySymbol();
127
                    foreach (var item in prioritySymbols)
128
                        SymbolModelingByPriority(item);
129 d1eac84d gaqhf
130 3939eebf gaqhf
                    // Equipment Modeling
131 f31645b6 gaqhf
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
132 3939eebf gaqhf
                    foreach (Equipment equipment in document.Equipments)
133
                        EquipmentModeling(equipment);
134
135
                    // LineRun Symbol Modeling
136 f31645b6 gaqhf
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbols Modeling");
137 3939eebf gaqhf
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
138
                        foreach (LineRun run in lineNumber.RUNS)
139
                            SymbolModelingByRun(run);
140
                    // TrimLineRun Symbol Modeling
141
                    foreach (TrimLine trimLine in document.TRIMLINES)
142
                        foreach (LineRun run in trimLine.RUNS)
143
                            SymbolModelingByRun(run);
144
145
                    // LineRun Line Modeling
146 f31645b6 gaqhf
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Lines Modeling");
147 3939eebf gaqhf
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
148
                        foreach (LineRun run in lineNumber.RUNS)
149
                            LineModelingByRun(run);
150
                    // TrimLineRun Line Modeling
151
                    foreach (TrimLine trimLine in document.TRIMLINES)
152
                        foreach (LineRun run in trimLine.RUNS)
153
                            LineModelingByRun(run);
154
155
                    // Branch Line Modeling
156 f31645b6 gaqhf
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Branch Lines Modeling");
157 3939eebf gaqhf
                    foreach (var item in BranchLines)
158
                        BranchLineModeling(item);
159
160
                    // EndBreak Modeling
161 f31645b6 gaqhf
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
162 3939eebf gaqhf
                    foreach (var item in document.EndBreaks)
163
                        EndBreakModeling(item);
164
165 53c81765 gaqhf
                    // SpecBreak Modeling
166
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "SpecBreaks Modeling");
167
                    foreach (var item in document.SpecBreaks)
168
                        SpecBreakModeling(item);
169
170 3939eebf gaqhf
                    // LineNumber Modeling
171 f31645b6 gaqhf
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "LineNumbers Modeling");
172 3939eebf gaqhf
                    foreach (var item in document.LINENUMBERS)
173
                        LineNumberModeling(item);
174
175 1ed39474 gaqhf
                    // FlowMark Modeling
176 b2d1c1aa gaqhf
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Flow Mark Modeling");
177
                    foreach (var item in document.LINES)
178
                        FlowMarkModeling(item);
179
180 3939eebf gaqhf
                    // Note Modeling
181 f31645b6 gaqhf
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
182 3939eebf gaqhf
                    foreach (var item in document.NOTES)
183
                        NoteModeling(item);
184
185
                    // Text Modeling
186 f31645b6 gaqhf
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
187 3939eebf gaqhf
                    foreach (var item in document.TEXTINFOS)
188
                        TextModeling(item);
189
190
                    // Input LineNumber Attribute
191 3ac248e3 gaqhf
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set LineNumbers Attribute");
192 3939eebf gaqhf
                    foreach (var item in document.LINENUMBERS)
193
                        InputLineNumberAttribute(item);
194
195
                    // Input Symbol Attribute
196 f31645b6 gaqhf
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
197 3939eebf gaqhf
                    foreach (var item in document.SYMBOLS)
198 73415441 gaqhf
                        InputSymbolAttribute(item, item.ATTRIBUTES);
199 3939eebf gaqhf
200 16584d30 gaqhf
                    // Input SpecBreak Attribute
201
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
202
                    foreach (var item in document.SpecBreaks)
203
                        InputSpecBreakAttribute(item);
204
205
                    // Label Symbol Modeling
206 f31645b6 gaqhf
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
207 3939eebf gaqhf
                    foreach (var item in document.SYMBOLS)
208 73415441 gaqhf
                        LabelSymbolModeling(item);
209 f31645b6 gaqhf
210 71ba1ca3 gaqhf
                    // LineRun Line Join
211
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Join LineRuns");
212
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
213
                        foreach (LineRun run in lineNumber.RUNS)
214
                            JoinRunLine(run);
215
                    // TrimLineRun Line Join
216
                    foreach (TrimLine trimLine in document.TRIMLINES)
217
                        foreach (LineRun run in trimLine.RUNS)
218
                            JoinRunLine(run);
219
220 f31645b6 gaqhf
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, AllCount);
221 310aeb31 gaqhf
                }
222 809a7640 gaqhf
            }
223 5e6ecf05 gaqhf
            catch (Exception ex)
224
            {
225
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
226
            }
227
            finally
228
            {
229 1ba9c671 gaqhf
                application.ActiveWindow.Fit();
230 b2d1c1aa gaqhf
231 b66a2996 gaqhf
                if (radApp.ActiveDocument != null)
232 3939eebf gaqhf
                {
233 b66a2996 gaqhf
                    //radApp.ActiveDocument.Save();
234
                    //radApp.ActiveDocument.SaveOnClose = false;
235
                    //radApp.ActiveDocument.Close(false);
236
237 1ba9c671 gaqhf
                    ReleaseCOMObjects(newDrawing);
238 3939eebf gaqhf
                }
239 1ba9c671 gaqhf
240 5e6ecf05 gaqhf
                ReleaseCOMObjects(dataSource);
241
                ReleaseCOMObjects(_placement);
242 965eb728 gaqhf
243 224535bb gaqhf
                Project_DB.InsertDrawingInfo(document.PATH, drawingNumber, drawingName, document);
244 b2d1c1aa gaqhf
                //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
245 965eb728 gaqhf
                SplashScreenManager.CloseForm(false);
246 5e6ecf05 gaqhf
            }
247 65a1ed4b gaqhf
        }
248
249 74752074 gaqhf
        /// <summary>
250
        /// 도면 생성 메서드
251
        /// </summary>
252 224535bb gaqhf
        private void CreateDocument(ref string drawingNumber, ref string drawingName)
253 0e0edfad gaqhf
        {
254 b66a2996 gaqhf
            GetDrawingNameAndNumber(ref drawingName, ref drawingNumber);
255
256
            newDrawing = application.Drawings.Add(document.Unit, document.Template, drawingNumber, drawingName);
257 0e0edfad gaqhf
            application.ActiveWindow.Fit();
258
            Thread.Sleep(1000);
259
            application.ActiveWindow.Zoom = 2000;
260
            Thread.Sleep(2000);
261 b66a2996 gaqhf
        }
262
263 02480ac1 gaqhf
        /// <summary>
264
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
265
        /// </summary>
266
        /// <param name="drawingName"></param>
267
        /// <param name="drawingNumber"></param>
268 b66a2996 gaqhf
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
269
        {
270
            LMDrawings drawings = new LMDrawings();
271
            drawings.Collect(dataSource);
272 1ed39474 gaqhf
            
273 b66a2996 gaqhf
            List<string> drawingNameList = new List<string>();
274
            List<string> drawingNumberList = new List<string>();
275
276
            foreach (LMDrawing item in drawings)
277
            {
278
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
279
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
280
            }
281
282
            int nameLength = drawingName.Length;
283
            while (drawingNameList.Contains(drawingName))
284
            {
285
                if (nameLength == drawingName.Length)
286
                    drawingName += "-1";
287
                else
288
                {
289
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
290
                    drawingName = drawingName.Substring(0, nameLength + 1);
291
                    drawingName += ++index;
292
                }
293
            }
294
295
            int numberLength = drawingNumber.Length;
296
            while (drawingNameList.Contains(drawingNumber))
297
            {
298
                if (numberLength == drawingNumber.Length)
299
                    drawingNumber += "-1";
300
                else
301
                {
302
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
303
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
304
                    drawingNumber += ++index;
305
                }
306
            }
307
308
            ReleaseCOMObjects(drawings);
309 0e0edfad gaqhf
        }
310
311 74752074 gaqhf
        /// <summary>
312
        /// 도면 크기 구하는 메서드
313
        /// </summary>
314
        /// <returns></returns>
315 0e0edfad gaqhf
        private bool DocumentCoordinateCorrection()
316
        {
317 6a7573b0 gaqhf
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
318 0e0edfad gaqhf
            {
319 6a7573b0 gaqhf
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
320 c01ce90b gaqhf
                document.CoordinateCorrection();
321 0e0edfad gaqhf
                return true;
322
            }
323
            else
324
                return false;
325
        }
326
327 74752074 gaqhf
        /// <summary>
328
        /// 라인을 Run 단위로 모델링하는 진입 메서드
329
        /// </summary>
330
        /// <param name="run"></param>
331 8aa6f2db gaqhf
        private void LineModelingByRun(LineRun run)
332 809a7640 gaqhf
        {
333
            Line prevLine = null;
334
            List<Line> lines = new List<Line>();
335
            foreach (var item in run.RUNITEMS)
336
            {
337
                // Line일 경우
338
                if (item.GetType() == typeof(Line))
339
                {
340
                    Line line = item as Line;
341
                    if (prevLine == null)
342
                        lines.Add(line);
343
                    else if (prevLine != null)
344
                    {
345
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
346
                            lines.Add(line);
347
                        else
348 f1c9dbaa gaqhf
                        {
349 f2baa6a3 gaqhf
                            if (lines.Count > 0)
350
                            {
351
                                LineModeling(lines);
352
                                lines.Clear();
353
                            }
354 809a7640 gaqhf
                            lines.Add(line);
355 5dfb8a24 gaqhf
                        }
356
                    }
357
358 809a7640 gaqhf
                    prevLine = line;
359 f31645b6 gaqhf
360
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
361 809a7640 gaqhf
                }
362
                // Symbol 일 경우
363
                else if (item.GetType() == typeof(Symbol))
364
                {
365 f1c9dbaa gaqhf
                    if (lines.Count > 0)
366 809a7640 gaqhf
                    {
367 f1c9dbaa gaqhf
                        LineModeling(lines);
368 809a7640 gaqhf
                        lines.Clear();
369
                    }
370 5dfb8a24 gaqhf
                }
371
            }
372
373 809a7640 gaqhf
            if (lines.Count > 0)
374
                LineModeling(lines);
375
        }
376 5dfb8a24 gaqhf
377 74752074 gaqhf
        /// <summary>
378
        /// 심볼을 Run 단위로 모델링하는 진입 메서드
379
        /// </summary>
380
        /// <param name="run"></param>
381 8aa6f2db gaqhf
        private void SymbolModelingByRun(LineRun run)
382 809a7640 gaqhf
        {
383
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
384
            if (run.RUNITEMS.Count > 0)
385
            {
386
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
387
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
388
389
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
390
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
391
            }
392
393
            Symbol targetSymbol = null;
394
            foreach (var item in run.RUNITEMS)
395
            {
396
                if (item.GetType() == typeof(Symbol))
397
                {
398
                    Symbol symbol = item as Symbol;
399 b2d1c1aa gaqhf
                    SymbolModeling(symbol, targetSymbol);
400 809a7640 gaqhf
                    targetSymbol = symbol;
401
                }
402
                else
403
                {
404
                    targetSymbol = null;
405
                }
406
            }
407
        }
408
409 74752074 gaqhf
        /// <summary>
410
        /// Run에 있는 심볼을 모델링하는데 기준이 Run의 시작점
411
        /// </summary>
412
        /// <param name="symbol"></param>
413
        /// <param name="run"></param>
414 8aa6f2db gaqhf
        private void SymbolModelingByRunStart(Symbol symbol, LineRun run)
415 809a7640 gaqhf
        {
416
            foreach (var connector in symbol.CONNECTORS)
417
            {
418
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
419
                if (targetItem != null &&
420
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
421 74752074 gaqhf
                    !IsSameLineRun(symbol, targetItem))
422 809a7640 gaqhf
                {
423 b2d1c1aa gaqhf
                    SymbolModeling(symbol, targetItem as Symbol);
424 809a7640 gaqhf
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
425
                    {
426
                        object item = run.RUNITEMS[i];
427
                        if (item.GetType() == typeof(Symbol))
428 b2d1c1aa gaqhf
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol);
429 809a7640 gaqhf
                        else
430
                            break;
431
                    }
432
                    break;
433
                }
434
            }
435
436
437
        }
438
439 74752074 gaqhf
        /// <summary>
440
        /// Run에 있는 심볼을 모델링하는데 기준이 Run의 끝점
441
        /// </summary>
442
        /// <param name="symbol"></param>
443
        /// <param name="run"></param>
444 8aa6f2db gaqhf
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
445 809a7640 gaqhf
        {
446
            foreach (var connector in symbol.CONNECTORS)
447
            {
448
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
449
                if (targetItem != null &&
450
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
451 74752074 gaqhf
                    !IsSameLineRun(symbol, targetItem))
452 809a7640 gaqhf
                {
453 b2d1c1aa gaqhf
                    SymbolModeling(symbol, targetItem as Symbol);
454 809a7640 gaqhf
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
455
                    {
456
                        object item = run.RUNITEMS[i];
457
                        if (item.GetType() == typeof(Symbol))
458 b2d1c1aa gaqhf
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol);
459 809a7640 gaqhf
                        else
460
                            break;
461
                    }
462
                    break;
463
                }
464
            }
465 5dfb8a24 gaqhf
        }
466 cfda1fed gaqhf
467 74752074 gaqhf
        /// <summary>
468
        /// 심볼을 실제로 Modeling 메서드
469
        /// </summary>
470
        /// <param name="symbol"></param>
471
        /// <param name="targetSymbol"></param>
472
        /// <param name="prevSymbol"></param>
473 b2d1c1aa gaqhf
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
474 809a7640 gaqhf
        {
475 4d2571ab gaqhf
#if DEBUG
476
            try
477
            {
478
#endif
479 6a7573b0 gaqhf
                // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
480
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
481 fb386b8c gaqhf
                if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
482 6a7573b0 gaqhf
                    return;
483
                // 이미 모델링 됐을 경우
484
                else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
485
                    return;
486
487
                LMSymbol _LMSymbol = null;
488
489
                string mappingPath = symbol.SPPID.MAPPINGNAME;
490
                double x = symbol.SPPID.ORIGINAL_X;
491
                double y = symbol.SPPID.ORIGINAL_Y;
492
                int mirror = 0;
493
                double angle = symbol.ANGLE;
494
495
                SPPIDUtil.ConvertGridPoint(ref x, ref y);
496
497
                // OPC 일경우 180도 일때 Mirror
498
                if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
499
                    mirror = 1;
500
501
                // Mirror 계산
502
                if (symbol.FLIP == 1)
503
                {
504
                    mirror = 1;
505
                    angle += Math.PI;
506
                }
507 809a7640 gaqhf
508 6a7573b0 gaqhf
                if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
509
                {
510
                    LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
511
                    Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
512
                    if (connector != null)
513
                        GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
514 2fdb56bf gaqhf
515 6a7573b0 gaqhf
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
516 1ab9a205 gaqhf
517
                    if (_LMSymbol != null && _TargetItem != null)
518
                    {
519
                        symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
520
                        LMConnector reModelingConnector = FindBreakLineTarget(symbol, targetSymbol);
521
522
                        if (reModelingConnector != null)
523
                            ReModelingLMConnector(reModelingConnector);
524
                    }
525
526 6a7573b0 gaqhf
                    ReleaseCOMObjects(_TargetItem);
527
                }
528
                else
529
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
530 809a7640 gaqhf
531
532 6a7573b0 gaqhf
                if (_LMSymbol != null)
533
                {
534
                    _LMSymbol.Commit();
535
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
536 224535bb gaqhf
                    symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
537 6a7573b0 gaqhf
                    symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
538 ac78b508 gaqhf
539 6a7573b0 gaqhf
                    foreach (var item in symbol.ChildSymbols)
540
                        CreateChildSymbol(item, _LMSymbol);
541
                }
542 809a7640 gaqhf
543 6a7573b0 gaqhf
                ReleaseCOMObjects(_LMSymbol);
544
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
545 4d2571ab gaqhf
#if DEBUG
546
547
            }
548
            catch (Exception ex)
549
            {
550
                System.Windows.Forms.MessageBox.Show(ex.StackTrace);
551
            }
552
#endif
553 809a7640 gaqhf
        }
554
555 d1eac84d gaqhf
        /// <summary>
556
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
557
        /// </summary>
558
        /// <param name="targetConnector"></param>
559
        /// <param name="targetSymbol"></param>
560
        /// <param name="x"></param>
561
        /// <param name="y"></param>
562
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
563
        {
564
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
565 2fdb56bf gaqhf
566
            double[] range = null;
567 d1eac84d gaqhf
            List<double[]> points = new List<double[]>();
568 2fdb56bf gaqhf
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
569
            double x1 = range[0];
570
            double y1 = range[1];
571
            double x2 = range[2];
572
            double y2 = range[3];
573 d1eac84d gaqhf
574
            // Origin 기준 Connector의 위치차이
575
            double sceneX = 0;
576
            double sceneY = 0;
577
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
578
            double originX = 0;
579
            double originY = 0;
580
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
581
            double gapX = originX - sceneX;
582
            double gapY = originY - sceneY;
583
584
            // SPPID Symbol과 ID2 심볼의 크기 차이
585
            double sizeWidth = 0;
586
            double sizeHeight = 0;
587
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
588
            double percentX = (x2 - x1) / sizeWidth;
589
            double percentY = (y2 - y1) / sizeHeight;
590
591
            double SPPIDgapX = gapX * percentX;
592
            double SPPIDgapY = gapY * percentY;
593
594
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
595
            double distance = double.MaxValue;
596
            double[] resultPoint;
597
            foreach (var point in points)
598
            {
599
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
600
                if (distance > result)
601
                {
602
                    distance = result;
603
                    resultPoint = point;
604
                    x = point[0];
605
                    y = point[1];
606
                }
607
            }
608 2fdb56bf gaqhf
609
            ReleaseCOMObjects(_TargetItem);
610
        }
611
612
        /// <summary>
613
        /// SPPID Symbol의 Range를 구한다.
614
        /// </summary>
615
        /// <param name="symbol"></param>
616
        /// <param name="range"></param>
617
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
618
        {
619
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
620
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
621
            double x1 = 0;
622
            double y1 = 0;
623
            double x2 = 0;
624
            double y2 = 0;
625
            symbol2d.Range(out x1, out y1, out x2, out y2);
626
            range = new double[] { x1, y1, x2, y2 };
627
628
            for (int i = 1; i < int.MaxValue; i++)
629
            {
630
                double connX = 0;
631
                double connY = 0;
632
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
633
                    points.Add(new double[] { connX, connY });
634
                else
635
                    break;
636
            }
637
638
            foreach (var childSymbol in symbol.ChildSymbols)
639
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
640
641
            ReleaseCOMObjects(_TargetItem);
642
        }
643
644
        /// <summary>
645
        /// Child Modeling 된 Symbol의 Range를 구한다.
646
        /// </summary>
647
        /// <param name="childSymbol"></param>
648
        /// <param name="range"></param>
649
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
650
        {
651
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
652
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
653
            double x1 = 0;
654
            double y1 = 0;
655
            double x2 = 0;
656
            double y2 = 0;
657
            symbol2d.Range(out x1, out y1, out x2, out y2);
658
            range[0] = Math.Min(range[0], x1);
659
            range[1] = Math.Min(range[1], y1);
660
            range[2] = Math.Max(range[2], x2);
661
            range[3] = Math.Max(range[3], y2);
662
663
            for (int i = 1; i < int.MaxValue; i++)
664
            {
665
                double connX = 0;
666
                double connY = 0;
667
                if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
668
                    points.Add(new double[] { connX, connY });
669
                else
670
                    break;
671
            }
672
673
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
674
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
675
676
            ReleaseCOMObjects(_ChildSymbol);
677 d1eac84d gaqhf
        }
678
679
        /// <summary>
680
        /// Label Symbol Modeling
681
        /// </summary>
682
        /// <param name="symbol"></param>
683 73415441 gaqhf
        private void LabelSymbolModeling(Symbol symbol)
684
        {
685 fb386b8c gaqhf
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
686 73415441 gaqhf
            {
687 fb386b8c gaqhf
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
688
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
689
                    return;
690
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
691
692
                string symbolUID = itemAttribute.VALUE;
693
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
694
                if (targetItem != null &&
695
                    (targetItem.GetType() == typeof(Symbol) ||
696
                    targetItem.GetType() == typeof(Equipment)))
697 73415441 gaqhf
                {
698 fb386b8c gaqhf
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
699
                    string sRep = null;
700
                    if (targetItem.GetType() == typeof(Symbol))
701
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
702
                    else if (targetItem.GetType() == typeof(Equipment))
703
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
704
                    if (!string.IsNullOrEmpty(sRep))
705 73415441 gaqhf
                    {
706 fb386b8c gaqhf
                        // LEADER Line 검사
707
                        bool leaderLine = false;
708
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
709
                        if (symbolMapping != null)
710
                            leaderLine = symbolMapping.LEADERLINE;
711
712
                        // Target Symbol Item 가져오고 Label Modeling
713
                        LMSymbol _TargetItem = dataSource.GetSymbol(sRep);
714
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: _TargetItem.AsLMRepresentation(), IsLeaderVisible: leaderLine);
715
716
                        //Leader 선 센터로
717
                        if (_LMLabelPresist != null)
718 73415441 gaqhf
                        {
719 fb386b8c gaqhf
                            // Target Item에 Label의 Attribute Input
720
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
721
722
                            string OID = _LMLabelPresist.get_GraphicOID();
723
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
724
                            if (dependency != null)
725 73415441 gaqhf
                            {
726 fb386b8c gaqhf
                                bool result = false;
727
                                foreach (var attributes in dependency.AttributeSets)
728 73415441 gaqhf
                                {
729 fb386b8c gaqhf
                                    foreach (var attribute in attributes)
730 73415441 gaqhf
                                    {
731 fb386b8c gaqhf
                                        string name = attribute.Name;
732
                                        string value = attribute.GetValue().ToString();
733
                                        if (name == "DrawingItemType" && value == "LabelPersist")
734 73415441 gaqhf
                                        {
735 fb386b8c gaqhf
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
736 b2d1c1aa gaqhf
                                            {
737 fb386b8c gaqhf
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
738
                                                {
739
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
740
                                                    double prevX = _TargetItem.get_XCoordinate();
741
                                                    double prevY = _TargetItem.get_YCoordinate();
742
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
743
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
744
                                                    result = true;
745
                                                    break;
746
                                                }
747 b2d1c1aa gaqhf
                                            }
748 73415441 gaqhf
                                        }
749 fb386b8c gaqhf
750
                                        if (result)
751
                                            break;
752 73415441 gaqhf
                                    }
753 b2d1c1aa gaqhf
754
                                    if (result)
755
                                        break;
756 73415441 gaqhf
                                }
757
                            }
758 fb386b8c gaqhf
759
                            _LMLabelPresist.Commit();
760
                            ReleaseCOMObjects(_LMLabelPresist);
761 73415441 gaqhf
                        }
762
763 fb386b8c gaqhf
                        ReleaseCOMObjects(_TargetItem);
764 b2d1c1aa gaqhf
                    }
765 73415441 gaqhf
                }
766 fb386b8c gaqhf
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
767 0860c756 gaqhf
                {
768 fb386b8c gaqhf
                    Line targetLine = targetItem as Line;
769
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
770
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
771
                    if (connectedLMConnector != null)
772 0860c756 gaqhf
                    {
773 fb386b8c gaqhf
                        // LEADER Line 검사
774
                        bool leaderLine = false;
775
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
776
                        if (symbolMapping != null)
777
                            leaderLine = symbolMapping.LEADERLINE;
778
779
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
780
                        if (_LMLabelPresist != null)
781
                        {
782
                            _LMLabelPresist.Commit();
783
                            ReleaseCOMObjects(_LMLabelPresist);
784
                        }
785
                        ReleaseCOMObjects(connectedLMConnector);
786 0860c756 gaqhf
                    }
787
788 fb386b8c gaqhf
                    foreach (var item in connectorVertices)
789
                        if (item.Key != null)
790
                            ReleaseCOMObjects(item.Key);
791
                }
792 0860c756 gaqhf
            }
793 73415441 gaqhf
794
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
795
        }
796
797 74752074 gaqhf
        /// <summary>
798
        /// Equipment를 실제로 Modeling 메서드
799
        /// </summary>
800
        /// <param name="equipment"></param>
801 b9e9f4c8 gaqhf
        private void EquipmentModeling(Equipment equipment)
802
        {
803
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
804
                return;
805
806
            LMSymbol _LMSymbol = null;
807
            LMSymbol targetItem = null;
808
            string mappingPath = equipment.SPPID.MAPPINGNAME;
809
            double x = equipment.SPPID.ORIGINAL_X;
810
            double y = equipment.SPPID.ORIGINAL_Y;
811
            int mirror = 0;
812
            double angle = equipment.ANGLE;
813
814 20972c61 gaqhf
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
815
816 b9e9f4c8 gaqhf
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
817
            if (connector != null)
818
            {
819
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
820
                if (connEquipment != null)
821
                {
822
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
823
                        EquipmentModeling(connEquipment);
824
825
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
826
                    {
827
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
828
                        if (targetItem != null)
829
                        {
830
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
831
                        }
832
                        else
833
                        {
834
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
835
                        }
836
                    }
837
                    else
838
                    {
839
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
840
                    }
841
                }
842
                else
843
                {
844
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
845
                }
846
            }
847
            else
848
            {
849
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
850
            }
851
852
            if (_LMSymbol != null)
853
            {
854
                _LMSymbol.Commit();
855
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
856 20972c61 gaqhf
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
857 b9e9f4c8 gaqhf
                ReleaseCOMObjects(_LMSymbol);
858
            }
859
860
            if (targetItem != null)
861
            {
862
                ReleaseCOMObjects(targetItem);
863
            }
864
            
865
            ReleaseCOMObjects(_LMSymbol);
866 f31645b6 gaqhf
867
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
868 b9e9f4c8 gaqhf
        }
869
870 4d2571ab gaqhf
        private void SymbolModelingByPriority(Symbol symbol)
871 d1eac84d gaqhf
        {
872 4d2571ab gaqhf
            // Angle, Center, 우선순위 모델링
873
            SymbolModeling(symbol, null);
874
            List<Symbol> group = new List<Symbol>() { symbol };
875
            SPPIDUtil.FindConnectedSymbolGroup(document, symbol, group);
876 d1eac84d gaqhf
877 4d2571ab gaqhf
            List<Symbol> endModeling = new List<Symbol>() { symbol };
878
            while (endModeling.Count != group.Count)
879
            {
880
                foreach (var item in group)
881 d1eac84d gaqhf
                {
882 4d2571ab gaqhf
                    if (!endModeling.Contains(item))
883
                    {
884
                        bool result = false;
885
                        foreach (var connector in item.CONNECTORS)
886
                        {
887
                            Symbol connSymbol = group.Find(x => x.UID == connector.CONNECTEDITEM);
888
                            if (connSymbol == item)
889
                                throw new Exception(connSymbol.UID);
890
891
                            if (connSymbol != null && endModeling.Contains(connSymbol))
892
                            {
893
                                SymbolModeling(item, connSymbol);
894
                                endModeling.Add(item);
895
                                result = true;
896
                                break;
897
                            }
898
                        }
899
900
                        if (result)
901
                            break;
902
                    }
903 d1eac84d gaqhf
                }
904
            }
905
        }
906
907
        /// <summary>
908 74752074 gaqhf
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
909
        /// </summary>
910
        /// <param name="childSymbol"></param>
911
        /// <param name="parentSymbol"></param>
912 4b4dbca9 gaqhf
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
913 ac78b508 gaqhf
        {
914 4b4dbca9 gaqhf
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
915
            double x1 = 0;
916
            double x2 = 0;
917
            double y1 = 0;
918
            double y2 = 0;
919
            symbol2d.Range(out x1, out y1, out x2, out y2);
920
921
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
922
            if (_LMSymbol != null)
923
            {
924
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
925
                foreach (var item in childSymbol.ChildSymbols)
926
                    CreateChildSymbol(item, _LMSymbol);
927
            }
928
            
929 ac78b508 gaqhf
930
            ReleaseCOMObjects(_LMSymbol);
931
        }
932
933 74752074 gaqhf
        /// <summary>
934
        /// item이 TargetItem과 같은 LineRun에 있는지 검사
935
        /// </summary>
936
        /// <param name="item"></param>
937
        /// <param name="targetItem"></param>
938
        /// <returns></returns>
939
        private bool IsSameLineRun(object item, object targetItem)
940 809a7640 gaqhf
        {
941
            foreach (var lineNumber in document.LINENUMBERS)
942
            {
943
                foreach (var run in lineNumber.RUNS)
944
                {
945
                    foreach (var runItem in run.RUNITEMS)
946
                    {
947
                        if (runItem == item)
948
                        {
949
                            foreach (var findItem in run.RUNITEMS)
950
                            {
951
                                if (findItem == targetItem)
952
                                {
953
                                    return true;
954
                                }
955
                            }
956
957
                            return false;
958
959
                        }
960
                    }
961
                }
962
            }
963
964
            return false;
965
        }
966
967 74752074 gaqhf
        /// <summary>
968
        /// Line을 실제로 모델링하는 메서드
969
        /// </summary>
970
        /// <param name="lines"></param>
971 1b261371 gaqhf
        private void LineModeling(List<Line> lines)
972
        {
973 335b7a24 gaqhf
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
974 1b261371 gaqhf
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
975 f1c9dbaa gaqhf
            LMSymbol _LMSymbol1 = null;
976
            LMSymbol _LMSymbol2 = null;
977 5e6ecf05 gaqhf
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
978
            LMConnector targetConnector1 = null;
979
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
980
            LMConnector targetConnector2 = null;
981 1ab9a205 gaqhf
            
982 335b7a24 gaqhf
            Line startBranchLine = null;
983
            Line endBranchLine = null;
984
985 0bbd73b5 gaqhf
            // Type, Line, TargetObjet, x, y
986
            List<Tuple<string, Line, object, double, double>> linePointInfo = new List<Tuple<string, Line, object, double, double>>();
987
            // Point 정리
988 1b261371 gaqhf
            for (int i = 0; i < lines.Count; i++)
989
            {
990
                Line line = lines[i];
991 f1c9dbaa gaqhf
                if (i == 0 || i + 1 != lines.Count)
992
                {
993 809a7640 gaqhf
                    // 시작점에 연결된 Symbol 찾기
994 f1c9dbaa gaqhf
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
995
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
996
                    {
997 2fdb56bf gaqhf
                        Symbol symbol1 = connItem as Symbol;
998
                        _LMSymbol1 = GetTargetSymbol(symbol1, line);
999 f1c9dbaa gaqhf
                        if (_LMSymbol1 != null)
1000 2fdb56bf gaqhf
                        {
1001
                            double x = line.SPPID.START_X;
1002
                            double y = line.SPPID.START_Y;
1003
                            Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, line.UID, symbol1);
1004
                            if (connector != null)
1005
                            {
1006
                                GetTargetSymbolConnectorPoint(connector, symbol1, ref x, ref y);
1007
                                line.SPPID.START_X = x;
1008
                                line.SPPID.START_Y = y;
1009
                            }
1010
1011 0bbd73b5 gaqhf
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>("SYMBOL", line, _LMSymbol1, x, y));
1012 2fdb56bf gaqhf
                        }
1013 f1c9dbaa gaqhf
                        else
1014 0bbd73b5 gaqhf
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
1015 f1c9dbaa gaqhf
                    }
1016 335b7a24 gaqhf
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
1017 5e6ecf05 gaqhf
                    {
1018
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
1019 56bc67e1 gaqhf
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
1020 5e6ecf05 gaqhf
1021
                        if (targetConnector1 != null)
1022 0bbd73b5 gaqhf
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>("LINE", line, targetConnector1, line.SPPID.START_X, line.SPPID.START_Y));
1023 5e6ecf05 gaqhf
                        else
1024 335b7a24 gaqhf
                        {
1025
                            startBranchLine = connItem as Line;
1026 0bbd73b5 gaqhf
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
1027 335b7a24 gaqhf
                        }
1028 5e6ecf05 gaqhf
                    }
1029 f1c9dbaa gaqhf
                    else
1030 0bbd73b5 gaqhf
                        linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
1031 ca7d0ada gaqhf
                        
1032 f1c9dbaa gaqhf
                }
1033
                if (i + 1 == lines.Count)
1034
                {
1035 809a7640 gaqhf
                    // 끝점에 연결된 Symbol 찾기
1036 f1c9dbaa gaqhf
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
1037 5e6ecf05 gaqhf
1038
                    if (i != 0)
1039 0bbd73b5 gaqhf
                        linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
1040 5e6ecf05 gaqhf
1041 f1c9dbaa gaqhf
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
1042
                    {
1043 2fdb56bf gaqhf
                        Symbol symbol2 = connItem as Symbol;
1044 f2baa6a3 gaqhf
                        _LMSymbol2 = GetTargetSymbol(connItem as Symbol, line);
1045 f1c9dbaa gaqhf
                        if (_LMSymbol2 != null)
1046 2fdb56bf gaqhf
                        {
1047
                            double x = line.SPPID.END_X;
1048
                            double y = line.SPPID.END_Y;
1049
                            Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, line.UID, symbol2);
1050
                            if (connector != null)
1051
                            {
1052
                                GetTargetSymbolConnectorPoint(connector, symbol2, ref x, ref y);
1053
                                line.SPPID.END_X = x;
1054
                                line.SPPID.END_Y = y;
1055
                            }
1056
1057 0bbd73b5 gaqhf
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>("SYMBOL", line, _LMSymbol2, x, y));
1058 2fdb56bf gaqhf
                        }
1059 f1c9dbaa gaqhf
                        else
1060 0bbd73b5 gaqhf
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.END_X, line.SPPID.END_Y));
1061 f1c9dbaa gaqhf
                    }
1062 335b7a24 gaqhf
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
1063 5e6ecf05 gaqhf
                    {
1064
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
1065 56bc67e1 gaqhf
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
1066 5e6ecf05 gaqhf
1067
                        if (targetConnector2 != null)
1068 0bbd73b5 gaqhf
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>("LINE", line, targetConnector2, line.SPPID.END_X, line.SPPID.END_Y));
1069 5e6ecf05 gaqhf
                        else
1070 335b7a24 gaqhf
                        {
1071
                            endBranchLine = connItem as Line;
1072 0bbd73b5 gaqhf
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.END_X, line.SPPID.END_Y));
1073 335b7a24 gaqhf
                        }
1074 5e6ecf05 gaqhf
                    }
1075 f1c9dbaa gaqhf
                    else
1076 0bbd73b5 gaqhf
                        linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.END_X, line.SPPID.END_Y));
1077 f1c9dbaa gaqhf
                }
1078
            }
1079
1080 0bbd73b5 gaqhf
            double prevX = double.NaN;
1081
            double prevY = double.NaN;
1082
            SlopeType prevSlopeType = SlopeType.None;
1083 ca7d0ada gaqhf
            for (int i = 0; i < linePointInfo.Count; i++)
1084
            {
1085 0bbd73b5 gaqhf
                Tuple<string, Line, object, double, double> item = linePointInfo[i];
1086
                Line line = item.Item2;
1087
                double x = item.Item4;
1088
                double y = item.Item5;
1089
                SlopeType slopeType = SPPIDUtil.CalcSlope(line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
1090
                // Symbol일 경우 바로 Input Point
1091 ca7d0ada gaqhf
                if (item.Item1 == "SYMBOL")
1092 0bbd73b5 gaqhf
                    placeRunInputs.AddSymbolTarget(item.Item3 as LMSymbol, x, y);
1093 b66a2996 gaqhf
                else
1094 0bbd73b5 gaqhf
                {
1095 b66a2996 gaqhf
                    SPPIDUtil.ConvertGridPoint(ref x, ref y);
1096
                    // i == 0은 그대로 사용
1097
                    if (i != 0)
1098 0bbd73b5 gaqhf
                    {
1099 b66a2996 gaqhf
                        Tuple<string, Line, object, double, double> prevItem = linePointInfo[i - 1];
1100
                        // y 좌표가 같아야함 및 Symbol 좌표가 정확하지 않으므로 한번더 보정
1101
                        if (prevSlopeType == SlopeType.HORIZONTAL)
1102
                        {
1103
                            y = prevY;
1104
                            SPPIDUtil.ConvertGridPointOnlyOnePoint(ref y);
1105
                        }
1106
                        else if (prevSlopeType == SlopeType.VERTICAL)
1107
                        {
1108
                            x = prevX;
1109
                            SPPIDUtil.ConvertGridPointOnlyOnePoint(ref x);
1110
                        }
1111
1112
                        // 마지막이 Symbol일 경우는 Symbol의 좌표를 따라감
1113
                        if (i + 1 == linePointInfo.Count - 1 && linePointInfo[i + 1].Item1 == "SYMBOL")
1114
                        {
1115
                            Line nextLine = linePointInfo[i + 1].Item2;
1116
                            SlopeType nextSlopeType = SPPIDUtil.CalcSlope(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y);
1117
                            if (slopeType == SlopeType.HORIZONTAL)
1118
                                y = linePointInfo[i + 1].Item5;
1119
                            else if (slopeType == SlopeType.VERTICAL)
1120
                                x = linePointInfo[i + 1].Item4;
1121
                        }
1122 0bbd73b5 gaqhf
                    }
1123
1124 b66a2996 gaqhf
                    if (item.Item1 == "LINE")
1125
                        placeRunInputs.AddConnectorTarget(item.Item3 as LMConnector, x, y);
1126
                    else
1127
                        placeRunInputs.AddPoint(x, y);
1128
                }
1129 0bbd73b5 gaqhf
1130
                prevX = x;
1131
                prevY = y;
1132
                prevSlopeType = slopeType;
1133 ca7d0ada gaqhf
            }
1134
1135 f1c9dbaa gaqhf
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1136 335b7a24 gaqhf
1137 f1c9dbaa gaqhf
            if (_lMConnector != null)
1138
            {
1139 ac82b020 gaqhf
                bool bStart = false;
1140
                bool bEnd = false;
1141
1142 0860c756 gaqhf
                NeedReModeling(lines[0], _LMSymbol1, ref bStart);
1143
                NeedReModeling(lines[lines.Count - 1], _LMSymbol2, ref bEnd);
1144 ac82b020 gaqhf
1145
                if (bStart || bEnd)
1146
                    ReModelingLine(lines, _lMConnector, _LMSymbol1, _LMSymbol2, bStart, bEnd);
1147 1ab9a205 gaqhf
                else
1148
                {
1149
                    foreach (var line in lines)
1150
                        line.SPPID.ModelItemId = _lMConnector.ModelItemID;
1151
                    _lMConnector.Commit();
1152
                }
1153 ac82b020 gaqhf
1154 335b7a24 gaqhf
                if (startBranchLine != null || endBranchLine != null)
1155 1ab9a205 gaqhf
                    BranchLines.Add(new Tuple<string, Line, Line>(lines[0].SPPID.ModelItemId, startBranchLine, endBranchLine));
1156 1b261371 gaqhf
            }
1157 5e6ecf05 gaqhf
1158 f1c9dbaa gaqhf
            if (_LMSymbol1 != null)
1159
                ReleaseCOMObjects(_LMSymbol1);
1160
            if (_LMSymbol2 != null)
1161
                ReleaseCOMObjects(_LMSymbol2);
1162 5e6ecf05 gaqhf
            if (targetConnector1 != null)
1163
                ReleaseCOMObjects(targetConnector1);
1164
            if (targetConnector2 != null)
1165
                ReleaseCOMObjects(targetConnector2);
1166
            foreach (var item in connectorVertices1)
1167
                ReleaseCOMObjects(item.Key);
1168
            foreach (var item in connectorVertices2)
1169
                ReleaseCOMObjects(item.Key);
1170 1b261371 gaqhf
1171 f1c9dbaa gaqhf
            ReleaseCOMObjects(_lMConnector);
1172 1b261371 gaqhf
            ReleaseCOMObjects(placeRunInputs);
1173
            ReleaseCOMObjects(_LMAItem);
1174
        }
1175
1176 0860c756 gaqhf
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
1177 ac82b020 gaqhf
        {
1178 0860c756 gaqhf
            if (symbol != null)
1179 ac82b020 gaqhf
            {
1180 fb386b8c gaqhf
                string repID = symbol.AsLMRepresentation().Id;
1181
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
1182
                string lineUID = line.UID;
1183 ac82b020 gaqhf
1184 fb386b8c gaqhf
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
1185
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
1186
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
1187 71ba1ca3 gaqhf
1188 fb386b8c gaqhf
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
1189
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
1190
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
1191 ac82b020 gaqhf
1192 fb386b8c gaqhf
                if (startSpecBreak != null || startEndBreak != null)
1193
                    result = true;
1194 ac82b020 gaqhf
            }
1195
        }
1196 0860c756 gaqhf
        
1197 74752074 gaqhf
        /// <summary>
1198 1ab9a205 gaqhf
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
1199
        /// </summary>
1200
        /// <param name="lines"></param>
1201
        /// <param name="prevLMConnector"></param>
1202
        /// <param name="startSymbol"></param>
1203
        /// <param name="endSymbol"></param>
1204 ac82b020 gaqhf
        private void ReModelingLine(List<Line> lines, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
1205 1ab9a205 gaqhf
        {
1206
            string symbolPath = string.Empty;
1207
            #region get symbol path
1208
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
1209
            foreach (LMRepresentation rep in modelItem.Representations)
1210
            {
1211
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1212
                {
1213
                    symbolPath = rep.get_FileName();
1214
                    break;
1215
                }
1216
            }
1217
            #endregion
1218
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1219
            LMConnector newConnector = null;
1220
            dynamic OID = prevLMConnector.get_GraphicOID();
1221
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1222
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1223
            int verticesCount = lineStringGeometry.VertexCount;
1224
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1225
            
1226
            List<double[]> vertices = new List<double[]>();
1227
            for (int i = 1; i <= verticesCount; i++)
1228
            {
1229
                double x = 0;
1230
                double y = 0;
1231
                lineStringGeometry.GetVertex(i, ref x, ref y);
1232
                vertices.Add(new double[] { x, y });
1233
            }
1234
1235
            for (int i = 0; i < vertices.Count; i++)
1236
            {
1237
                double[] points = vertices[i];
1238
                // 시작 심볼이 있고 첫번째 좌표일 때
1239
                if (startSymbol != null && i == 0)
1240
                {
1241 ac82b020 gaqhf
                    if (bStart)
1242
                    {
1243
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
1244
                        if (slopeType == SlopeType.HORIZONTAL)
1245
                            placeRunInputs.AddPoint(points[0], -0.1);
1246
                        else if (slopeType == SlopeType.VERTICAL)
1247
                            placeRunInputs.AddPoint(-0.1, points[1]);
1248
                        else
1249
                            placeRunInputs.AddPoint(points[0], -0.1);
1250 1ab9a205 gaqhf
1251 ac82b020 gaqhf
                        placeRunInputs.AddPoint(points[0], points[1]);
1252
                    }
1253
                    else
1254
                    {
1255
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1]);
1256
                    }
1257 1ab9a205 gaqhf
                }
1258
                // 마지막 심볼이 있고 마지막 좌표일 때
1259
                else if (endSymbol != null && i == vertices.Count - 1)
1260
                {
1261 ac82b020 gaqhf
                    if (bEnd)
1262
                    {
1263
                        placeRunInputs.AddPoint(points[0], points[1]);
1264 1ab9a205 gaqhf
1265 ac82b020 gaqhf
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
1266
                        if (slopeType == SlopeType.HORIZONTAL)
1267
                            placeRunInputs.AddPoint(points[0], -0.1);
1268
                        else if (slopeType == SlopeType.VERTICAL)
1269
                            placeRunInputs.AddPoint(-0.1, points[1]);
1270
                        else
1271
                            placeRunInputs.AddPoint(points[0], -0.1);
1272
                    }
1273 1ab9a205 gaqhf
                    else
1274 ac82b020 gaqhf
                    {
1275
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1]);
1276
                    }
1277 1ab9a205 gaqhf
                }
1278
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
1279
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
1280
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1281
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
1282
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
1283
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1284
                else
1285
                    placeRunInputs.AddPoint(points[0], points[1]);
1286
            }
1287
1288
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
1289
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1290
1291
            ReleaseCOMObjects(placeRunInputs);
1292
            ReleaseCOMObjects(_LMAItem);
1293
            ReleaseCOMObjects(modelItem);
1294
1295
            if (newConnector != null)
1296
            {
1297 ac82b020 gaqhf
                if (startSymbol != null && bStart)
1298 1ab9a205 gaqhf
                {
1299
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1300
                    placeRunInputs = new PlaceRunInputs();
1301
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
1302
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
1303
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1304
                    if (_LMConnector != null)
1305
                    {
1306
                        RemoveConnectorForReModelingLine(newConnector);
1307 87f02fc0 gaqhf
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
1308 1ab9a205 gaqhf
                        ReleaseCOMObjects(_LMConnector);
1309
                    }
1310
                    ReleaseCOMObjects(placeRunInputs);
1311
                    ReleaseCOMObjects(_LMAItem);
1312
                }
1313
1314 ac82b020 gaqhf
                if (endSymbol != null && bEnd)
1315 1ab9a205 gaqhf
                {
1316
                    if (startSymbol != null)
1317
                    {
1318
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
1319
                        newConnector = dicVertices.First().Key;
1320
                    }
1321
1322
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1323
                    placeRunInputs = new PlaceRunInputs();
1324
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
1325
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
1326
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1327
                    if (_LMConnector != null)
1328
                    {
1329
                        RemoveConnectorForReModelingLine(newConnector);
1330 87f02fc0 gaqhf
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
1331 1ab9a205 gaqhf
                        ReleaseCOMObjects(_LMConnector);
1332
                    }
1333
                    ReleaseCOMObjects(placeRunInputs);
1334
                    ReleaseCOMObjects(_LMAItem);
1335
                }
1336
1337
                foreach (var line in lines)
1338
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
1339
                ReleaseCOMObjects(newConnector);
1340
            }
1341
1342
            ReleaseCOMObjects(modelItem);
1343
        }
1344
1345
        /// <summary>
1346
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
1347
        /// </summary>
1348
        /// <param name="connector"></param>
1349
        private void RemoveConnectorForReModelingLine(LMConnector connector)
1350
        {
1351
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
1352
            foreach (var item in dicVertices)
1353
            {
1354
                bool result = false;
1355
                foreach (var point in item.Value)
1356
                {
1357
                    if (point[0] < 0 || point[1] < 0)
1358
                    {
1359
                        result = true;
1360
                        _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
1361
                        break;
1362
                    }
1363
                }
1364
1365
                if (result)
1366
                    break;
1367
            }
1368
            foreach (var item in dicVertices)
1369
                ReleaseCOMObjects(item.Key);
1370
        }
1371
1372
        /// <summary>
1373 74752074 gaqhf
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
1374
        /// </summary>
1375
        /// <param name="symbol"></param>
1376
        /// <param name="line"></param>
1377
        /// <returns></returns>
1378 f2baa6a3 gaqhf
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
1379
        {
1380
            LMSymbol _LMSymbol = null;
1381
            foreach (var connector in symbol.CONNECTORS)
1382
            {
1383
                if (connector.CONNECTEDITEM == line.UID)
1384
                {
1385
                    if (connector.Index == 0)
1386
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1387
                    else
1388
                    {
1389
                        ChildSymbol child = null;
1390
                        foreach (var childSymbol in symbol.ChildSymbols)
1391
                        {
1392
                            if (childSymbol.Connectors.Contains(connector))
1393
                                child = childSymbol;
1394
                            else
1395
                                child = GetChildSymbolByConnector(childSymbol, connector);
1396
1397
                            if (child != null)
1398
                                break;
1399
                        }
1400
1401
                        if (child != null)
1402
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
1403
                    }
1404
1405
                    break;  
1406
                }
1407
            }
1408
1409
            return _LMSymbol;
1410
        }
1411
1412 74752074 gaqhf
        /// <summary>
1413
        /// Connector를 가지고 있는 ChildSymbol Object 반환
1414
        /// </summary>
1415
        /// <param name="item"></param>
1416
        /// <param name="connector"></param>
1417
        /// <returns></returns>
1418 f2baa6a3 gaqhf
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
1419
        {
1420
            foreach (var childSymbol in item.ChildSymbols)
1421
            {
1422
                if (childSymbol.Connectors.Contains(connector))
1423
                    return childSymbol;
1424
                else
1425
                    return GetChildSymbolByConnector(childSymbol, connector);
1426
            }
1427
1428
            return null;
1429
        }
1430
1431 74752074 gaqhf
        /// <summary>
1432
        /// Branch 라인을 다시 모델링하는 진입 메서드
1433
        /// </summary>
1434
        /// <param name="branch"></param>
1435 335b7a24 gaqhf
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
1436
        {
1437
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
1438 b66a2996 gaqhf
1439 335b7a24 gaqhf
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
1440 8b085570 gaqhf
            LMConnector _StartConnector = null;
1441
            LMConnector _EndConnector = null;
1442 335b7a24 gaqhf
            double lengthStart = double.MaxValue;
1443
            double lengthEnd = double.MaxValue;
1444
            List<double[]> startPoints = new List<double[]>();
1445
            List<double[]> endPoints = new List<double[]>();
1446
1447
            foreach (var item in connectorVertices)
1448
            {
1449
                foreach (var point in item.Value)
1450
                {
1451
                    // Start Point가 Branch
1452
                    if (branch.Item2 != null)
1453
                    {
1454
                        Line targetLine = branch.Item2;
1455
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
1456
                        if (lengthStart > distance)
1457
                        {
1458 8b085570 gaqhf
                            _StartConnector = item.Key;
1459 335b7a24 gaqhf
                            lengthStart = distance;
1460
                            startPoints = item.Value;
1461
                        }
1462
                    }
1463
                    // End Point가 Branch
1464
                    if (branch.Item3 != null)
1465
                    {
1466
                        Line targetLine = branch.Item3;
1467
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
1468
                        if (lengthEnd > distance)
1469
                        {
1470 8b085570 gaqhf
                            _EndConnector = item.Key;
1471 335b7a24 gaqhf
                            lengthEnd = distance;
1472
                            endPoints = item.Value;
1473
                        }
1474
                    }
1475
                }
1476
            }
1477
            #region Branch가 양쪽 전부일 때
1478 8b085570 gaqhf
            if (_StartConnector != null && _StartConnector == _EndConnector)
1479 335b7a24 gaqhf
            {
1480 8b085570 gaqhf
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
1481 335b7a24 gaqhf
1482
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
1483
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1484
1485
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
1486 8b085570 gaqhf
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
1487 335b7a24 gaqhf
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
1488 8b085570 gaqhf
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
1489 335b7a24 gaqhf
                   startPoints[startPoints.Count - 1][0],
1490
                   startPoints[startPoints.Count - 1][1],
1491
                   startPoints[startPoints.Count - 2][0],
1492
                   startPoints[startPoints.Count - 2][1]);
1493
1494
                for (int i = 0; i < startPoints.Count; i++)
1495
                {
1496
                    double[] point = startPoints[i];
1497
                    if (i == 0)
1498 8b085570 gaqhf
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
1499 335b7a24 gaqhf
                    else if (i == startPoints.Count - 1)
1500 8b085570 gaqhf
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
1501 335b7a24 gaqhf
                    else
1502
                        placeRunInputs.AddPoint(point[0], point[1]);
1503
                }
1504
1505
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1506
                if (_LMConnector != null)
1507
                {
1508
                    _LMConnector.Commit();
1509
                    foreach (var item in lines)
1510
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
1511
                }
1512
1513
                foreach (var item in startConnectorVertices)
1514
                    ReleaseCOMObjects(item.Key);
1515
                foreach (var item in endConnectorVertices)
1516
                    ReleaseCOMObjects(item.Key);
1517
                ReleaseCOMObjects(placeRunInputs);
1518
                ReleaseCOMObjects(_LMAItem);
1519
                ReleaseCOMObjects(_LMConnector);
1520
            }
1521
            #endregion
1522 8b085570 gaqhf
            #region 양쪽이 다른 Branch 
1523 335b7a24 gaqhf
            else
1524
            {
1525
                // Branch 시작 Connector
1526 8b085570 gaqhf
                if (_StartConnector != null)
1527 6b298450 gaqhf
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
1528
                // Branch 끝 Connector
1529 0860c756 gaqhf
                else if (_EndConnector != null)
1530 6b298450 gaqhf
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
1531
            }
1532
            #endregion
1533 335b7a24 gaqhf
1534 6b298450 gaqhf
            if (_StartConnector != null)
1535
                ReleaseCOMObjects(_StartConnector);
1536
            if (_EndConnector != null)
1537
                ReleaseCOMObjects(_EndConnector);
1538
            foreach (var item in connectorVertices)
1539
                ReleaseCOMObjects(item.Key);
1540
        }
1541 335b7a24 gaqhf
1542 74752074 gaqhf
        /// <summary>
1543
        /// Branch 라인을 다시 실제로 모델링하는 메서드
1544
        /// </summary>
1545
        /// <param name="branch"></param>
1546
        /// <param name="_Connector"></param>
1547
        /// <param name="points"></param>
1548
        /// <param name="IsStart"></param>
1549 6b298450 gaqhf
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
1550
        {
1551
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
1552
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
1553
            LMConnector _SameRunTargetConnector = null;
1554 0860c756 gaqhf
            LMSymbol _SameRunTargetLMSymbol = null;
1555
            Symbol _SameRunTargetSymbol = null;
1556 6b298450 gaqhf
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
1557
            LMConnector _BranchTargetConnector = null;
1558
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1559 335b7a24 gaqhf
1560 6b298450 gaqhf
            // 같은 Line Run의 Connector 찾기
1561
            foreach (var item in connectorVertices)
1562
            {
1563
                if (item.Key == _Connector)
1564
                    continue;
1565 335b7a24 gaqhf
1566 6b298450 gaqhf
                if (IsStart &&
1567
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
1568
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
1569
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
1570
                {
1571
                    _SameRunTargetConnector = item.Key;
1572
                    break;
1573
                }
1574
                else if (!IsStart &&
1575
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
1576
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
1577
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
1578
                {
1579
                    _SameRunTargetConnector = item.Key;
1580
                    break;
1581
                }
1582
            }
1583
1584
            // Branch 반대편이 Symbol
1585
            if (_SameRunTargetConnector == null)
1586
            {
1587
                foreach (var line in lines)
1588
                {
1589
                    foreach (var connector in line.CONNECTORS)
1590 335b7a24 gaqhf
                    {
1591 6b298450 gaqhf
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
1592
                        if (symbol != null)
1593 335b7a24 gaqhf
                        {
1594 0860c756 gaqhf
                            _SameRunTargetSymbol = symbol;
1595
                            _SameRunTargetLMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1596 6b298450 gaqhf
                            break;
1597 335b7a24 gaqhf
                        }
1598
                    }
1599 6b298450 gaqhf
                }
1600
            }
1601 335b7a24 gaqhf
1602 6b298450 gaqhf
            // 기존 Connector 제거
1603
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
1604
            
1605
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
1606
            if (IsStart)
1607
            {
1608
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
1609
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
1610
            }
1611
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
1612
            else
1613
            {
1614
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
1615
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
1616
                    points[points.Count - 1][0],
1617
                    points[points.Count - 1][1],
1618
                    points[points.Count - 2][0],
1619
                    points[points.Count - 2][1]);
1620
            }
1621 335b7a24 gaqhf
1622 0860c756 gaqhf
1623
            bool bNeedRemodeling = false;
1624
            if (_SameRunTargetLMSymbol != null)
1625
            {
1626
                Line line = lines.Find(x => x.CONNECTORS.Find(y => y.CONNECTEDITEM == _SameRunTargetSymbol.UID) != null);
1627
                NeedReModeling(line, _SameRunTargetLMSymbol, ref bNeedRemodeling);
1628
            }
1629
1630
1631
1632 6b298450 gaqhf
            for (int i = 0; i < points.Count; i++)
1633
            {
1634
                double[] point = points[i];
1635
                if (i == 0)
1636 335b7a24 gaqhf
                {
1637 6b298450 gaqhf
                    if (IsStart)
1638 335b7a24 gaqhf
                    {
1639 b2d1c1aa gaqhf
                        if (_BranchTargetConnector != null)
1640
                        {
1641
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
1642
                        }
1643
                        else
1644
                        {
1645
                            placeRunInputs.AddPoint(point[0], point[1]);
1646
                        }
1647 335b7a24 gaqhf
                    }
1648 6b298450 gaqhf
                    else
1649 335b7a24 gaqhf
                    {
1650 6b298450 gaqhf
                        if (_SameRunTargetConnector != null)
1651
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
1652 0860c756 gaqhf
                        else if (_SameRunTargetLMSymbol != null && bNeedRemodeling)
1653
                        {
1654
                            SlopeType slopeType = SPPIDUtil.CalcSlope(point[0], point[1], points[i + 1][0], points[i + 1][1]);
1655
                            if (slopeType == SlopeType.HORIZONTAL)
1656
                                placeRunInputs.AddPoint(point[0], -0.1);
1657
                            else if (slopeType == SlopeType.VERTICAL)
1658
                                placeRunInputs.AddPoint(-0.1, point[1]);
1659
                            else
1660
                                placeRunInputs.AddPoint(point[0], -0.1);
1661
1662
                            placeRunInputs.AddPoint(point[0], point[1]);
1663
                        }
1664
                        else if (_SameRunTargetLMSymbol != null)
1665
                            placeRunInputs.AddSymbolTarget(_SameRunTargetLMSymbol, point[0], point[1]);
1666 6b298450 gaqhf
                        else
1667
                            placeRunInputs.AddPoint(point[0], point[1]);
1668 335b7a24 gaqhf
                    }
1669 6b298450 gaqhf
                }
1670
                else if (i == points.Count - 1)
1671
                {
1672
                    if (IsStart)
1673 335b7a24 gaqhf
                    {
1674 6b298450 gaqhf
                        if (_SameRunTargetConnector != null)
1675
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
1676 0860c756 gaqhf
                        else if (_SameRunTargetLMSymbol != null && bNeedRemodeling)
1677
                        {
1678
                            placeRunInputs.AddPoint(point[0], point[1]);
1679
1680
                            SlopeType slopeType = SPPIDUtil.CalcSlope(point[0], point[1], points[i - 1][0], points[i - 1][1]);
1681
                            if (slopeType == SlopeType.HORIZONTAL)
1682
                                placeRunInputs.AddPoint(point[0], -0.1);
1683
                            else if (slopeType == SlopeType.VERTICAL)
1684
                                placeRunInputs.AddPoint(-0.1, point[1]);
1685
                            else
1686
                                placeRunInputs.AddPoint(point[0], -0.1);
1687
                        }
1688
                        else if (_SameRunTargetLMSymbol != null)
1689
                            placeRunInputs.AddSymbolTarget(_SameRunTargetLMSymbol, point[0], point[1]);
1690 335b7a24 gaqhf
                        else
1691
                            placeRunInputs.AddPoint(point[0], point[1]);
1692
                    }
1693 6b298450 gaqhf
                    else
1694 335b7a24 gaqhf
                    {
1695 f9125a54 gaqhf
                        if (_BranchTargetConnector != null)
1696
                        {
1697
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
1698
                        }
1699 b2d1c1aa gaqhf
                        else
1700
                        {
1701
                            placeRunInputs.AddPoint(point[0], point[1]);
1702
                        }
1703 335b7a24 gaqhf
                    }
1704 6b298450 gaqhf
                }
1705
                else
1706
                    placeRunInputs.AddPoint(point[0], point[1]);
1707
            }
1708
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
1709
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1710 335b7a24 gaqhf
1711 6b298450 gaqhf
            if (_LMConnector != null)
1712
            {
1713
                if (_SameRunTargetConnector != null)
1714
                {
1715
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
1716
                }
1717
                else
1718
                {
1719 0860c756 gaqhf
                    if (_SameRunTargetLMSymbol != null && bNeedRemodeling)
1720
                    {
1721
                        string symbolPath = string.Empty;
1722
                        #region get symbol path
1723
                        LMModelItem modelItem = dataSource.GetModelItem(_LMConnector.ModelItemID);
1724
                        foreach (LMRepresentation rep in modelItem.Representations)
1725
                        {
1726
                            if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1727
                            {
1728
                                symbolPath = rep.get_FileName();
1729
                                break;
1730
                            }
1731
                        }
1732
                        ReleaseCOMObjects(modelItem);
1733
                        #endregion
1734
1735
                        double[] point = null;
1736
                        if (IsStart)
1737
                            point = points[points.Count - 1];
1738
                        else
1739
                            point = points[0];
1740
1741
                        _LMAItem = _placement.PIDCreateItem(symbolPath);
1742
                        placeRunInputs = new PlaceRunInputs();
1743
                        placeRunInputs.AddSymbolTarget(_SameRunTargetLMSymbol, point[0], point[1]);
1744
                        placeRunInputs.AddConnectorTarget(_LMConnector, point[0], point[1]);
1745
                        LMConnector _ZeroLengthLMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1746
                        if (_ZeroLengthLMConnector != null)
1747
                        {
1748
                            RemoveConnectorForReModelingLine(_LMConnector);
1749
                            ZeroLengthModelItemID.Add(_ZeroLengthLMConnector.ModelItemID);
1750
                            ReleaseCOMObjects(_ZeroLengthLMConnector);
1751
                        }
1752
                        ReleaseCOMObjects(placeRunInputs);
1753
                        ReleaseCOMObjects(_LMAItem);
1754
                    }
1755
1756 6b298450 gaqhf
                    foreach (var item in lines)
1757
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
1758 335b7a24 gaqhf
                }
1759
1760 6b298450 gaqhf
                _LMConnector.Commit();
1761
                ReleaseCOMObjects(_LMConnector);
1762 335b7a24 gaqhf
            }
1763
1764 6b298450 gaqhf
            ReleaseCOMObjects(placeRunInputs);
1765
            ReleaseCOMObjects(_LMAItem);
1766
            if (_BranchTargetConnector != null)
1767
                ReleaseCOMObjects(_BranchTargetConnector);
1768
            if (_SameRunTargetConnector != null)
1769
                ReleaseCOMObjects(_SameRunTargetConnector);
1770 0860c756 gaqhf
            if (_SameRunTargetLMSymbol != null)
1771
                ReleaseCOMObjects(_SameRunTargetLMSymbol);
1772 335b7a24 gaqhf
            foreach (var item in connectorVertices)
1773
                ReleaseCOMObjects(item.Key);
1774 6b298450 gaqhf
            foreach (var item in branchConnectorVertices)
1775
                ReleaseCOMObjects(item.Key);
1776 335b7a24 gaqhf
        }
1777
1778 74752074 gaqhf
        /// <summary>
1779
        /// EndBreak 모델링 메서드
1780
        /// </summary>
1781
        /// <param name="endBreak"></param>
1782 3165c259 gaqhf
        private void EndBreakModeling(EndBreak endBreak)
1783 335b7a24 gaqhf
        {
1784 10c7195c gaqhf
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
1785 1ab9a205 gaqhf
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
1786
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
1787 3165c259 gaqhf
1788 1ab9a205 gaqhf
            if (targetLMConnector != null)
1789 10c7195c gaqhf
            {
1790 1ab9a205 gaqhf
                Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1791
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1792 2a4872ec gaqhf
            }
1793 1ab9a205 gaqhf
            
1794 f31645b6 gaqhf
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1795 2a4872ec gaqhf
        }
1796 b9e9f4c8 gaqhf
1797 02480ac1 gaqhf
        private LMConnector ReModelingLMConnector(LMConnector connector)
1798
        {
1799 de97eaaa gaqhf
            string symbolPath = string.Empty;
1800
            #region get symbol path
1801
            LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
1802
            foreach (LMRepresentation rep in modelItem.Representations)
1803
            {
1804
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1805
                {
1806
                    symbolPath = rep.get_FileName();
1807
                    break;
1808
                }
1809
            }
1810
            #endregion
1811
1812 02480ac1 gaqhf
            LMConnector newConnector = null;
1813
            dynamic OID = connector.get_GraphicOID();
1814
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1815
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1816
            int verticesCount = lineStringGeometry.VertexCount;
1817
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1818 de97eaaa gaqhf
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1819 02480ac1 gaqhf
1820
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
1821
            {
1822
                double[] vertices = null;
1823
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1824
                double x = 0;
1825
                double y = 0;
1826
                lineStringGeometry.GetVertex(1, ref x, ref y);
1827
1828
                placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
1829
                placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
1830
1831
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
1832
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1833
            }
1834
            else
1835
            {
1836
                List<double[]> vertices = new List<double[]>();
1837
                for (int i = 1; i <= verticesCount; i++)
1838
                {
1839
                    double x = 0;
1840
                    double y = 0;
1841
                    lineStringGeometry.GetVertex(i, ref x, ref y);
1842
                    vertices.Add(new double[] { x, y });
1843
                }
1844
1845
                for (int i = 0; i < vertices.Count; i++)
1846
                {
1847
                    double[] points = vertices[i];
1848
                    if (i == 0)
1849
                    {
1850
                        if (connector.ConnectItem1SymbolObject != null)
1851
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, points[0], points[1]);
1852
                        else
1853
                            placeRunInputs.AddPoint(points[0], points[1]);
1854
                    }
1855
                    else if (i == vertices.Count - 1)
1856
                    {
1857
                        if (connector.ConnectItem2SymbolObject != null)
1858
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, points[0], points[1]);
1859
                        else
1860
                            placeRunInputs.AddPoint(points[0], points[1]);
1861
                    }
1862
                    else
1863
                        placeRunInputs.AddPoint(points[0], points[1]);
1864
                }
1865
1866
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, connector.ModelItemID);
1867
1868
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
1869
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1870
1871
                foreach (var line in lines)
1872
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
1873
            }
1874
1875
1876
            return newConnector;
1877
        }
1878
1879 74752074 gaqhf
        /// <summary>
1880 53c81765 gaqhf
        /// SpecBreak Modeling 메서드
1881
        /// </summary>
1882
        /// <param name="specBreak"></param>
1883
        private void SpecBreakModeling(SpecBreak specBreak)
1884
        {
1885
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
1886
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
1887
1888
            if (upStreamObj != null &&
1889
                downStreamObj != null)
1890
            {
1891 1ab9a205 gaqhf
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
1892
1893
                if (targetLMConnector != null)
1894 53c81765 gaqhf
                {
1895 16584d30 gaqhf
                    foreach (var attribute in specBreak.ATTRIBUTES)
1896 53c81765 gaqhf
                    {
1897 16584d30 gaqhf
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
1898
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
1899
                        {
1900
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
1901
                            Array array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
1902
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, Rotation: specBreak.ANGLE, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
1903 53c81765 gaqhf
1904 16584d30 gaqhf
                            if (_LmLabelPersist != null)
1905
                            {
1906
                                ReleaseCOMObjects(_LmLabelPersist);
1907
                            }
1908
                        }
1909
                    }
1910 1ab9a205 gaqhf
                    ReleaseCOMObjects(targetLMConnector);
1911
                }
1912
            }
1913
        }
1914 53c81765 gaqhf
1915 1ab9a205 gaqhf
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
1916
        {
1917
            LMConnector targetConnector = null;
1918
            Symbol targetSymbol = targetObj as Symbol;
1919
            Symbol connectedSymbol = connectedObj as Symbol;
1920
            Line targetLine = targetObj as Line;
1921
            Line connectedLine = connectedObj as Line;
1922
            if (targetSymbol != null && connectedSymbol != null)
1923
            {
1924
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
1925
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
1926
1927
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
1928
                {
1929
                    if (connector.get_ItemStatus() != "Active")
1930
                        continue;
1931
1932
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
1933
                    {
1934
                        targetConnector = connector;
1935
                        break;
1936
                    }
1937
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
1938
                    {
1939
                        targetConnector = connector;
1940
                        break;
1941 53c81765 gaqhf
                    }
1942
                }
1943 1ab9a205 gaqhf
1944
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
1945 53c81765 gaqhf
                {
1946 1ab9a205 gaqhf
                    if (connector.get_ItemStatus() != "Active")
1947
                        continue;
1948 53c81765 gaqhf
1949 1ab9a205 gaqhf
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
1950
                    {
1951
                        targetConnector = connector;
1952
                        break;
1953
                    }
1954
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
1955
                    {
1956
                        targetConnector = connector;
1957
                        break;
1958 53c81765 gaqhf
                    }
1959
                }
1960 1ab9a205 gaqhf
1961
                ReleaseCOMObjects(targetLMSymbol);
1962
                ReleaseCOMObjects(connectedLMSymbol);
1963
            }
1964
            else if (targetLine != null && connectedLine != null)
1965
            {
1966
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
1967
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
1968
1969
                if (targetModelItem != null && connectedModelItem != null)
1970 53c81765 gaqhf
                {
1971 1ab9a205 gaqhf
                    foreach (LMRepresentation rep in targetModelItem.Representations)
1972 53c81765 gaqhf
                    {
1973 1ab9a205 gaqhf
                        if (targetConnector != null)
1974
                            break;
1975 53c81765 gaqhf
1976 1ab9a205 gaqhf
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1977 53c81765 gaqhf
                        {
1978 1ab9a205 gaqhf
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1979
1980
                            if (IsConnected(_LMConnector, connectedModelItem))
1981
                                targetConnector = _LMConnector;
1982
                            else
1983
                                ReleaseCOMObjects(_LMConnector);
1984 53c81765 gaqhf
                        }
1985
                    }
1986 1ab9a205 gaqhf
1987
                    ReleaseCOMObjects(targetModelItem);
1988 53c81765 gaqhf
                }
1989 1ab9a205 gaqhf
            }
1990
            else
1991
            {
1992
                LMSymbol connectedLMSymbol = connectedSymbol != null ? 
1993
                    dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId) : dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
1994
                LMModelItem targetModelItem = targetLine != null ?
1995
                    dataSource.GetModelItem(targetLine.SPPID.ModelItemId) : dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
1996
                if (connectedLMSymbol != null && targetModelItem != null)
1997 53c81765 gaqhf
                {
1998 1ab9a205 gaqhf
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
1999
                    {
2000 ac82b020 gaqhf
                        if (connector.get_ItemStatus() != "Active")
2001
                            continue;
2002
2003 1ab9a205 gaqhf
                        if (IsConnected(connector, targetModelItem))
2004
                        {
2005
                            targetConnector = connector;
2006
                            break;
2007
                        }
2008
                    }
2009 53c81765 gaqhf
2010 1ab9a205 gaqhf
                    if (targetConnector == null)
2011 53c81765 gaqhf
                    {
2012 1ab9a205 gaqhf
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
2013 53c81765 gaqhf
                        {
2014 ac82b020 gaqhf
                            if (connector.get_ItemStatus() != "Active")
2015
                                continue;
2016
2017 1ab9a205 gaqhf
                            if (IsConnected(connector, targetModelItem))
2018 53c81765 gaqhf
                            {
2019 1ab9a205 gaqhf
                                targetConnector = connector;
2020
                                break;
2021 53c81765 gaqhf
                            }
2022
                        }
2023
                    }
2024
                }
2025
2026 1ab9a205 gaqhf
            }
2027 02480ac1 gaqhf
2028 1ab9a205 gaqhf
            return targetConnector;
2029
        }
2030 53c81765 gaqhf
2031 1ab9a205 gaqhf
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
2032
        {
2033
            bool result = false;
2034
2035
            foreach (LMRepresentation rep in modelItem.Representations)
2036
            {
2037
                if (result)
2038
                    break;
2039
2040
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2041
                {
2042
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2043
2044
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
2045
                        connector.ConnectItem1SymbolObject != null &&
2046
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2047 53c81765 gaqhf
                    {
2048 1ab9a205 gaqhf
                        result = true;
2049
                        ReleaseCOMObjects(_LMConnector);
2050
                        break;
2051
                    }
2052
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
2053
                        connector.ConnectItem2SymbolObject != null &&
2054
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2055
                    {
2056
                        result = true;
2057
                        ReleaseCOMObjects(_LMConnector);
2058
                        break;
2059
                    }
2060
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2061
                        connector.ConnectItem1SymbolObject != null &&
2062
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2063
                    {
2064
                        result = true;
2065
                        ReleaseCOMObjects(_LMConnector);
2066
                        break;
2067
                    }
2068
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2069
                        connector.ConnectItem2SymbolObject != null &&
2070
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2071
                    {
2072
                        result = true;
2073
                        ReleaseCOMObjects(_LMConnector);
2074
                        break;
2075 53c81765 gaqhf
                    }
2076
2077 1ab9a205 gaqhf
                    ReleaseCOMObjects(_LMConnector);
2078
                }
2079 53c81765 gaqhf
            }
2080 1ab9a205 gaqhf
2081
2082
            return result;
2083 53c81765 gaqhf
        }
2084
2085
        /// <summary>
2086 74752074 gaqhf
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
2087
        /// </summary>
2088
        /// <param name="fromModelItemId"></param>
2089
        /// <param name="toModelItemId"></param>
2090 335b7a24 gaqhf
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
2091
        {
2092 310aeb31 gaqhf
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
2093
            _LMAItem item1 = modelItem1.AsLMAItem();
2094
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
2095
            _LMAItem item2 = modelItem2.AsLMAItem();
2096
            
2097 335b7a24 gaqhf
            // item2가 item1으로 조인
2098
            try
2099
            {
2100
                _placement.PIDJoinRuns(ref item1, ref item2);
2101 65a1ed4b gaqhf
                item1.Commit();
2102
                item2.Commit();
2103 3939eebf gaqhf
2104
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
2105
                foreach (var line in lines)
2106
                    line.SPPID.ModelItemId = toModelItemId;
2107 335b7a24 gaqhf
            }
2108
            catch (Exception ex)
2109
            {
2110
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2111
            }
2112
            finally
2113
            {
2114 310aeb31 gaqhf
                ReleaseCOMObjects(modelItem1);
2115
                ReleaseCOMObjects(item1);
2116
                ReleaseCOMObjects(modelItem2);
2117
                ReleaseCOMObjects(item2);
2118 335b7a24 gaqhf
            }
2119
        }
2120
2121 74752074 gaqhf
        /// <summary>
2122
        /// PipeRun을 자동으로 Join하는 메서드
2123
        /// </summary>
2124
        /// <param name="modelItemId"></param>
2125 335b7a24 gaqhf
        private void AutoJoinPipeRun(string modelItemId)
2126
        {
2127 310aeb31 gaqhf
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
2128
            _LMAItem item = modelItem.AsLMAItem();
2129 65a1ed4b gaqhf
            try
2130
            {
2131 87f02fc0 gaqhf
                if (modelItem.get_ItemStatus() == "Active")
2132 65a1ed4b gaqhf
                {
2133 87f02fc0 gaqhf
                    string modelitemID = item.Id;
2134
                    _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
2135
                    string afterModelItemID = item.Id;
2136
2137
                    if (modelitemID != afterModelItemID)
2138
                    {
2139
                        List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
2140
                        foreach (var line in lines)
2141
                            line.SPPID.ModelItemId = afterModelItemID;
2142
                    }
2143
                    item.Commit();
2144 65a1ed4b gaqhf
                }
2145
            }
2146
            catch (Exception ex)
2147
            {
2148
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2149
            }
2150
            finally
2151
            {
2152 310aeb31 gaqhf
                ReleaseCOMObjects(modelItem);
2153
                ReleaseCOMObjects(item);
2154 65a1ed4b gaqhf
            }
2155 335b7a24 gaqhf
        }
2156
2157 74752074 gaqhf
        /// <summary>
2158
        /// LineRun에 있는 Line들을 Join하는 진입 메서드
2159
        /// </summary>
2160
        /// <param name="run"></param>
2161 335b7a24 gaqhf
        private void JoinRunLine(LineRun run)
2162
        {
2163
            string modelItemId = string.Empty;
2164
            foreach (var item in run.RUNITEMS)
2165
            {
2166
                if (item.GetType() == typeof(Line))
2167
                {
2168
                    Line line = item as Line;
2169 1ba9c671 gaqhf
                    AutoJoinPipeRun(line.SPPID.ModelItemId);
2170
                    modelItemId = line.SPPID.ModelItemId;
2171 f31645b6 gaqhf
2172
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2173 335b7a24 gaqhf
                }
2174
            }
2175
        }
2176
2177 74752074 gaqhf
        /// <summary>
2178
        /// PipeRun의 좌표를 가져오는 메서드
2179
        /// </summary>
2180
        /// <param name="modelId"></param>
2181
        /// <returns></returns>
2182 5e6ecf05 gaqhf
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
2183
        {
2184
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2185 310aeb31 gaqhf
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2186
2187
            if (modelItem != null)
2188 5e6ecf05 gaqhf
            {
2189 310aeb31 gaqhf
                foreach (LMRepresentation rep in modelItem.Representations)
2190 5e6ecf05 gaqhf
                {
2191
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2192
                    {
2193
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2194
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2195
                        dynamic OID = rep.get_GraphicOID();
2196 335b7a24 gaqhf
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2197 5e6ecf05 gaqhf
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2198
                        int verticesCount = lineStringGeometry.VertexCount;
2199
                        double[] vertices = null;
2200
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2201
                        for (int i = 0; i < verticesCount; i++)
2202
                        {
2203
                            double x = 0;
2204
                            double y = 0;
2205
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2206 335b7a24 gaqhf
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
2207 5e6ecf05 gaqhf
                        }
2208
                    }
2209
                }
2210
2211 310aeb31 gaqhf
                ReleaseCOMObjects(modelItem);
2212 5e6ecf05 gaqhf
            }
2213
2214
            return connectorVertices;
2215
        }
2216
2217 7c7bcd10 gaqhf
        /// <summary>
2218
        /// LMConnector들을 가져온다.
2219
        /// </summary>
2220
        /// <param name="modelId"></param>
2221
        /// <returns></returns>
2222 53c81765 gaqhf
        private List<LMConnector> GetConnectors(string modelId)
2223
        {
2224
            List<LMConnector> connectors = new List<LMConnector>();
2225
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2226
2227
            if (modelItem != null)
2228
            {
2229
                foreach (LMRepresentation rep in modelItem.Representations)
2230
                {
2231
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2232
                    {
2233
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2234
                        connectors.Add(_LMConnector);
2235
                    }
2236
                }
2237
2238
                ReleaseCOMObjects(modelItem);
2239
            }
2240
2241
            return connectors;
2242
        }
2243
2244 74752074 gaqhf
        /// <summary>
2245
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 두점으로 라인의 교차점을 기준으로 구함
2246
        /// </summary>
2247
        /// <param name="connectorVertices"></param>
2248
        /// <param name="connX"></param>
2249
        /// <param name="connY"></param>
2250
        /// <param name="x2"></param>
2251
        /// <param name="y2"></param>
2252
        /// <returns></returns>
2253 56bc67e1 gaqhf
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
2254 5e6ecf05 gaqhf
        {
2255
            double length = double.MaxValue;
2256
            LMConnector targetConnector = null;
2257
            foreach (var item in connectorVertices)
2258
            {
2259
                List<double[]> points = item.Value;
2260
                for (int i = 0; i < points.Count - 1; i++)
2261
                {
2262
                    double[] point1 = points[i];
2263
                    double[] point2 = points[i + 1];
2264
2265 335b7a24 gaqhf
                    double maxLineX = Math.Max(point1[0], point2[0]);
2266
                    double minLineX = Math.Min(point1[0], point2[0]);
2267
                    double maxLineY = Math.Max(point1[1], point2[1]);
2268
                    double minLineY = Math.Min(point1[1], point2[1]);
2269
2270
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
2271
2272 56bc67e1 gaqhf
                    // 두직선의 교차점
2273
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
2274
                    if (crossingPoint != null)
2275 5e6ecf05 gaqhf
                    {
2276 30a9ffce gaqhf
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
2277 335b7a24 gaqhf
                        if (length >= distance)
2278 30a9ffce gaqhf
                        {
2279 335b7a24 gaqhf
                            if (slope == SlopeType.Slope &&
2280
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
2281
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2282
                            {
2283
                                targetConnector = item.Key;
2284
                                length = distance;
2285
                            }
2286
                            else if (slope == SlopeType.HORIZONTAL &&
2287
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
2288
                            {
2289
                                targetConnector = item.Key;
2290
                                length = distance;
2291
                            }
2292
                            else if (slope == SlopeType.VERTICAL &&
2293
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2294
                            {
2295
                                targetConnector = item.Key;
2296
                                length = distance;
2297
                            }
2298 30a9ffce gaqhf
                        }
2299 5e6ecf05 gaqhf
                    }
2300
                }
2301 c3d2e266 gaqhf
2302
2303
            }
2304
2305
            if (targetConnector == null)
2306
            {
2307
                foreach (var item in connectorVertices)
2308
                {
2309
                    List<double[]> points = item.Value;
2310
                    foreach (var point in points)
2311
                    {
2312
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, point[0], point[1]);
2313
                        if (length >= distance)
2314
                        {
2315
                            targetConnector = item.Key;
2316
                            length = distance;
2317
                        }
2318
                    }
2319
                }
2320
2321 5e6ecf05 gaqhf
            }
2322
2323
            return targetConnector;
2324
        }
2325
2326 74752074 gaqhf
        /// <summary>
2327
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 한점으로 제일 가까운 기준으로 구함(단순)
2328
        /// </summary>
2329
        /// <param name="connectorVertices"></param>
2330
        /// <param name="connX"></param>
2331
        /// <param name="connY"></param>
2332
        /// <returns></returns>
2333 ac78b508 gaqhf
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
2334
        {
2335
            double length = double.MaxValue;
2336
            LMConnector targetConnector = null;
2337
            foreach (var item in connectorVertices)
2338
            {
2339
                List<double[]> points = item.Value;
2340
2341
                foreach (double[] point in points)
2342
                {
2343
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2344
                    if (length >= distance)
2345
                    {
2346
                        targetConnector = item.Key;
2347
                        length = distance;
2348
                    }
2349
                }
2350
            }
2351
2352
            return targetConnector;
2353
        }
2354
2355 74752074 gaqhf
        /// <summary>
2356
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
2357
        /// </summary>
2358
        /// <param name="connectorVertices"></param>
2359
        /// <param name="connX"></param>
2360
        /// <param name="connY"></param>
2361
        /// <returns></returns>
2362 68464385 gaqhf
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
2363
        {
2364
            double length = double.MaxValue;
2365
            LMConnector targetConnector = null;
2366
            foreach (var item in connectorVertices)
2367
            {
2368
                List<double[]> points = item.Value;
2369
                for (int i = 0; i < points.Count - 1; i++)
2370
                {
2371
                    double[] point1 = points[i];
2372
                    double[] point2 = points[i + 1];
2373
                    double x1 = Math.Min(point1[0], point2[0]);
2374
                    double y1 = Math.Min(point1[1], point2[1]);
2375
                    double x2 = Math.Max(point1[0], point2[0]);
2376
                    double y2 = Math.Max(point1[1], point2[1]);
2377
2378
                    if ((x1 <= connX && x2 >= connX) ||
2379
                        (y1 <= connY && y2 >= connY))
2380
                    {
2381
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
2382
                        if (length >= distance)
2383
                        {
2384
                            targetConnector = item.Key;
2385
                            length = distance;
2386
                        }
2387
2388
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
2389
                        if (length >= distance)
2390
                        {
2391
                            targetConnector = item.Key;
2392
                            length = distance;
2393
                        }
2394
                    }
2395
                }
2396
            }
2397
2398
            // 못찾았을때.
2399
            length = double.MaxValue;
2400
            if (targetConnector == null)
2401
            {
2402
                foreach (var item in connectorVertices)
2403
                {
2404
                    List<double[]> points = item.Value;
2405
2406
                    foreach (double[] point in points)
2407
                    {
2408
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2409
                        if (length >= distance)
2410
                        {
2411
                            targetConnector = item.Key;
2412
                            length = distance;
2413
                        }
2414
                    }
2415
                }
2416
            }
2417
2418
            return targetConnector;
2419
        }
2420
2421 74752074 gaqhf
        /// <summary>
2422
        /// Line Number Symbol을 실제로 Modeling하는 메서드
2423
        /// </summary>
2424
        /// <param name="lineNumber"></param>
2425 cfda1fed gaqhf
        private void LineNumberModeling(LineNumber lineNumber)
2426
        {
2427 f4880c6a gaqhf
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
2428
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2429 68464385 gaqhf
            LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
2430 f4880c6a gaqhf
            if (connectedLMConnector != null)
2431 10872260 gaqhf
            {
2432 b65a7e32 gaqhf
                double x = 0;
2433
                double y = 0;
2434
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
2435
2436
                Array points = new double[] { 0, x, y };
2437 f4880c6a gaqhf
                LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: false);
2438 cfda1fed gaqhf
2439 f4880c6a gaqhf
                foreach (var item in connectorVertices)
2440
                    ReleaseCOMObjects(item.Key);
2441
                if (_LmLabelPresist != null)
2442
                {
2443 c3d2e266 gaqhf
                    _LmLabelPresist.Commit();
2444 f4880c6a gaqhf
                    lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
2445
                    ReleaseCOMObjects(_LmLabelPresist);
2446 10872260 gaqhf
                }
2447 c3d2e266 gaqhf
                else
2448
                {
2449
2450
                }
2451 10872260 gaqhf
            }
2452 f31645b6 gaqhf
2453
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2454 cfda1fed gaqhf
        }
2455
2456 74752074 gaqhf
        /// <summary>
2457 b2d1c1aa gaqhf
        /// Flow Mark Modeling
2458
        /// </summary>
2459
        /// <param name="line"></param>
2460
        private void FlowMarkModeling(Line line)
2461
        {
2462 a0965e07 gaqhf
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
2463 b2d1c1aa gaqhf
            {
2464 47ad9a46 gaqhf
                SlopeType targetSlopeType = SPPIDUtil.CalcSlope(line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
2465 b2d1c1aa gaqhf
                string mappingPath = _ETCSetting.FlowMarkSymbolPath;
2466 47ad9a46 gaqhf
                double percent = line.FLOWMARK_PERCENT;
2467 b2d1c1aa gaqhf
                double tempX = 0;
2468
                double tempY = 0;
2469
2470 47ad9a46 gaqhf
                double gapX;
2471
                double gapY;
2472
                // ID2 기준의 Gap을 구함
2473
                if (percent == 0)
2474
                {
2475
                    gapX = 0;
2476
                    gapY = 0;
2477
                }
2478
                else
2479
                {
2480
                    gapX = Math.Abs(line.SPPID.START_X - line.SPPID.END_X) / 100 * percent;
2481
                    gapY = Math.Abs(line.SPPID.START_Y - line.SPPID.END_Y) / 100 * percent;
2482
                }
2483 b2d1c1aa gaqhf
2484 47ad9a46 gaqhf
                if (line.SPPID.START_X < line.SPPID.END_X)
2485
                    tempX = line.SPPID.START_X + gapX;
2486
                else
2487
                    tempX = line.SPPID.START_X - gapX;
2488 b2d1c1aa gaqhf
2489 47ad9a46 gaqhf
                if (line.SPPID.START_Y < line.SPPID.END_Y)
2490
                    tempY = line.SPPID.START_Y + gapY;
2491
                else
2492
                    tempY = line.SPPID.START_Y - gapY;
2493 b2d1c1aa gaqhf
2494 47ad9a46 gaqhf
                
2495
               
2496 b2d1c1aa gaqhf
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2497
                LMConnector _TargetItem = null;
2498
                double distance = double.MaxValue;
2499 47ad9a46 gaqhf
                double[] startPoint = null;
2500
                double[] endPoint = null;
2501
                // ID2의 기준 Gap으로 제일 가까운 Line 찾음(Slope도 같은조건)
2502 b2d1c1aa gaqhf
                foreach (var item in connectorVertices)
2503
                {
2504
                    for (int i = 0; i < item.Value.Count - 1; i++)
2505
                    {
2506
                        List<double[]> points = item.Value;
2507
                        double[] point1 = points[i];
2508
                        double[] point2 = points[i + 1];
2509
2510
                        SlopeType slopeType = SPPIDUtil.CalcSlope(point1[0], point1[1], point2[0], point2[1]);
2511 47ad9a46 gaqhf
                        if (slopeType == targetSlopeType)
2512 b2d1c1aa gaqhf
                        {
2513 47ad9a46 gaqhf
                            double result = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], tempX, tempY);
2514
                            if (result < distance)
2515 b2d1c1aa gaqhf
                            {
2516 47ad9a46 gaqhf
                                distance = result;
2517
                                _TargetItem = item.Key;
2518
2519
                                startPoint = point1;
2520
                                endPoint = point2;
2521 b2d1c1aa gaqhf
                            }
2522 47ad9a46 gaqhf
2523
                            result = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], tempX, tempY);
2524
                            if (result < distance)
2525 b2d1c1aa gaqhf
                            {
2526 47ad9a46 gaqhf
                                distance = result;
2527
                                _TargetItem = item.Key;
2528 b2d1c1aa gaqhf
2529 47ad9a46 gaqhf
                                startPoint = point1;
2530
                                endPoint = point2;
2531
                            }
2532 b2d1c1aa gaqhf
                        }
2533
                    }
2534
                }
2535
2536
                if (_TargetItem != null)
2537
                {
2538 47ad9a46 gaqhf
                    double x = 0;
2539
                    double y = 0;
2540
                    double angle = 0;
2541
                    // SPPID 기준의 Gap으로 실 좌표를 구함
2542
                    if (percent == 0)
2543
                    {
2544
                        gapX = 0;
2545
                        gapY = 0;
2546
                    }
2547
                    else
2548
                    {
2549
                        gapX = Math.Abs(startPoint[0] - endPoint[0]) / 100 * percent;
2550
                        gapY = Math.Abs(startPoint[1] - endPoint[1]) / 100 * percent;
2551
                    }
2552 b2d1c1aa gaqhf
2553 47ad9a46 gaqhf
                    if (startPoint[0] < endPoint[0])
2554
                        x = startPoint[0] + gapX;
2555
                    else
2556
                        x = startPoint[0] - gapX;
2557
2558
                    if (startPoint[1] < endPoint[1])
2559
                        y = startPoint[1] + gapY;
2560
                    else
2561
                        y = startPoint[1] - gapY;
2562
                    
2563
                    if (targetSlopeType == SlopeType.HORIZONTAL)
2564
                    {
2565
                        if (startPoint[0] < endPoint[0])
2566
                            angle = 0;
2567
                        else
2568
                            angle = Math.PI;
2569
                    }
2570
                    // 90 270
2571
                    else if (targetSlopeType == SlopeType.VERTICAL)
2572
                    {
2573
                        if (startPoint[1] < endPoint[1])
2574
                            angle = 90 * Math.PI / 180;
2575
                        else
2576
                            angle = 270 * Math.PI / 180;
2577
                    }
2578
2579
                    LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: 0, Rotation: angle, TargetItem: _TargetItem);
2580
                    
2581 b2d1c1aa gaqhf
                    if (_LMSymbol != null)
2582
                    {
2583
                        ReleaseCOMObjects(_LMSymbol);
2584
                    }
2585
                        
2586
                }
2587
2588
                foreach (var item in connectorVertices)
2589
                    ReleaseCOMObjects(item.Key);
2590
            }
2591
        }
2592
2593
        /// <summary>
2594 74752074 gaqhf
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
2595
        /// </summary>
2596
        /// <param name="lineNumber"></param>
2597 a7e9beec gaqhf
        private void InputLineNumberAttribute(LineNumber lineNumber)
2598
        {
2599 8634af60 gaqhf
            foreach (LineRun run in lineNumber.RUNS)
2600 a7e9beec gaqhf
            {
2601 8634af60 gaqhf
                foreach (var item in run.RUNITEMS)
2602 a7e9beec gaqhf
                {
2603 8634af60 gaqhf
                    if (item.GetType() == typeof(Symbol))
2604 a7e9beec gaqhf
                    {
2605 8634af60 gaqhf
                        Symbol symbol = item as Symbol;
2606
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2607
                        LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
2608
2609
                        if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2610
                        {
2611
                            foreach (var attribute in lineNumber.ATTRIBUTES)
2612
                            {
2613
                                LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2614 b2d1c1aa gaqhf
                                if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2615 8634af60 gaqhf
                                {
2616
                                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
2617
                                    if (_LMAAttribute != null)
2618
                                    {
2619
                                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2620
                                            _LMAAttribute.set_Value(attribute.VALUE);
2621
                                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
2622
                                            _LMAAttribute.set_Value(attribute.VALUE);
2623
                                    }
2624
                                }
2625
                            }
2626
                            _LMModelItem.Commit();
2627
                        }
2628
                        if (_LMModelItem != null)
2629
                            ReleaseCOMObjects(_LMModelItem);
2630
                        if (_LMSymbol != null)
2631
                            ReleaseCOMObjects(_LMSymbol);
2632
                    }
2633
                    else if (item.GetType() == typeof(Line))
2634
                    {
2635
                        Line line = item as Line;
2636
                        if (line != null)
2637 a7e9beec gaqhf
                        {
2638 8634af60 gaqhf
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2639
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2640 a7e9beec gaqhf
                            {
2641 8634af60 gaqhf
                                foreach (var attribute in lineNumber.ATTRIBUTES)
2642
                                {
2643
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2644 b2d1c1aa gaqhf
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2645 8634af60 gaqhf
                                    {
2646
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
2647
                                        if (_LMAAttribute != null)
2648
                                        {
2649
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2650
                                                _LMAAttribute.set_Value(attribute.VALUE);
2651
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
2652
                                                _LMAAttribute.set_Value(attribute.VALUE);
2653
                                            
2654
                                        }
2655
                                    }
2656
                                }
2657 68464385 gaqhf
                                _LMModelItem.Commit();
2658 a7e9beec gaqhf
                            }
2659 8634af60 gaqhf
                            if (_LMModelItem != null)
2660
                                ReleaseCOMObjects(_LMModelItem);
2661 a7e9beec gaqhf
                        }
2662
                    }
2663
                }
2664
            }
2665 f31645b6 gaqhf
2666
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2667 a7e9beec gaqhf
        }
2668
2669 74752074 gaqhf
        /// <summary>
2670
        /// Symbol Attribute 입력 메서드
2671
        /// </summary>
2672 73415441 gaqhf
        /// <param name="item"></param>
2673
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
2674 1efc25a3 gaqhf
        {
2675 ac78b508 gaqhf
            try
2676 1efc25a3 gaqhf
            {
2677 73415441 gaqhf
                // Object 아이템이 Symbol일 경우 Equipment일 경우 
2678
                string sRep = null;
2679
                if (targetItem.GetType() == typeof(Symbol))
2680
                    sRep = ((Symbol)targetItem).SPPID.RepresentationId;
2681
                else if (targetItem.GetType() == typeof(Equipment))
2682
                    sRep = ((Equipment)targetItem).SPPID.RepresentationId;
2683
                    
2684
                if (!string.IsNullOrEmpty(sRep))
2685 402ef5b1 gaqhf
                {
2686 73415441 gaqhf
                    LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
2687 310aeb31 gaqhf
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
2688 ea80efaa gaqhf
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
2689 402ef5b1 gaqhf
2690 73415441 gaqhf
                    foreach (var item in targetAttributes)
2691 65a1ed4b gaqhf
                    {
2692 ac78b508 gaqhf
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
2693 26c6f818 gaqhf
                        if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
2694 ac78b508 gaqhf
                        {
2695
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
2696
                            if (_Attribute != null)
2697 73415441 gaqhf
                                _Attribute.set_Value(item.VALUE);
2698 ac78b508 gaqhf
                        }
2699 65a1ed4b gaqhf
                    }
2700 73415441 gaqhf
                    _LMModelItem.Commit();
2701
                    
2702 ea80efaa gaqhf
                    ReleaseCOMObjects(_Attributes);
2703
                    ReleaseCOMObjects(_LMModelItem);
2704 73415441 gaqhf
                    ReleaseCOMObjects(_LMSymbol);
2705 ac78b508 gaqhf
                }
2706
            }
2707
            catch (Exception ex)
2708
            {
2709
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2710 1efc25a3 gaqhf
            }
2711 f31645b6 gaqhf
2712
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2713 1efc25a3 gaqhf
        }
2714
2715 74752074 gaqhf
        /// <summary>
2716 16584d30 gaqhf
        /// Input SpecBreak Attribute
2717
        /// </summary>
2718
        /// <param name="specBreak"></param>
2719
        private void InputSpecBreakAttribute(SpecBreak specBreak)
2720
        {
2721
            try
2722
            {
2723
                object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
2724
                object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
2725
2726
                if (upStreamObj != null &&
2727
                    downStreamObj != null)
2728
                {
2729
                    LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2730
2731
                    if (targetLMConnector != null)
2732
                    {
2733
                        foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
2734
                        {
2735
                            string symbolPath = _LMLabelPersist.get_FileName();
2736
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
2737
                            if (mapping != null)
2738
                            {
2739
                                BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
2740
                                if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2741
                                {
2742
                                    string[] values = attribute.VALUE.Split(new char[] { ',' });
2743
                                    if (values.Length == 2)
2744
                                    {
2745
                                        string upStreamValue = values[0];
2746
                                        string downStreamValue = values[1];
2747
2748
                                        InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
2749
                                    }
2750
                                }
2751
                            }
2752
                        }
2753
2754
                        ReleaseCOMObjects(targetLMConnector);
2755
                    }
2756
                }
2757
            }
2758
            catch (Exception ex)
2759
            {
2760
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2761
            }
2762
2763
            #region 내부에서만 쓰는 메서드
2764
            void InputAttributeForSpecBreak(object upStreamObj, object downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
2765
            {
2766
                try
2767
                {
2768
                    Symbol upStreamSymbol = upStreamObj as Symbol;
2769
                    Line upStreamLine = upStreamObj as Line;
2770
                    Symbol downStreamSymbol = downStreamObj as Symbol;
2771
                    Line downStreamLine = downStreamObj as Line;
2772
                    // 둘다 Line일 경우
2773
                    if (upStreamLine != null && downStreamLine != null)
2774
                    {
2775
                        InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
2776
                        InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
2777
                    }
2778
                    // 둘다 Symbol일 경우
2779
                    else if (upStreamSymbol != null && downStreamSymbol != null)
2780
                    {
2781
                        LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
2782
                        LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
2783
                        LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
2784
2785
                        foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
2786
                        {
2787
                            if (connector.get_ItemStatus() != "Active")
2788
                                continue;
2789
2790
                            if (connector.Id != zeroLenthConnector.Id)
2791
                                InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
2792
                        }
2793
2794
                        foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
2795
                        {
2796
                            if (connector.get_ItemStatus() != "Active")
2797
                                continue;
2798
2799
                            if (connector.Id != zeroLenthConnector.Id)
2800
                                InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
2801
                        }
2802
2803
                        foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
2804
                        {
2805
                            if (connector.get_ItemStatus() != "Active")
2806
                                continue;
2807
2808
                            if (connector.Id != zeroLenthConnector.Id)
2809
                                InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
2810
                        }
2811
2812
                        foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
2813
                        {
2814
                            if (connector.get_ItemStatus() != "Active")
2815
                                continue;
2816
2817
                            if (connector.Id != zeroLenthConnector.Id)
2818
                                InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
2819
                        }
2820
2821
                        ReleaseCOMObjects(zeroLenthConnector);
2822
                        ReleaseCOMObjects(upStreamLMSymbol);
2823
                        ReleaseCOMObjects(downStreamLMSymbol);
2824
                    }
2825
                    else if (upStreamSymbol != null && downStreamLine != null)
2826
                    {
2827
                        LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
2828
                        InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
2829
                        LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
2830
2831
                        foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
2832
                        {
2833
                            if (connector.get_ItemStatus() != "Active")
2834
                                continue;
2835
2836
                            if (connector.Id != zeroLenthConnector.Id)
2837
                                InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
2838
                        }
2839
2840
                        foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
2841
                        {
2842
                            if (connector.get_ItemStatus() != "Active")
2843
                                continue;
2844
2845
                            if (connector.Id != zeroLenthConnector.Id)
2846
                                InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
2847
                        }
2848
2849
                        ReleaseCOMObjects(zeroLenthConnector);
2850
                        ReleaseCOMObjects(upStreamLMSymbol);
2851
                    }
2852
                    else if (upStreamLine != null && downStreamSymbol != null)
2853
                    {
2854
                        LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
2855
                        InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
2856
                        LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
2857
2858
                        foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
2859
                        {
2860
                            if (connector.get_ItemStatus() != "Active")
2861
                                continue;
2862
2863
                            if (connector.Id != zeroLenthConnector.Id)
2864
                                InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
2865
                        }
2866
2867
                        foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
2868
                        {
2869
                            if (connector.get_ItemStatus() != "Active")
2870
                                continue;
2871
2872
                            if (connector.Id != zeroLenthConnector.Id)
2873
                                InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
2874
                        }
2875
2876
                        ReleaseCOMObjects(zeroLenthConnector);
2877
                        ReleaseCOMObjects(downStreamLMSymbol);
2878
                    }
2879
                }
2880
                catch (Exception ex)
2881
                {
2882
                    System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2883
                }
2884
            }
2885
2886
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
2887
            {
2888
                try
2889
                {
2890
                    LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2891
                    if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2892
                    {
2893
                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
2894
                        if (_LMAAttribute != null)
2895
                        {
2896
                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2897
                                _LMAAttribute.set_Value(value);
2898
                            else if (_LMAAttribute.get_Value() != value)
2899
                                _LMAAttribute.set_Value(value);
2900
                        }
2901
2902
                        _LMModelItem.Commit();
2903
                    }
2904
                    if (_LMModelItem != null)
2905
                        ReleaseCOMObjects(_LMModelItem);
2906
                }
2907
                catch (Exception ex)
2908
                {
2909
                    System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2910
                }
2911
            }
2912
2913
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
2914
            {
2915
                try
2916
                {
2917
                    LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
2918
                    if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2919
                    {
2920
                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
2921
                        if (_LMAAttribute != null)
2922
                        {
2923
                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2924
                                _LMAAttribute.set_Value(value);
2925
                            else if (_LMAAttribute.get_Value() != value)
2926
                                _LMAAttribute.set_Value(value);
2927
                        }
2928
2929
                        _LMModelItem.Commit();
2930
                    }
2931
                    if (_LMModelItem != null)
2932
                        ReleaseCOMObjects(_LMModelItem);
2933
                }
2934
                catch (Exception ex)
2935
                {
2936
                    System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2937
                }
2938
            }
2939
            #endregion
2940
        }
2941
2942
        /// <summary>
2943 74752074 gaqhf
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
2944
        /// </summary>
2945
        /// <param name="text"></param>
2946 cfda1fed gaqhf
        private void TextModeling(Text text)
2947
        {
2948 6b298450 gaqhf
            LMSymbol _LMSymbol = null;
2949 0860c756 gaqhf
            LMConnector connectedLMConnector = null;
2950 6b298450 gaqhf
            try
2951
            {
2952 8b069d9f gaqhf
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
2953
                if (text.ASSOCIATION)
2954 ea80efaa gaqhf
                {
2955
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
2956
                    if (owner.GetType() == typeof(Symbol))
2957
                    {
2958
                        Symbol symbol = owner as Symbol;
2959
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2960
                        if (_LMSymbol != null)
2961
                        {
2962
                            Association association = symbol.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
2963
                            List<BaseModel.Attribute> attributes = symbol.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
2964
                            AttributeMapping mapping = null;
2965
                            foreach (var attribute in attributes)
2966
                            {
2967 26c6f818 gaqhf
                                if (string.IsNullOrEmpty(attribute.VALUE) || attribute.VALUE == "None")
2968
                                    continue;
2969
2970 ea80efaa gaqhf
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
2971
                                if (mapping != null)
2972
                                    break;  
2973
                            }
2974
2975
                            if (mapping != null)
2976
                            {
2977 1a3a74a8 gaqhf
                                double x = 0;
2978
                                double y = 0;
2979
2980 b65a7e32 gaqhf
                                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, mapping.Location);
2981
                                Array array = new double[] { 0, x, y };
2982 1a3a74a8 gaqhf
2983 1ba9c671 gaqhf
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
2984 ea80efaa gaqhf
                                if (_LMLabelPersist!=null)
2985
                                {
2986 f9125a54 gaqhf
                                    _LMLabelPersist.Commit();
2987 ea80efaa gaqhf
                                    ReleaseCOMObjects(_LMLabelPersist);
2988
                                }
2989
                            }
2990
                        }
2991
                    }
2992
                    else if (owner.GetType() == typeof(Line))
2993
                    {
2994 0860c756 gaqhf
                        Line line = owner as Line;
2995
                        Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2996
                        connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
2997 ea80efaa gaqhf
2998 0860c756 gaqhf
                        if (connectedLMConnector != null)
2999
                        {
3000
                            Association association = line.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
3001
                            List<BaseModel.Attribute> attributes = line.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
3002
                            AttributeMapping mapping = null;
3003
                            foreach (var attribute in attributes)
3004
                            {
3005
                                if (string.IsNullOrEmpty(attribute.VALUE) || attribute.VALUE == "None")
3006
                                    continue;
3007
3008
                                mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3009
                                if (mapping != null)
3010
                                    break;
3011
                            }
3012
3013
                            if (mapping != null)
3014
                            {
3015
                                double x = 0;
3016
                                double y = 0;
3017
3018
                                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, mapping.Location);
3019
                                Array array = new double[] { 0, x, y };
3020
3021 71ba1ca3 gaqhf
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3022 0860c756 gaqhf
                                if (_LMLabelPersist != null)
3023
                                {
3024
                                    _LMLabelPersist.Commit();
3025
                                    ReleaseCOMObjects(_LMLabelPersist);
3026
                                }
3027
                            }
3028
                        }
3029 ea80efaa gaqhf
                    }
3030
                }
3031
                else
3032
                {
3033
                    LMItemNote _LMItemNote = null;
3034
                    LMAAttribute _LMAAttribute = null;
3035
3036 b65a7e32 gaqhf
                    double x = 0;
3037
                    double y = 0;
3038
3039
                    CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
3040
3041
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y);
3042 ea80efaa gaqhf
                    _LMSymbol.Commit();
3043
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3044
                    _LMItemNote.Commit();
3045
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3046
                    _LMAAttribute.set_Value(text.VALUE);
3047
                    _LMItemNote.Commit();
3048
3049
                    if (_LMAAttribute != null)
3050
                        ReleaseCOMObjects(_LMAAttribute);
3051
                    if (_LMItemNote != null)
3052
                        ReleaseCOMObjects(_LMItemNote);
3053
                }
3054 6b298450 gaqhf
            }
3055
            catch (Exception ex)
3056
            {
3057 cfda1fed gaqhf
3058 6b298450 gaqhf
            }
3059
            finally
3060
            {
3061
                if (_LMSymbol != null)
3062
                    ReleaseCOMObjects(_LMSymbol);
3063
            }
3064 f31645b6 gaqhf
3065
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
3066 cfda1fed gaqhf
        }
3067
3068 74752074 gaqhf
        /// <summary>
3069
        /// Note Modeling
3070
        /// </summary>
3071
        /// <param name="note"></param>
3072 cfda1fed gaqhf
        private void NoteModeling(Note note)
3073
        {
3074 6b298450 gaqhf
            LMSymbol _LMSymbol = null;
3075
            LMItemNote _LMItemNote = null;
3076
            LMAAttribute _LMAAttribute = null;
3077
3078
            try
3079
            {
3080 b65a7e32 gaqhf
                double x = 0;
3081
                double y = 0;
3082
3083
                CalcLabelLocation(ref x, ref y, note.SPPID.ORIGINAL_X, note.SPPID.ORIGINAL_Y, note.SPPIDLabelLocation, _ETCSetting.NoteLocation);
3084
3085
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
3086 6b298450 gaqhf
                _LMSymbol.Commit();
3087
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3088
                _LMItemNote.Commit();
3089
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3090
                _LMAAttribute.set_Value(note.VALUE);
3091
                _LMItemNote.Commit();
3092
            }
3093
            catch (Exception ex)
3094
            {
3095 cfda1fed gaqhf
3096 6b298450 gaqhf
            }
3097
            finally
3098
            {
3099
                if (_LMAAttribute != null)
3100
                    ReleaseCOMObjects(_LMAAttribute);
3101
                if (_LMItemNote != null)
3102
                    ReleaseCOMObjects(_LMItemNote);
3103
                if (_LMSymbol != null)
3104
                    ReleaseCOMObjects(_LMSymbol);
3105
            }
3106 f31645b6 gaqhf
3107
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
3108 cfda1fed gaqhf
        }
3109
3110 74752074 gaqhf
        /// <summary>
3111
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
3112
        /// </summary>
3113
        /// <param name="x"></param>
3114
        /// <param name="y"></param>
3115
        /// <param name="originX"></param>
3116
        /// <param name="originY"></param>
3117
        /// <param name="SPPIDLabelLocation"></param>
3118
        /// <param name="location"></param>
3119 b65a7e32 gaqhf
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
3120
        {
3121
            if (location == Location.None)
3122
            {
3123
                x = originX;
3124
                y = originY;
3125
            }
3126
            else
3127
            {
3128
                if (location.HasFlag(Location.Center))
3129
                {
3130
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
3131
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
3132
                }
3133
3134
                if (location.HasFlag(Location.Left))
3135
                    x = SPPIDLabelLocation.X1;
3136
                else if (location.HasFlag(Location.Right))
3137
                    x = SPPIDLabelLocation.X2;
3138
3139
                if (location.HasFlag(Location.Down))
3140
                    y = SPPIDLabelLocation.Y1;
3141
                else if (location.HasFlag(Location.Up))
3142
                    y = SPPIDLabelLocation.Y2;
3143
            }
3144
        }
3145 5a4b8f32 gaqhf
3146 74752074 gaqhf
        /// <summary>
3147 4d2571ab gaqhf
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
3148
        /// 1. Angle Valve
3149
        /// 2. 3개로 이루어진 Symbol Group
3150
        /// </summary>
3151
        /// <returns></returns>
3152
        private List<Symbol> GetPrioritySymbol()
3153
        {
3154
            DataTable symbolTable = document.SymbolTable;
3155
3156
            // List에 순서대로 쌓는다.
3157
            List<Symbol> symbols = new List<Symbol>();
3158
            // Angle Valve 부터
3159
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.Count == 2))
3160
            {
3161
                if (!symbols.Contains(symbol))
3162
                {
3163
                    double originX = 0;
3164
                    double originY = 0;
3165
3166
                    // ID2 Table에서 Original Point 가져옴.
3167
                    string OriginalPoint = symbolTable.Select(string.Format("UID = {0}",symbol.DBUID))[0]["OriginalPoint"].ToString();
3168
                    SPPIDUtil.ConvertPointBystring(OriginalPoint, ref originX, ref originY);
3169
3170
                    SlopeType slopeType1 = SlopeType.None;
3171
                    SlopeType slopeType2 = SlopeType.None;
3172
                    foreach (Connector connector in symbol.CONNECTORS)
3173
                    {
3174
                        double connectorX = 0;
3175
                        double connectorY = 0;
3176
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
3177
                        if (slopeType1 == SlopeType.None)
3178
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3179
                        else
3180
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3181
                    }
3182
3183
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
3184
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
3185
                        symbols.Add(symbol);
3186
                }
3187
            }
3188
3189
            // 3개의 Symbol이 뭉쳐 있을 때
3190
            foreach (var item in document.SYMBOLS)
3191
            {
3192
                List<Symbol> group = new List<Symbol>();
3193
                SPPIDUtil.FindConnectedSymbolGroup(document, item, group);
3194
                if (group.Count == 3)
3195
                {
3196
                    Symbol symbol = SPPIDUtil.FindCenterAtThreeSymbols(document, group);
3197
                    if (!symbols.Contains(symbol))
3198
                        symbols.Add(symbol);
3199
                }
3200
            }
3201
3202
            // Connection Point가 3개 이상
3203
            foreach (var symbol in document.SYMBOLS)
3204
                if (symbol.CONNECTORS.Count > 2 && !symbols.Contains(symbol))
3205
                    symbols.Add(symbol);
3206
3207
            return symbols;
3208
        }
3209
3210
        /// <summary>
3211 b2d1c1aa gaqhf
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
3212
        /// </summary>
3213
        /// <param name="graphicOID"></param>
3214
        /// <param name="milliseconds"></param>
3215
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
3216
        {
3217
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3218
            {
3219
                double minX = 0;
3220
                double minY = 0;
3221
                double maxX = 0;
3222
                double maxY = 0;
3223
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
3224
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
3225
3226
                Thread.Sleep(milliseconds);
3227
            }
3228
        }
3229
3230
        /// <summary>
3231 74752074 gaqhf
        /// ComObject를 Release
3232
        /// </summary>
3233
        /// <param name="objVars"></param>
3234 5a4b8f32 gaqhf
        public void ReleaseCOMObjects(params object[] objVars)
3235
        {
3236
            int intNewRefCount = 0;
3237
            foreach (object obj in objVars)
3238
            {
3239
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
3240
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
3241
            }
3242
        }
3243 cfda1fed gaqhf
    }
3244
}
클립보드 이미지 추가 (최대 크기: 500 MB)