프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 5e6ecf05

이력 | 보기 | 이력해설 | 다운로드 (21.9 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 Ingr.RAD2D.Interop.RAD2D;
9
using Ingr.RAD2D.Internal;
10
using Ingr.RAD2D.Helper;
11
using Converter.BaseModel;
12
using Converter.SPPID.Model;
13
using Converter.SPPID.Properties;
14
using Converter.SPPID.Util;
15
using Converter.SPPID.DB;
16
using Ingr.RAD2D.MacroControls.CmdCtrl;
17
using Ingr.RAD2D;
18
using System.Windows;
19
using System.Threading;
20
using System.Drawing;
21
using Microsoft.VisualBasic;
22
using Newtonsoft.Json;
23

    
24
namespace Converter.SPPID
25
{
26
    public class AutoModeling
27
    {
28
        Placement _placement;
29
        LMADataSource dataSource;
30
        Ingr.RAD2D.Application radApp;
31
        SPPID_Document document;
32

    
33
        public AutoModeling(SPPID_Document document, Ingr.RAD2D.Application radApp)
34
        {
35
            this.document = document;
36
            this.radApp = radApp;
37
        }
38

    
39
        public void Run()
40
        {
41
            _placement = new Placement();
42
            dataSource = _placement.PIDDataSource;
43
            //dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
44
            //dynamic newDrawing = application.Drawings.Add(document.Unit, document.Template, document.DrawingNumber, document.DrawingName);
45
            //application.ActiveWindow.Fit();
46
            //Thread.Sleep(100);
47
            //application.ActiveWindow.Zoom = 60;
48
            //Thread.Sleep(100);
49
            try
50
            {
51
                foreach (Equipment equipment in document.Equipments)
52
                {
53
                    SymbolModeling(equipment as Symbol, null, null);
54
                }
55

    
56
                foreach (LineNumber lineNumber in document.LINENUMBERS)
57
                {
58
                    foreach (LineRun run in lineNumber.RUNS)
59
                    {
60
                        SymbolModelingByRun(run);
61
                    }
62
                }
63

    
64
                foreach (TrimLine trimLine in document.TRIMLINES)
65
                {
66
                    foreach (LineRun run in trimLine.RUNS)
67
                    {
68
                        SymbolModelingByRun(run);
69
                    }
70
                }
71

    
72
                foreach (LineNumber lineNumber in document.LINENUMBERS)
73
                {
74
                    foreach (LineRun run in lineNumber.RUNS)
75
                    {
76
                        LineModelingByRun(run);
77
                    }
78
                }
79

    
80
                foreach (TrimLine trimLine in document.TRIMLINES)
81
                {
82
                    foreach (LineRun run in trimLine.RUNS)
83
                    {
84
                        LineModelingByRun(run);
85
                    }
86
                }
87
            }
88
            catch (Exception ex)
89
            {
90
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
91
            }
92
            finally
93
            {
94
                ReleaseCOMObjects(dataSource);
95
                ReleaseCOMObjects(_placement);
96
            }
97
           
98
            System.Windows.Forms.MessageBox.Show("end");
99
        }
100

    
101
        private void LineModelingByRun(LineRun run)
102
        {
103
            Line prevLine = null;
104
            List<Line> lines = new List<Line>();
105
            foreach (var item in run.RUNITEMS)
106
            {
107
                // Line일 경우
108
                if (item.GetType() == typeof(Line))
109
                {
110
                    Line line = item as Line;
111
                    if (prevLine == null)
112
                        lines.Add(line);
113
                    else if (prevLine != null)
114
                    {
115
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
116
                            lines.Add(line);
117
                        else
118
                        {
119
                            LineModeling(lines);
120
                            lines.Clear();
121
                            lines.Add(line);
122
                        }
123
                    }
124

    
125
                    prevLine = line;
126
                }
127
                // Symbol 일 경우
128
                else if (item.GetType() == typeof(Symbol))
129
                {
130
                    if (lines.Count > 0)
131
                    {
132
                        LineModeling(lines);
133
                        lines.Clear();
134
                    }
135
                }
136
            }
137

    
138
            if (lines.Count > 0)
139
                LineModeling(lines);
140
        }
141

    
142
        private void SymbolModelingByRun(LineRun run)
143
        {
144
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
145
            if (run.RUNITEMS.Count > 0)
146
            {
147
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
148
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
149

    
150
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
151
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
152
            }
153

    
154
            Symbol prevSymbol = null;
155
            Symbol targetSymbol = null;
156
            foreach (var item in run.RUNITEMS)
157
            {
158
                if (item.GetType() == typeof(Symbol))
159
                {
160
                    Symbol symbol = item as Symbol;
161
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
162
                    prevSymbol = symbol;
163
                    targetSymbol = symbol;
164
                }
165
                else
166
                {
167
                    targetSymbol = null;
168
                }
169
            }
170
        }
171

    
172
        private void SymbolModelingByRunStart(Symbol symbol, LineRun run)
173
        {
174
            foreach (var connector in symbol.CONNECTORS)
175
            {
176
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
177
                if (targetItem != null &&
178
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
179
                    !IsSameLineNumber(symbol, targetItem))
180
                {
181
                    SymbolModeling(symbol, targetItem as Symbol, null);
182
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
183
                    {
184
                        object item = run.RUNITEMS[i];
185
                        if (item.GetType() == typeof(Symbol))
186
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol, null);
187
                        else
188
                            break;
189
                    }
190
                    break;
191
                }
192
            }
193

    
194

    
195
        }
196

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

    
220

    
221
        private void TestSource()
222
        {
223

    
224
        }
225

    
226
        private void SymbolModeling(Symbol symbol, Symbol prevSymbol, object prevItem, bool IsFirst)
227
        {
228
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
229
                return;
230

    
231
            LMSymbol _LMSymbol = null;
232

    
233
            string mappingPath = symbol.SPPID.MAPPINGNAME;
234
            double x = symbol.SPPID.ORIGINAL_X;
235
            double y = symbol.SPPID.ORIGINAL_Y;
236
            int mirror = 0;
237
            double angle = symbol.ANGLE;
238
            LMSymbol _TargetItem = null;
239

    
240
            if (IsFirst)
241
            {
242
                foreach (var connector in symbol.CONNECTORS)
243
                {
244
                    object item = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
245
                    if (item != null && 
246
                        (item.GetType() == typeof(Symbol) || item.GetType() == typeof(Equipment)) && 
247
                        !IsSameLineNumber(symbol, item))
248
                    {
249
                        _TargetItem = dataSource.GetSymbol(((Symbol)item).SPPID.RepresentationId);
250
                        break;
251
                    }
252
                }
253
            }
254
            else if (prevItem != null && prevItem.GetType() == typeof(Symbol))
255
                _TargetItem = dataSource.GetSymbol(((Symbol)prevItem).SPPID.RepresentationId);
256
            
257
            if (prevSymbol != null)
258
            {
259
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
260
                LMSymbol prevLMSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
261
                double prevX = prevLMSymbol.get_XCoordinate();
262
                double prevY = prevLMSymbol.get_YCoordinate();
263
                if (slopeType == SlopeType.HORIZONTAL)
264
                    y = prevY;
265
                else if (slopeType == SlopeType.VERTICAL)
266
                    x = prevX;
267
                
268
                ReleaseCOMObjects(prevLMSymbol);
269
            }
270

    
271
            if (_TargetItem == null)
272
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
273
            else
274
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
275

    
276
            if (_LMSymbol != null)
277
            {
278
                _LMSymbol.Commit();
279
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
280
            }
281

    
282
            if (_TargetItem != null)
283
                ReleaseCOMObjects(_TargetItem);
284
            
285
            ReleaseCOMObjects(_LMSymbol);
286
        }
287

    
288
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
289
        {
290
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
291
                return;
292

    
293
            LMSymbol _LMSymbol = null;
294

    
295
            string mappingPath = symbol.SPPID.MAPPINGNAME;
296
            double x = symbol.SPPID.ORIGINAL_X;
297
            double y = symbol.SPPID.ORIGINAL_Y;
298
            int mirror = 0;
299
            double angle = symbol.ANGLE;
300

    
301
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
302
            {
303
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
304
                //SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, targetSymbol.SPPID.ORIGINAL_X, targetSymbol.SPPID.ORIGINAL_Y);
305
                //double prevX = _TargetItem.get_XCoordinate();
306
                //double prevY = _TargetItem.get_YCoordinate();
307
                //if (slopeType == SlopeType.HORIZONTAL)
308
                //    y = prevY;
309
                //else if (slopeType == SlopeType.VERTICAL)
310
                //    x = prevX;
311

    
312
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
313
                ReleaseCOMObjects(_TargetItem);
314
            }
315
            else if (prevSymbol != null)
316
            {
317
                LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
318
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
319
                double prevX = _PrevSymbol.get_XCoordinate();
320
                double prevY = _PrevSymbol.get_YCoordinate();
321
                if (slopeType == SlopeType.HORIZONTAL)
322
                    y = prevY;
323
                else if (slopeType == SlopeType.VERTICAL)
324
                    x = prevX;
325
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
326
            }
327
            else
328
            {
329
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
330
            }
331

    
332

    
333
            if (_LMSymbol != null)
334
            {
335
                _LMSymbol.Commit();
336
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
337
            }
338

    
339
            ReleaseCOMObjects(_LMSymbol);
340
        }
341

    
342
        private bool IsSameLineNumber(object item, object targetItem)
343
        {
344
            foreach (var lineNumber in document.LINENUMBERS)
345
            {
346
                foreach (var run in lineNumber.RUNS)
347
                {
348
                    foreach (var runItem in run.RUNITEMS)
349
                    {
350
                        if (runItem == item)
351
                        {
352
                            foreach (var findItem in run.RUNITEMS)
353
                            {
354
                                if (findItem == targetItem)
355
                                {
356
                                    return true;
357
                                }
358
                            }
359

    
360
                            return false;
361

    
362
                        }
363
                    }
364
                }
365
            }
366

    
367
            return false;
368
        }
369

    
370
        private void LineModeling(List<Line> lines)
371
        {
372
            object DrwingID = "0";
373
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME, ref DrwingID);
374
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
375
            LMSymbol _LMSymbol1 = null;
376
            LMSymbol _LMSymbol2 = null;
377
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
378
            LMConnector targetConnector1 = null;
379
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
380
            LMConnector targetConnector2 = null;
381
            for (int i = 0; i < lines.Count; i++)
382
            {
383
                Line line = lines[i];
384
                if (i == 0 || i + 1 != lines.Count)
385
                {
386
                    // 시작점에 연결된 Symbol 찾기
387
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
388
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
389
                    {
390
                        _LMSymbol1 = dataSource.GetSymbol(((Symbol)connItem).SPPID.RepresentationId);
391
                        if (_LMSymbol1 != null)
392
                            placeRunInputs.AddSymbolTarget(_LMSymbol1, line.SPPID.START_X, line.SPPID.START_Y);
393
                        else
394
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
395
                    }
396
                    else if (connItem != null && connItem.GetType() == typeof(Line))
397
                    {
398
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
399
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y);
400

    
401
                        if (targetConnector1 != null)
402
                            placeRunInputs.AddConnectorTarget(targetConnector1, line.SPPID.START_X, line.SPPID.START_Y);
403
                        else
404
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
405
                    }
406
                    else
407
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
408
                }
409

    
410
                if (i + 1 == lines.Count)
411
                {
412
                    // 끝점에 연결된 Symbol 찾기
413
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
414

    
415
                    if (i != 0)
416
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
417

    
418
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
419
                    {
420
                        _LMSymbol2 = dataSource.GetSymbol(((Symbol)connItem).SPPID.RepresentationId);
421
                        if (_LMSymbol2 != null)
422
                            placeRunInputs.AddSymbolTarget(_LMSymbol2, line.SPPID.END_X, line.SPPID.END_Y);
423
                        else
424
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
425
                            
426
                    }
427
                    else if (connItem != null && connItem.GetType() == typeof(Line))
428
                    {
429
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
430
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y);
431

    
432
                        if (targetConnector2 != null)
433
                            placeRunInputs.AddConnectorTarget(targetConnector2, line.SPPID.END_X, line.SPPID.END_Y);
434
                        else
435
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
436
                    }
437
                    else
438
                    {
439
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
440
                    }
441
                }
442
            }
