프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / OPC / AutoModeling_OPC.cs @ 32da6a8a

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

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using System.Data;
7
using Ingr.SPPID.Llama;
8
using Ingr.SPPID.Plaice;
9
using Ingr.RAD2D.Interop.RAD2D;
10
using Ingr.RAD2D.Internal;
11
using Ingr.RAD2D.Helper;
12
using Converter.BaseModel;
13
using Converter.SPPID.Model;
14
using Converter.SPPID.Properties;
15
using Converter.SPPID.Util;
16
using Converter.SPPID.DB;
17
using Ingr.RAD2D.MacroControls.CmdCtrl;
18
using Ingr.RAD2D;
19
using System.Windows;
20
using System.Threading;
21
using System.Drawing;
22
using Microsoft.VisualBasic;
23
using Newtonsoft.Json;
24

    
25
using DevExpress.XtraSplashScreen;
26
namespace Converter.SPPID.OPC
27
{
28
    public class AutoModeling_OPC
29
    {
30
        dynamic application;
31
        dynamic drawing;
32
        Ingr.RAD2D.Application radApp;
33
        string fromOPCModelId;
34
        string toOPCModelId;
35
        string toDrawingName;
36
        List<string[]> toOPCAttributes;
37

    
38
        public AutoModeling_OPC(dynamic application, Ingr.RAD2D.Application radApp, string fromOPCModelId, string toOPCModelId, string toDrawingName, List<string[]> toOPCAttributes)
39
        {
40
            this.application = application;
41
            this.radApp = radApp;
42
            this.fromOPCModelId = fromOPCModelId;
43
            this.toOPCModelId = toOPCModelId;
44
            this.toDrawingName = toDrawingName;
45
            this.toOPCAttributes = toOPCAttributes;
46
        }
47

    
48
        public bool Run()
49
        {
50
            bool result = false;
51
            if (OpenDrawing(toDrawingName))
52
                result = PairedOPCModeling();
53
            CloseDrawing();
54

    
55
            return result;
56
        }
57

    
58
        public bool PairedOPCModeling()
59
        {
60
            Placement _placement = new Placement();
61
            LMADataSource dataSource = _placement.PIDDataSource;
62

    
63
            bool result = false;
64
            bool isPaired = false;
65
            LMOPC _FROM_OPC = dataSource.GetOPC(fromOPCModelId);
66
            LMOPC _TO_OPC = dataSource.GetOPC(toOPCModelId);
67
            if (_FROM_OPC != null && _FROM_OPC.ItemStatus.ToString() == "Active" && _TO_OPC != null && _TO_OPC.ItemStatus.ToString() == "Active")
68
            {
69
                string symbolPath = GetSPPIDFileName(_FROM_OPC);
70
                LMOPC pairOPC = _FROM_OPC.pairedWithOPCObject;
71

    
72
                foreach (LMRepresentation rep in pairOPC.Representations)
73
                    if (rep.DrawingID.ToString() != "0")
74
                        isPaired = true;
75
                try
76
                {
77
                    if (!isPaired)
78
                    {
79
                        foreach (LMRepresentation rep in _TO_OPC.Representations)
80
                        {
81
                            LMSymbol removeSymbol = dataSource.GetSymbol(rep.Id);
82
                            if (removeSymbol != null)
83
                            {
84
                                ZoomObjectByGraphicOID(removeSymbol.GraphicOID.ToString());
85

    
86
                                int mirror = removeSymbol.IsMirroredIndex;
87
                                double angle = Convert.ToDouble(removeSymbol.RotationAngle);
88
                                double x = Convert.ToDouble(removeSymbol.XCoordinate);
89
                                double y = Convert.ToDouble(removeSymbol.YCoordinate);
90
                                LMSymbol newOPC = null;
91
                                LMConnector connConnector = null;
92
                                int connConnectorIndex = 0;
93
                                foreach (LMConnector LMConnector in removeSymbol.Avoid1Connectors)
94
                                {
95
                                    if (LMConnector.ItemStatus.ToString() == "Active")
96
                                    {
97
                                        connConnector = LMConnector;
98
                                        if (!DBNull.Value.Equals(connConnector.ConnectItem1SymbolID) && connConnector.ConnectItem1SymbolID == removeSymbol.Id)
99
                                            connConnectorIndex = 1;
100
                                        else if (!DBNull.Value.Equals(connConnector.ConnectItem2SymbolID) && connConnector.ConnectItem2SymbolID == removeSymbol.Id)
101
                                            connConnectorIndex = 2;
102
                                        break;
103
                                    }
104
                                }
105

    
106
                                if (connConnector == null)
107
                                {
108
                                    foreach (LMConnector LMConnector in removeSymbol.Avoid2Connectors)
109
                                    {
110
                                        if (LMConnector.ItemStatus.ToString() == "Active")
111
                                        {
112
                                            connConnector = LMConnector;
113
                                            if (!DBNull.Value.Equals(connConnector.ConnectItem1SymbolID) && connConnector.ConnectItem1SymbolID == removeSymbol.Id)
114
                                                connConnectorIndex = 1;
115
                                            else if (!DBNull.Value.Equals(connConnector.ConnectItem2SymbolID) && connConnector.ConnectItem2SymbolID == removeSymbol.Id)
116
                                                connConnectorIndex = 2;
117
                                            break;
118
                                        }
119
                                    }
120
                                }
121

    
122
                                if (connConnector != null)
123
                                {
124
                                    _placement.PIDRemovePlacement(removeSymbol.AsLMRepresentation());
125
                                    ReleaseCOMObjects(removeSymbol);
126
                                    newOPC = _placement.PIDPlaceSymbol(symbolPath, x, y, Mirror: mirror, Rotation: angle, ExistingItem: pairOPC.AsLMAItem());
127
                                    if (newOPC != null)
128
                                    {
129
                                        LMModelItem modelItem = connConnector.ModelItemObject;
130
                                        string _symbolPath = null;
131
                                        foreach (LMRepresentation representation in modelItem.Representations)
132
                                        {
133
                                            if (!DBNull.Value.Equals(representation.FileName) && !string.IsNullOrEmpty(representation.FileName.ToString()))
134
                                            {
135
                                                _symbolPath = representation.FileName.ToString();
136
                                                break;
137
                                            }
138
                                        }
139

    
140
                                        if (!string.IsNullOrEmpty(_symbolPath))
141
                                        {
142
                                            LMAItem _LMAItem = _placement.PIDCreateItem(_symbolPath);
143
                                            List<double[]> vertices = GetConnectorVertices(connConnector);
144
                                            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
145
                                            double[] point = null;
146
                                            if (connConnectorIndex == 1)
147
                                                point = new double[] { vertices[0][0], vertices[0][1] };
148
                                            else
149
                                                point = new double[] { vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1] };
150
                                            placeRunInputs.AddSymbolTarget(newOPC, point[0], point[1]);
151
                                            placeRunInputs.AddConnectorTarget(connConnector, point[0], point[1]);
152
                                            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
153

    
154
                                            ReleaseCOMObjects(_LMAItem);
155
                                            _LMAItem = null;
156
                                            ReleaseCOMObjects(placeRunInputs);
157
                                            placeRunInputs = null;
158
                                            ReleaseCOMObjects(_lMConnector);
159
                                            _lMConnector = null;
160
                                        }
161

    
162
                                        ReleaseCOMObjects(modelItem);
163
                                        modelItem = null;
164
                                    }
165
                                }
166

    
167

    
168
                                if (newOPC != null)
169
                                {
170
                                    LMModelItem modelitem = newOPC.ModelItemObject;
171
                                    result = true;
172
                                    foreach (var attribute in toOPCAttributes)
173
                                    {
174
                                        LMAAttribute _attribute = modelitem.Attributes[attribute[0]];
175
                                        if (_attribute != null)
176
                                            _attribute.Value = attribute[1];
177
                                    }
178
                                    newOPC.Commit();
179
                                    modelitem.Commit();
180
                                    ReleaseCOMObjects(modelitem);
181
                                    modelitem = null;
182
                                }
183

    
184

    
185
                                ReleaseCOMObjects(connConnector);
186
                                connConnector = null;
187
                                ReleaseCOMObjects(newOPC);
188
                                newOPC = null;
189
                                ReleaseCOMObjects(removeSymbol);
190
                                removeSymbol = null;
191
                                break;
192
                            }
193
                            ReleaseCOMObjects(removeSymbol);
194
                            removeSymbol = null;
195
                        }
196
                    }
197
                }
198
                catch (Exception ex)
199
                {
200
                    Log.Write("error OPC");
201
                }
202
                finally
203
                {
204
                    ReleaseCOMObjects(_placement);
205
                    _placement = null;
206
                    ReleaseCOMObjects(dataSource);
207
                    dataSource = null;
208

    
209
                    ReleaseCOMObjects(_FROM_OPC);
210
                    _FROM_OPC = null;
211
                    ReleaseCOMObjects(_TO_OPC);
212
                    _TO_OPC = null;
213
                    ReleaseCOMObjects(pairOPC);
214
                    pairOPC = null;
215
                }
216
            }
217

    
218
            return result;
219
        }
220
        private List<double[]> GetConnectorVertices(LMConnector connector)
221
        {
222
            List<double[]> vertices = new List<double[]>();
223
            if (connector != null)
224
            {
225
                dynamic OID = connector.GraphicOID.ToString();
226
                Ingr.RAD2D.Components.Connector.Connector drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as Ingr.RAD2D.Components.Connector.Connector;
227
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
228
                int verticesCount = lineStringGeometry.VertexCount;
229
                double[] value = null;
230
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
231
                for (int i = 0; i < verticesCount; i++)
232
                {
233
                    double x = 0;
234
                    double y = 0;
235
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
236
                    vertices.Add(new double[] { x, y });
237
                }
238
            }
239
            return vertices;
240
        }
241
        /// <summary>
242
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
243
        /// </summary>
244
        /// <param name="graphicOID"></param>
245
        /// <param name="milliseconds"></param>
246
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
247
        {
248
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
249
            {
250
                double minX = 0;
251
                double minY = 0;
252
                double maxX = 0;
253
                double maxY = 0;
254
                ((GraphicObject)radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID]).Range(out minX, out minY, out maxX, out maxY);
255
                // TODO : ZOOM FIT
256
                //radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
257

    
258
                Thread.Sleep(milliseconds);
259
            }
260
        }
