hytos / DTI_PID / SPPIDConverter / OPC / AutoModeling_OPC.cs @ 30a35908
이력 | 보기 | 이력해설 | 다운로드 (13.9 KB)
1 | 69b7387a | gaqhf | 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 | 4d4dce52 | esham21 | using Llama; |
8 | using Plaice; |
||
9 | 69b7387a | gaqhf | 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 | 3d7ff99c | gaqhf | namespace Converter.SPPID.OPC |
27 | 69b7387a | gaqhf | { |
28 | public class AutoModeling_OPC |
||
29 | { |
||
30 | dynamic application; |
||
31 | 154d8f43 | gaqhf | dynamic drawing; |
32 | 69b7387a | gaqhf | Ingr.RAD2D.Application radApp; |
33 | 154d8f43 | gaqhf | string fromOPCModelId; |
34 | string toOPCModelId; |
||
35 | string toDrawingName; |
||
36 | b7a29053 | gaqhf | List<string[]> toOPCAttributes; |
37 | 69b7387a | gaqhf | |
38 | b7a29053 | gaqhf | public AutoModeling_OPC(dynamic application, Ingr.RAD2D.Application radApp, string fromOPCModelId, string toOPCModelId, string toDrawingName, List<string[]> toOPCAttributes) |
39 | 69b7387a | gaqhf | { |
40 | this.application = application; |
||
41 | this.radApp = radApp; |
||
42 | 154d8f43 | gaqhf | this.fromOPCModelId = fromOPCModelId; |
43 | this.toOPCModelId = toOPCModelId; |
||
44 | this.toDrawingName = toDrawingName; |
||
45 | b7a29053 | gaqhf | this.toOPCAttributes = toOPCAttributes; |
46 | 69b7387a | gaqhf | } |
47 | |||
48 | 154d8f43 | gaqhf | public bool Run() |
49 | 69b7387a | gaqhf | { |
50 | 154d8f43 | gaqhf | bool result = false; |
51 | if (OpenDrawing(toDrawingName)) |
||
52 | result = PairedOPCModeling(); |
||
53 | CloseDrawing(); |
||
54 | 69b7387a | gaqhf | |
55 | 154d8f43 | gaqhf | return result; |
56 | 1ed39474 | gaqhf | } |
57 | 69b7387a | gaqhf | |
58 | 154d8f43 | gaqhf | public bool PairedOPCModeling() |
59 | 1ed39474 | gaqhf | { |
60 | 154d8f43 | gaqhf | Placement _placement = new Placement(); |
61 | LMADataSource dataSource = _placement.PIDDataSource; |
||
62 | 224535bb | gaqhf | |
63 | 154d8f43 | gaqhf | bool result = false; |
64 | bool isPaired = false; |
||
65 | LMOPC _FROM_OPC = dataSource.GetOPC(fromOPCModelId); |
||
66 | LMOPC _TO_OPC = dataSource.GetOPC(toOPCModelId); |
||
67 | 4d4dce52 | esham21 | if (_FROM_OPC != null && _FROM_OPC.get_ItemStatus() == "Active" && _TO_OPC != null && _TO_OPC.get_ItemStatus() == "Active") |
68 | 1ed39474 | gaqhf | { |
69 | 154d8f43 | gaqhf | string symbolPath = GetSPPIDFileName(_FROM_OPC); |
70 | LMOPC pairOPC = _FROM_OPC.pairedWithOPCObject; |
||
71 | 224535bb | gaqhf | |
72 | 154d8f43 | gaqhf | foreach (LMRepresentation rep in pairOPC.Representations) |
73 | 4d4dce52 | esham21 | if (rep.DrawingID != "0") |
74 | 154d8f43 | gaqhf | isPaired = true; |
75 | try |
||
76 | 71ba1ca3 | gaqhf | { |
77 | 154d8f43 | gaqhf | if (!isPaired) |
78 | 88bac50c | gaqhf | { |
79 | 154d8f43 | gaqhf | foreach (LMRepresentation rep in _TO_OPC.Representations) |
80 | 88bac50c | gaqhf | { |
81 | 154d8f43 | gaqhf | LMSymbol removeSymbol = dataSource.GetSymbol(rep.Id); |
82 | if (removeSymbol != null) |
||
83 | 88bac50c | gaqhf | { |
84 | 4d4dce52 | esham21 | ZoomObjectByGraphicOID(removeSymbol.get_GraphicOID().ToString()); |
85 | 88bac50c | gaqhf | |
86 | 4d4dce52 | esham21 | 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 | 154d8f43 | gaqhf | LMSymbol newOPC = null; |
91 | 58993ba9 | gaqhf | LMConnector connConnector = null; |
92 | int connConnectorIndex = 0; |
||
93 | 154d8f43 | gaqhf | foreach (LMConnector LMConnector in removeSymbol.Avoid1Connectors) |
94 | 88bac50c | gaqhf | { |
95 | 4d4dce52 | esham21 | if (LMConnector.get_ItemStatus() == "Active") |
96 | 58993ba9 | gaqhf | { |
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 | 88bac50c | gaqhf | } |
105 | 154d8f43 | gaqhf | |
106 | 58993ba9 | gaqhf | if (connConnector == null) |
107 | 154d8f43 | gaqhf | { |
108 | foreach (LMConnector LMConnector in removeSymbol.Avoid2Connectors) |
||
109 | { |
||
110 | 4d4dce52 | esham21 | if (LMConnector.get_ItemStatus() == "Active") |
111 | 58993ba9 | gaqhf | { |
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 | 154d8f43 | gaqhf | } |
120 | } |
||
121 | |||
122 | 58993ba9 | gaqhf | 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 | 4d4dce52 | esham21 | if (!DBNull.Value.Equals(representation.get_FileName()) && !string.IsNullOrEmpty(representation.get_FileName())) |
134 | 58993ba9 | gaqhf | { |
135 | 4d4dce52 | esham21 | _symbolPath = representation.get_FileName(); |
136 | 58993ba9 | gaqhf | break; |
137 | } |
||
138 | } |
||
139 | |||
140 | if (!string.IsNullOrEmpty(_symbolPath)) |
||
141 | { |
||
142 | 4d4dce52 | esham21 | _LMAItem _LMAItem = _placement.PIDCreateItem(_symbolPath); |
143 | 58993ba9 | gaqhf | 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 | 154d8f43 | gaqhf | if (newOPC != null) |
169 | b7a29053 | gaqhf | { |
170 | 58993ba9 | gaqhf | LMModelItem modelitem = newOPC.ModelItemObject; |
171 | 154d8f43 | gaqhf | result = true; |
172 | b7a29053 | gaqhf | foreach (var attribute in toOPCAttributes) |
173 | { |
||
174 | 58993ba9 | gaqhf | LMAAttribute _attribute = modelitem.Attributes[attribute[0]]; |
175 | b7a29053 | gaqhf | if (_attribute != null) |
176 | 4d4dce52 | esham21 | _attribute.set_Value(attribute[1]); |
177 | b7a29053 | gaqhf | } |
178 | newOPC.Commit(); |
||
179 | 58993ba9 | gaqhf | modelitem.Commit(); |
180 | ReleaseCOMObjects(modelitem); |
||
181 | modelitem = null; |
||
182 | b7a29053 | gaqhf | } |
183 | 154d8f43 | gaqhf | |
184 | 58993ba9 | gaqhf | |
185 | ReleaseCOMObjects(connConnector); |
||
186 | connConnector = null; |
||
187 | 154d8f43 | gaqhf | ReleaseCOMObjects(newOPC); |
188 | newOPC = null; |
||
189 | ReleaseCOMObjects(removeSymbol); |
||
190 | removeSymbol = null; |
||
191 | break; |
||
192 | 88bac50c | gaqhf | } |
193 | 154d8f43 | gaqhf | ReleaseCOMObjects(removeSymbol); |
194 | removeSymbol = null; |
||
195 | 88bac50c | gaqhf | } |
196 | } |
||
197 | 71ba1ca3 | gaqhf | } |
198 | 154d8f43 | gaqhf | 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 | 71ba1ca3 | gaqhf | } |
217 | 224535bb | gaqhf | |
218 | 154d8f43 | gaqhf | return result; |
219 | } |
||
220 | 58993ba9 | gaqhf | private List<double[]> GetConnectorVertices(LMConnector connector) |
221 | { |
||
222 | List<double[]> vertices = new List<double[]>(); |
||
223 | if (connector != null) |
||
224 | { |
||
225 | 4d4dce52 | esham21 | dynamic OID = connector.get_GraphicOID().ToString(); |
226 | DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID]; |
||
227 | 58993ba9 | gaqhf | 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 | 154d8f43 | gaqhf | /// <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 | 4d4dce52 | esham21 | 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 | 154d8f43 | gaqhf | |
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 | 4d4dce52 | esham21 | if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName())) |
267 | 154d8f43 | gaqhf | { |
268 | 4d4dce52 | esham21 | symbolPath = rep.get_FileName(); |
269 | 154d8f43 | gaqhf | break; |
270 | } |
||
271 | } |
||
272 | return symbolPath; |
||
273 | 224535bb | gaqhf | } |
274 | |||
275 | public void ReleaseCOMObjects(params object[] objVars) |
||
276 | { |
||
277 | 154d8f43 | gaqhf | if (objVars != null) |
278 | 224535bb | gaqhf | { |
279 | 154d8f43 | gaqhf | 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 | 224535bb | gaqhf | } |
286 | 69b7387a | gaqhf | } |
287 | 1ed39474 | gaqhf | |
288 | 71ba1ca3 | gaqhf | private bool OpenDrawing(string drawingName) |
289 | 1ed39474 | gaqhf | { |
290 | 71ba1ca3 | gaqhf | bool result = false; |
291 | try |
||
292 | { |
||
293 | 154d8f43 | gaqhf | drawing = application.Drawings.OpenDrawing(drawingName); |
294 | drawing.Activate(); |
||
295 | 71ba1ca3 | gaqhf | |
296 | result = true; |
||
297 | } |
||
298 | catch (Exception ex) |
||
299 | { |
||
300 | System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); |
||
301 | } |
||
302 | 1ed39474 | gaqhf | |
303 | 71ba1ca3 | gaqhf | return result; |
304 | 1ed39474 | gaqhf | } |
305 | 154d8f43 | gaqhf | |
306 | private void CloseDrawing() |
||
307 | { |
||
308 | if (drawing != null) |
||
309 | { |
||
310 | drawing.CloseDrawing(true); |
||
311 | ReleaseCOMObjects(drawing); |
||
312 | drawing = null; |
||
313 | } |
||
314 | } |
||
315 | 69b7387a | gaqhf | } |
316 | } |