443

    
444
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
445
            if (_lMConnector != null)
446
            {
447
                foreach (var line in lines)
448
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
449
                _lMConnector.Commit();
450
            }
451

    
452

    
453
            if (_LMSymbol1 != null)
454
                ReleaseCOMObjects(_LMSymbol1);
455
            if (_LMSymbol2 != null)
456
                ReleaseCOMObjects(_LMSymbol2);
457
            if (targetConnector1 != null)
458
                ReleaseCOMObjects(targetConnector1);
459
            if (targetConnector2 != null)
460
                ReleaseCOMObjects(targetConnector2);
461
            foreach (var item in connectorVertices1)
462
                ReleaseCOMObjects(item.Key);
463
            foreach (var item in connectorVertices2)
464
                ReleaseCOMObjects(item.Key);
465

    
466
            ReleaseCOMObjects(_lMConnector);
467
            ReleaseCOMObjects(placeRunInputs);
468
            ReleaseCOMObjects(_LMAItem);
469
        }
470

    
471
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
472
        {
473
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
474
            LMPipeRun _LMPipeRun = dataSource.GetPipeRun(modelId);
475
            if (_LMPipeRun != null)
476
            {
477
                foreach (LMRepresentation rep in _LMPipeRun.Representations)
478
                {
479
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
480
                    {
481
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
482
                        connectorVertices.Add(_LMConnector, new List<double[]>());
483
                        dynamic OID = rep.get_GraphicOID();
484
                        Ingr.RAD2D.DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
485
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
486
                        int verticesCount = lineStringGeometry.VertexCount;
487
                        double[] vertices = null;
488
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
489
                        for (int i = 0; i < verticesCount; i++)
490
                        {
491
                            double x = 0;
492
                            double y = 0;
493
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
494
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
495
                        }
496
                    }
497
                }
498

    
499
                ReleaseCOMObjects(_LMPipeRun);
500
            }
501

    
502
            return connectorVertices;
503
        }
