프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 0819fd28

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

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using Llama;
7
using Plaice;
8
using Converter.BaseModel;
9
using Converter.SPPID.Model;
10
using Converter.SPPID.Properties;
11
using Converter.SPPID.Util;
12
using Converter.SPPID.DB;
13

    
14
using System.Windows;
15
using System.Threading;
16
using System.Drawing;
17
using Microsoft.VisualBasic;
18
using Newtonsoft.Json;
19

    
20
namespace Converter.SPPID
21
{
22
    public class AutoModeling
23
    {
24
        Placement _placement;
25
        LMADataSource dataSource;
26

    
27
        SPPID_Document document;
28

    
29
        public AutoModeling(SPPID_Document document)
30
        {
31
            this.document = document;
32
        }
33

    
34
        public void Run()
35
        {
36
            _placement = new Placement();
37
            dataSource = _placement.PIDDataSource;
38

    
39
            //dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
40
            //dynamic newDrawing = application.Drawings.Add(document.Unit, document.Template, document.DrawingNumber, document.DrawingName);
41
            //application.ActiveWindow.Fit();
42
            //Thread.Sleep(100);
43
            //application.ActiveWindow.Zoom = 60;
44
            //Thread.Sleep(100);
45

    
46
            foreach (Equipment equipment in document.Equipments)
47
            {
48
                SymbolModeling(equipment as Symbol, null, null);
49
            }
50

    
51
            foreach (LineNumber lineNumber in document.LINENUMBERS)
52
            {
53
                foreach (LineNumberRun run in lineNumber.RUNS)
54
                {
55
                    SymbolModelingByRun(run);
56
                    LineModelingByRun(run);
57

    
58
                    //bool IsFirst = true;
59
                    //// Run 별로 Modeling
60
                    //Symbol prevSymbol = null;
61
                    //object prevItem = null;
62

    
63
                    
64

    
65
                    //// Symbol 먼저
66
                    //foreach (var item in run.RUNITEMS)
67
                    //{
68
                    //    if (item.GetType() == typeof(Symbol))
69
                    //    {
70
                    //        Symbol symbol = item as Symbol;
71
                    //        SymbolModeling(symbol, prevSymbol, prevItem, IsFirst);
72
                    //        prevSymbol = symbol;
73
                    //    }
74
                    //    IsFirst = false;
75
                    //    prevItem = item;
76
                    //}
77

    
78

    
79
                    //Line prevLine = null;
80
                    //prevSymbol = null;
81
                    //prevItem = null;
82
                    //List<Line> lines = new List<Line>();
83
                    //foreach (var item in run.RUNITEMS)
84
                    //{
85
                    //    // Line일 경우
86
                    //    if (item.GetType() == typeof(Line))
87
                    //    {
88
                    //        Line line = item as Line;
89
                    //        if (prevLine == null)
90
                    //            lines.Add(line);
91
                    //        else if (prevLine != null)
92
                    //        {
93
                    //            if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
94
                    //                lines.Add(line);
95
                    //            else
96
                    //            {
97
                    //                LineModeling(lines);
98
                    //                lines.Clear();
99
                    //                lines.Add(line);
100
                    //            }
101
                    //        }
102

    
103
                    //        prevLine = line;
104
                    //    }
105
                    //    // Symbol 일 경우
106
                    //    else if (item.GetType() == typeof(Symbol))
107
                    //    {
108
                    //        if (lines.Count > 0)
109
                    //        {
110
                    //            LineModeling(lines);
111
                    //            lines.Clear();
112
                    //        }
113
                    //    }
114
                    //    prevItem = item;
115
                    //}
116

    
117
                    //if (lines.Count > 0)
118
                    //    LineModeling(lines);
119
                }
120
            }
121

    
122

    
123
            ReleaseCOMObjects(dataSource);
124
            ReleaseCOMObjects(_placement);
125
            System.Windows.Forms.MessageBox.Show("end");
126
        }
127

    
128
        private void LineModelingByRun(LineNumberRun run)
129
        {
130
            Line prevLine = null;
131
            List<Line> lines = new List<Line>();
132
            foreach (var item in run.RUNITEMS)
133
            {
134
                // Line일 경우
135
                if (item.GetType() == typeof(Line))
136
                {
137
                    Line line = item as Line;
138
                    if (prevLine == null)
139
                        lines.Add(line);
140
                    else if (prevLine != null)
141
                    {
142
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
143
                            lines.Add(line);
144
                        else
145
                        {
146
                            LineModeling(lines);
147
                            lines.Clear();
148
                            lines.Add(line);
149
                        }
150
                    }
151

    
152
                    prevLine = line;
153
                }
154
                // Symbol 일 경우
155
                else if (item.GetType() == typeof(Symbol))
156
                {
157
                    if (lines.Count > 0)
158
                    {
159
                        LineModeling(lines);
160
                        lines.Clear();
161
                    }
162
                }
163
            }
164

    
165
            if (lines.Count > 0)
166
                LineModeling(lines);
167
        }
168

    
169
        private void SymbolModelingByRun(LineNumberRun run)
170
        {
171
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
172
            if (run.RUNITEMS.Count > 0)
173
            {
174
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
175
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
176

    
177
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
178
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
179
            }
180

    
181
            Symbol prevSymbol = null;
182
            Symbol targetSymbol = null;
183
            foreach (var item in run.RUNITEMS)
184
            {
185
                if (item.GetType() == typeof(Symbol))
186
                {
187
                    Symbol symbol = item as Symbol;
188
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
189
                    prevSymbol = symbol;
190
                    targetSymbol = symbol;
191
                }
192
                else
193
                {
194
                    targetSymbol = null;
195
                }
196
            }
197
        }
198

    
199
        private void SymbolModelingByRunStart(Symbol symbol, LineNumberRun run)
200
        {
201
            foreach (var connector in symbol.CONNECTORS)
202
            {
203
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
204
                if (targetItem != null &&
205
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
206
                    !IsSameLineNumber(symbol, targetItem))
207
                {
208
                    SymbolModeling(symbol, targetItem as Symbol, null);
209
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
210
                    {
211
                        object item = run.RUNITEMS[i];
212
                        if (item.GetType() == typeof(Symbol))
213
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol, null);
214
                        else
215
                            break;
216
                    }
217
                    break;
218
                }
219
            }
220

    
221

    
222
        }
223

    
224
        private void SymbolModelingByRunEnd(Symbol symbol, LineNumberRun run)
225
        {
226
            foreach (var connector in symbol.CONNECTORS)
227
            {
228
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
229
                if (targetItem != null &&
230
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
231
                    !IsSameLineNumber(symbol, targetItem))
232
                {
233
                    SymbolModeling(symbol, targetItem as Symbol, null);
234
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
235
                    {
236
                        object item = run.RUNITEMS[i];
237
                        if (item.GetType() == typeof(Symbol))
238
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol, null);
239
                        else
240
                            break;
241
                    }
242
                    break;
243
                }
244
            }
245
        }
