프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / OPC / AutoModeling_OPC.cs @ 7b262903

이력 | 보기 | 이력해설 | 다운로드 (13.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 System.Data;
7
using Llama;
8
using 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.get_ItemStatus() == "Active" && _TO_OPC != null && _TO_OPC.get_ItemStatus() == "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 != "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.get_GraphicOID().ToString());
85

    
86
                                int mirror = removeSymbol.get_IsMirroredIndex();
87
                                double angle = Convert.ToDouble(removeSymbol.get_RotationAngle());
88
                                double x = removeSymbol.get_XCoordinate();
89
                                double y = removeSymbol.get_YCoordinate();
90
                                LMSymbol newOPC = null;
91
                                LMConnector connConnector = null;
92
                                int connConnectorIndex = 0;
93
                                foreach (LMConnector LMConnector in removeSymbol.Avoid1Connectors)
94
                                {
95
                                    if (LMConnector.get_ItemStatus() == "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.get_ItemStatus() == "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.get_FileName()) && !string.IsNullOrEmpty(representation.get_FileName()))
134
                                            {
135
                                                _symbolPath = representation.get_FileName();
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.set_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.get_GraphicOID().ToString();
226
                DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
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
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
255
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
256

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

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

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

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

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

    
303
            return result;
304
        }
305

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