프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / OPC / AutoModeling_OPC.cs @ b7a29053

이력 | 보기 | 이력해설 | 다운로드 (8.61 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
                                foreach (LMConnector LMConnector in removeSymbol.Avoid1Connectors)
92
                                {
93
                                    _placement.PIDRemovePlacement(removeSymbol.AsLMRepresentation());
94
                                    ReleaseCOMObjects(removeSymbol);
95
                                    newOPC = _placement.PIDPlaceSymbol(symbolPath, x, y, Mirror: mirror, Rotation: angle, ExistingItem: pairOPC.AsLMAItem(), TargetItem: LMConnector);
96
                                    break;
97
                                }
98

    
99
                                if (newOPC == null)
100
                                {
101
                                    foreach (LMConnector LMConnector in removeSymbol.Avoid2Connectors)
102
                                    {
103
                                        _placement.PIDRemovePlacement(removeSymbol.AsLMRepresentation());
104
                                        ReleaseCOMObjects(removeSymbol);
105
                                        newOPC = _placement.PIDPlaceSymbol(symbolPath, x, y, Mirror: mirror, Rotation: angle, ExistingItem: pairOPC.AsLMAItem(), TargetItem: LMConnector);
106
                                        break;
107
                                    }
108
                                }
109

    
110
                                if (newOPC != null)
111
                                {
112
                                    result = true;
113
                                    foreach (var attribute in toOPCAttributes)
114
                                    {
115
                                        LMAAttribute _attribute = newOPC.Attributes[attribute[0]];
116
                                        if (_attribute != null)
117
                                            _attribute.set_Value(attribute[1]);
118
                                    }
119
                                    newOPC.Commit();
120
                                }
121

    
122
                                ReleaseCOMObjects(newOPC);
123
                                newOPC = null;
124
                                ReleaseCOMObjects(removeSymbol);
125
                                removeSymbol = null;
126
                                break;
127
                            }
128
                            ReleaseCOMObjects(removeSymbol);
129
                            removeSymbol = null;
130
                        }
131
                    }
132
                }
133
                catch (Exception ex)
134
                {
135
                    Log.Write("error OPC");
136
                }
137
                finally
138
                {
139
                    ReleaseCOMObjects(_placement);
140
                    _placement = null;
141
                    ReleaseCOMObjects(dataSource);
142
                    dataSource = null;
143

    
144
                    ReleaseCOMObjects(_FROM_OPC);
145
                    _FROM_OPC = null;
146
                    ReleaseCOMObjects(_TO_OPC);
147
                    _TO_OPC = null;
148
                    ReleaseCOMObjects(pairOPC);
149
                    pairOPC = null;
150
                }
151
            }
152

    
153
            return result;
154
        }
155

    
156
        /// <summary>
157
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
158
        /// </summary>
159
        /// <param name="graphicOID"></param>
160
        /// <param name="milliseconds"></param>
161
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
162
        {
163
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
164
            {
165
                double minX = 0;
166
                double minY = 0;
167
                double maxX = 0;
168
                double maxY = 0;
169
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
170
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
171

    
172
                Thread.Sleep(milliseconds);
173
            }
174
        }
175

    
176
        private string GetSPPIDFileName(LMOPC opc)
177
        {
178
            string symbolPath = null;
179
            foreach (LMRepresentation rep in opc.Representations)
180
            {
181
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
182
                {
183
                    symbolPath = rep.get_FileName();
184
                    break;
185
                }
186
            }
187
            return symbolPath;
188
        }
189

    
190
        public void ReleaseCOMObjects(params object[] objVars)
191
        {
192
            if (objVars != null)
193
            {
194
                int intNewRefCount = 0;
195
                foreach (object obj in objVars)
196
                {
197
                    if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
198
                        intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
199
                }
200
            }
201
        }
202

    
203
        private bool OpenDrawing(string drawingName)
204
        {
205
            bool result = false;
206
            try
207
            {
208
                drawing = application.Drawings.OpenDrawing(drawingName);
209
                drawing.Activate();
210

    
211
                result = true;
212
            }
213
            catch (Exception ex)
214
            {
215
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
216
            }
217

    
218
            return result;
219
        }
220

    
221
        private void CloseDrawing()
222
        {
223
            if (drawing != null)
224
            {
225
                drawing.CloseDrawing(true);
226
                ReleaseCOMObjects(drawing);
227
                drawing = null;
228
            }
229
        }
230
    }
231
}
클립보드 이미지 추가 (최대 크기: 500 MB)