504

    
505
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double x, double y)
506
        {
507
            double length = double.MaxValue;
508
            LMConnector targetConnector = null;
509
            foreach (var item in connectorVertices)
510
            {
511
                List<double[]> points = item.Value;
512
                for (int i = 0; i < points.Count - 1; i++)
513
                {
514
                    double[] point1 = points[i];
515
                    double[] point2 = points[i + 1];
516

    
517
                    double maxLineX = Math.Max(point1[0], point2[0]);
518
                    double minLineX = Math.Min(point1[0], point2[0]);
519
                    double maxLineY = Math.Max(point1[1], point2[1]);
520
                    double minLineY = Math.Min(point1[1], point2[1]);
521

    
522
                    // 두직선의 교차점으로 구하면 될듯 그리고 교차점이 x y랑 제일 가까운것
523

    
524
                    if ((minLineX <= x && maxLineX >= x) ||
525
                        (minLineY <= y && maxLineY >= y))
526
                    {
527
                        double result = SPPIDUtil.CalcLineToPointDistance(point1[0], point1[1], point2[0], point2[1], x, y);
528
                        if (length > result)
529
                        {
530
                            targetConnector = item.Key;
531
                            length = result;
532
                        }
533
                    }
534
                }
535
            }
536

    
537
            return targetConnector;
538
        }
539

    
540
        private void LineNumberModeling(LineNumber lineNumber)
541
        {
542

    
543
        }
544

    
545
        private void TextModeling(Text text)
546
        {
547

    
548
        }
549

    
550
        private void NoteModeling(Note note)
551
        {
552

    
553
        }
554

    
555

    
556
        public void ReleaseCOMObjects(params object[] objVars)
557
        {
558
            int intNewRefCount = 0;
559
            foreach (object obj in objVars)
560
            {
561
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
562
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
563
            }
564
        }
565
    }
566
}
클립보드 이미지 추가 (최대 크기: 500 MB)