261

    
262
        private string GetSPPIDFileName(LMOPC opc)
263
        {
264
            string symbolPath = null;
265
            foreach (LMRepresentation rep in opc.Representations)
266
            {
267
                if (!DBNull.Value.Equals(rep.FileName) && !string.IsNullOrEmpty(rep.FileName.ToString()))
268
                {
269
                    symbolPath = rep.FileName.ToString();
270
                    break;
271
                }
272
            }
273
            return symbolPath;
274
        }
275

    
276
        public void ReleaseCOMObjects(params object[] objVars)
277
        {
278
            if (objVars != null)
279
            {
280
                int intNewRefCount = 0;
281
                foreach (object obj in objVars)
282
                {
283
                    if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
284
                        intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
285
                }
286
            }
287
        }
288

    
289
        private bool OpenDrawing(string drawingName)
290
        {
291
            bool result = false;
292
            try
293
            {
294
                drawing = application.Drawings.OpenDrawing(drawingName);
295
                drawing.Activate();
296

    
297
                result = true;
298
            }
299
            catch (Exception ex)
300
            {
301
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
302
            }
303

    
304
            return result;
305
        }
306

    
307
        private void CloseDrawing()
308
        {
309
            if (drawing != null)
310
            {
311
                drawing.CloseDrawing(true);
312
                ReleaseCOMObjects(drawing);
313
                drawing = null;
314
            }
315
        }
316
    }
317
}
클립보드 이미지 추가 (최대 크기: 500 MB)