246

    
247

    
248
        private void TestSource()
249
        {
250

    
251
        }
252

    
253
        private void SymbolModeling(Symbol symbol, Symbol prevSymbol, object prevItem, bool IsFirst)
254
        {
255
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
256
                return;
257

    
258
            LMSymbol _LMSymbol = null;
259

    
260
            string mappingPath = symbol.SPPID.MAPPINGNAME;
261
            double x = symbol.SPPID.ORIGINAL_X;
262
            double y = symbol.SPPID.ORIGINAL_Y;
263
            int mirror = 0;
264
            double angle = symbol.ANGLE;
265
            LMSymbol _TargetItem = null;
266

    
267
            if (IsFirst)
268
            {
269
                foreach (var connector in symbol.CONNECTORS)
270
                {
271
                    object item = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
272
                    if (item != null && 
273
                        (item.GetType() == typeof(Symbol) || item.GetType() == typeof(Equipment)) && 
274
                        !IsSameLineNumber(symbol, item))
275
                    {
276
                        _TargetItem = dataSource.GetSymbol(((Symbol)item).SPPID.RepresentationId);
277
                        break;
278
                    }
279
                }
280
            }
281
            else if (prevItem != null && prevItem.GetType() == typeof(Symbol))
282
                _TargetItem = dataSource.GetSymbol(((Symbol)prevItem).SPPID.RepresentationId);
283

    
284
            if (prevSymbol != null)
285
            {
286
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
287
                LMSymbol prevLMSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
288
                double prevX = prevLMSymbol.get_XCoordinate();
289
                double prevY = prevLMSymbol.get_YCoordinate();
290
                if (slopeType == SlopeType.HORIZONTAL)
291
                    y = prevY;
292
                else if (slopeType == SlopeType.VERTICAL)
293
                    x = prevX;
294

    
295
                ReleaseCOMObjects(prevLMSymbol);
296
            }
297

    
298
            if (_TargetItem == null)
299
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
300
            else
301
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
302

    
303
            if (_LMSymbol != null)
304
            {
305
                _LMSymbol.Commit();
306
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
307
            }
308

    
309
            if (_TargetItem != null)
310
                ReleaseCOMObjects(_TargetItem);
311
            
312
            ReleaseCOMObjects(_LMSymbol);
313
        }
314

    
315
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
316
        {
317
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
318
                return;
319

    
320
            LMSymbol _LMSymbol = null;
321

    
322
            string mappingPath = symbol.SPPID.MAPPINGNAME;
323
            double x = symbol.SPPID.ORIGINAL_X;
324
            double y = symbol.SPPID.ORIGINAL_Y;
325
            int mirror = 0;
326
            double angle = symbol.ANGLE;
327

    
328
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
329
            {
330
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
331
                //SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, targetSymbol.SPPID.ORIGINAL_X, targetSymbol.SPPID.ORIGINAL_Y);
332
                //double prevX = _TargetItem.get_XCoordinate();
333
                //double prevY = _TargetItem.get_YCoordinate();
334
                //if (slopeType == SlopeType.HORIZONTAL)
335
                //    y = prevY;
336
                //else if (slopeType == SlopeType.VERTICAL)
337
                //    x = prevX;
338

    
339
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
340
                ReleaseCOMObjects(_TargetItem);
341
            }
342
            else if (prevSymbol != null)
343
            {
344
                LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
345
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
346
                double prevX = _PrevSymbol.get_XCoordinate();
347
                double prevY = _PrevSymbol.get_YCoordinate();
348
                if (slopeType == SlopeType.HORIZONTAL)
349
                    y = prevY;
350
                else if (slopeType == SlopeType.VERTICAL)
351
                    x = prevX;
352
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
353
            }
354
            else
355
            {
356
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
357
            }
358

    
359

    
360
            if (_LMSymbol != null)
361
            {
362
                _LMSymbol.Commit();
363
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
364
            }
365

    
366
            ReleaseCOMObjects(_LMSymbol);
367
        }
368

    
369
        private bool IsSameLineNumber(object item, object targetItem)
370
        {
371
            foreach (var lineNumber in document.LINENUMBERS)
372
            {
373
                foreach (var run in lineNumber.RUNS)
374
                {
375
                    foreach (var runItem in run.RUNITEMS)
376
                    {
377
                        if (runItem == item)
378
                        {
379
                            foreach (var findItem in run.RUNITEMS)
380
                            {
381
                                if (findItem == targetItem)
382
                                {
383
                                    return true;
384
                                }
385
                            }
386

    
387
                            return false;
388

    
389
                        }
390
                    }
391
                }
392
            }
393

    
394
            return false;
395
        }
396

    
397
        private void LineModeling(List<Line> lines)
398
        {
399
            object DrwingID = "0";
400
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME, ref DrwingID);
401
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
402
            LMSymbol _LMSymbol1 = null;
403
            LMSymbol _LMSymbol2 = null;
404
            for (int i = 0; i < lines.Count; i++)
405
            {
406
                Line line = lines[i];
407
                if (i == 0 || i + 1 != lines.Count)
408
                {
409
                    // 시작점에 연결된 Symbol 찾기
410
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
411
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
412
                    {
413
                        _LMSymbol1 = dataSource.GetSymbol(((Symbol)connItem).SPPID.RepresentationId);
414
                        if (_LMSymbol1 != null)
415
                            placeRunInputs.AddSymbolTarget(_LMSymbol1, line.SPPID.START_X, line.SPPID.START_Y);
416
                        else
417
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
418
                    }
419
                    else
420
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
421
                }
422

    
423
                if (i + 1 == lines.Count)
424
                {
425
                    // 끝점에 연결된 Symbol 찾기
426
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
427
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
428
                    {
429
                        _LMSymbol2 = dataSource.GetSymbol(((Symbol)connItem).SPPID.RepresentationId);
430
                        if (_LMSymbol2 != null)
431
                        {
432
                            if (i != 0)
433
                                placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
434

    
435
                            placeRunInputs.AddSymbolTarget(_LMSymbol2, line.SPPID.END_X, line.SPPID.END_Y);
436
                        }
437
                        else
438
                        {
439
                            if (i != 0)
440
                                placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
441
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
442
                        }
443
                            
444
                    }
445
                    else
446
                    {
447
                        if (i != 0)
448
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
449
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
450
                    }
451
                }
452
            }
453

    
454
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
455
            if (_lMConnector != null)
456
            {
457
                foreach (var line in lines)
458
                    line.SPPID.RepresentationId = _lMConnector.AsLMRepresentation().Id;
459
                _lMConnector.Commit();
460
            }
461
            
462

    
463
            if (_LMSymbol1 != null)
464
                ReleaseCOMObjects(_LMSymbol1);
465
            if (_LMSymbol2 != null)
466
                ReleaseCOMObjects(_LMSymbol2);
467

    
468
            ReleaseCOMObjects(_lMConnector);
469
            ReleaseCOMObjects(placeRunInputs);
470
            ReleaseCOMObjects(_LMAItem);
471
        }
472

    
473
        private void LineNumberModeling(LineNumber lineNumber)
474
        {
475

    
476
        }
477

    
478
        private void TextModeling(Text text)
479
        {
480

    
481
        }
482

    
483
        private void NoteModeling(Note note)
484
        {
485

    
486
        }
487

    
488

    
489
        public void ReleaseCOMObjects(params object[] objVars)
490
        {
491
            int intNewRefCount = 0;
492
            foreach (object obj in objVars)
493
            {
494
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
495
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
496
            }
497
        }
498
    }
499
}