hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ 70b0b5d7
이력 | 보기 | 이력해설 | 다운로드 (16.9 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.ComponentModel; |
4 |
using System.Drawing; |
5 |
using System.Data; |
6 |
using System.Linq; |
7 |
using System.Text; |
8 |
using System.Threading.Tasks; |
9 |
using System.Windows.Forms; |
10 |
using System.Threading; |
11 |
using System.IO; |
12 |
using Microsoft.VisualBasic; |
13 |
using Ingr.RAD2D; |
14 |
using Converter.BaseModel; |
15 |
using Converter.SPPID.Properties; |
16 |
using Converter.SPPID.DB; |
17 |
using Converter.SPPID.Util; |
18 |
using Converter.SPPID.Form; |
19 |
using Converter.SPPID.Model; |
20 |
using Plaice; |
21 |
using Llama; |
22 |
using DevExpress.XtraSplashScreen; |
23 |
using Newtonsoft.Json; |
24 |
using System.Runtime.InteropServices; |
25 |
using System.Reflection; |
26 |
using Converter.SPPID.OPC; |
27 |
|
28 |
namespace Converter.SPPID.Wrapper |
29 |
{ |
30 |
public partial class ConverterDocking : UserControl |
31 |
{ |
32 |
Ingr.RAD2D.Application application; |
33 |
static bool addEvent = false; |
34 |
public ConverterDocking() |
35 |
{ |
36 |
InitializeComponent(); |
37 |
|
38 |
dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application"); |
39 |
WrapperApplication wApp = new WrapperApplication(dApplication.Application); |
40 |
application = wApp.RADApplication; |
41 |
|
42 |
ReleaseCOMObjects(dApplication); |
43 |
dApplication = null; |
44 |
try |
45 |
{ |
46 |
textEditDrawingX.EditValue = Settings.Default.DrawingX; |
47 |
textEditDrawingY.EditValue = Settings.Default.DrawingY; |
48 |
|
49 |
Project_Info _ProjectInfo = Project_Info.GetInstance(); |
50 |
_ProjectInfo.DefaultPath = Settings.Default.ProjectPath; |
51 |
_ProjectInfo.DBType = (ID2DB_Type)Settings.Default.ProjectDBType; |
52 |
_ProjectInfo.ServerIP = Settings.Default.ProjectServerIP; |
53 |
_ProjectInfo.Port = Settings.Default.ProjectPort; |
54 |
_ProjectInfo.DBUser = Settings.Default.ProjectDBUser; |
55 |
_ProjectInfo.DBPassword = Settings.Default.ProjectDBPassword; |
56 |
|
57 |
if (!addEvent) |
58 |
{ |
59 |
application.EventObject.BeforeApplicationExit += ApplicationEvents_ApplicationExit; |
60 |
RegisterHotKey(this.Handle, 0, (int)KeyModifier.Shift, Keys.A.GetHashCode()); |
61 |
addEvent = true; |
62 |
} |
63 |
} |
64 |
catch (Exception ex) |
65 |
{ |
66 |
StringBuilder sb = new StringBuilder(); |
67 |
sb.AppendLine(ex.Message); |
68 |
sb.AppendLine(ex.StackTrace); |
69 |
MessageBox.Show(sb.ToString()); |
70 |
} |
71 |
} |
72 |
|
73 |
/// <summary> |
74 |
/// 선택한 도면들을 읽어서 SPPID로 변환한다. |
75 |
/// </summary> |
76 |
/// <param name="sender"></param> |
77 |
/// <param name="e"></param> |
78 |
private void btnConverter_Click(object sender, EventArgs e) |
79 |
{ |
80 |
ConverterForm converterForm = new ConverterForm(); |
81 |
if (converterForm.ShowDialog() == DialogResult.OK) |
82 |
{ |
83 |
//Ingr.RAD2D.Document doc = application.Documents.Add(); |
84 |
|
85 |
try |
86 |
{ |
87 |
CloseOPCForm.Run(); |
88 |
|
89 |
for (int i = 0; i < converterForm.Documents.Count; i++) |
90 |
{ |
91 |
SPPID_Document document = converterForm.Documents[i]; |
92 |
if (document.SetSPPIDMapping() && document.Enable) |
93 |
{ |
94 |
using (AutoModeling modeling = new AutoModeling(document, converterForm.checkEditCloseDocument.Checked)) |
95 |
{ |
96 |
modeling.DocumentLabelText = string.Format("Drawing Name : {0} ({1}/{2})", document.DrawingName, i + 1, converterForm.Documents.Count); |
97 |
modeling.Run(); |
98 |
} |
99 |
} |
100 |
} |
101 |
} |
102 |
catch (Exception ex) |
103 |
{ |
104 |
MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); |
105 |
} |
106 |
finally |
107 |
{ |
108 |
CloseOPCForm.Stop(); |
109 |
|
110 |
//doc.SaveOnClose = false; |
111 |
//doc.Close(false); |
112 |
|
113 |
//dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application"); |
114 |
//dApplication.Drawings[1].CloseDrawing(false); |
115 |
//ReleaseCOMObjects(dApplication); |
116 |
//dApplication = null; |
117 |
} |
118 |
|
119 |
MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information); |
120 |
} |
121 |
} |
122 |
|
123 |
private void btnLinkOPC_Click(object sender, EventArgs e) |
124 |
{ |
125 |
DataTable tOPCInfo = Project_DB.SelectOPCInfo(); |
126 |
DataTable tOPCRelations = Project_DB.SelectOPCRelations(); |
127 |
DataTable tDrawingInfo = Project_DB.SelectDrawingInfo(); |
128 |
dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application"); |
129 |
|
130 |
foreach (DataRow row in tOPCInfo.Rows) |
131 |
{ |
132 |
if (!Convert.ToBoolean(row["PAIRED"])) |
133 |
{ |
134 |
string drawingUID = row["ID2_DRAWING_UID"].ToString(); |
135 |
string OPCUID = row["ID2_OPC_UID"].ToString(); |
136 |
DataRow[] rows = tOPCRelations.Select(string.Format("(From_Drawings_UID = '{0}' AND From_OPC_UID = '{1}') OR (To_Drawings_UID = '{0}' AND To_OPC_UID = '{1}')", drawingUID, OPCUID)); |
137 |
|
138 |
if (rows.Length == 2) |
139 |
{ |
140 |
string fromDrawingsUID1 = rows[0]["From_Drawings_UID"].ToString(); |
141 |
string fromDrawingsUID2 = rows[1]["From_Drawings_UID"].ToString(); |
142 |
string fromOPCUID1 = rows[0]["From_OPC_UID"].ToString(); |
143 |
string fromOPCUID2 = rows[1]["From_OPC_UID"].ToString(); |
144 |
string toDrawingsUID1 = rows[0]["To_Drawings_UID"].ToString(); |
145 |
string toDrawingsUID2 = rows[1]["To_Drawings_UID"].ToString(); |
146 |
string toOPCUID1 = rows[0]["To_OPC_UID"].ToString(); |
147 |
string toOPCUID2 = rows[1]["To_OPC_UID"].ToString(); |
148 |
|
149 |
DataRow[] fromDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", fromDrawingsUID1)); |
150 |
DataRow[] toDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", toDrawingsUID1)); |
151 |
DataRow[] fromOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", fromOPCUID1)); |
152 |
DataRow[] toOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", toOPCUID1)); |
153 |
|
154 |
if (fromOPCUID1 == toOPCUID2 && fromOPCUID2 == toOPCUID1 && fromDrawing.Length == 1 && toDrawing.Length == 1 && fromOPCInfoRows.Length == 1 && toOPCInfoRows.Length == 1) |
155 |
{ |
156 |
DataRow fromOPCInfoRow = fromOPCInfoRows[0]; |
157 |
DataRow toOPCInfoRow = toOPCInfoRows[0]; |
158 |
string fromOPCModelId = fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(); |
159 |
string toOPCModelId = toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(); |
160 |
string toDrawingName = toDrawing[0]["DRAWINGNAME"].ToString(); |
161 |
List<string[]> toOPCAttributes = JsonConvert.DeserializeObject<List<string[]>>(toOPCInfoRow["ATTRIBUTES"].ToString()); |
162 |
AutoModeling_OPC opc = new AutoModeling_OPC(dApplication, application, fromOPCModelId, toOPCModelId, toDrawingName, toOPCAttributes); |
163 |
if (opc.Run()) |
164 |
{ |
165 |
fromOPCInfoRow["PAIRED"] = true; |
166 |
toOPCInfoRow["PAIRED"] = true; |
167 |
|
168 |
Project_DB.InsertOPCInfo(fromOPCInfoRow["ID2_OPC_UID"].ToString(), fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), fromOPCInfoRow["ID2_DRAWING_UID"].ToString(), true); |
169 |
Project_DB.InsertOPCInfo(toOPCInfoRow["ID2_OPC_UID"].ToString(), toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), toOPCInfoRow["ID2_DRAWING_UID"].ToString(), true); |
170 |
} |
171 |
} |
172 |
} |
173 |
} |
174 |
} |
175 |
|
176 |
tOPCInfo.Dispose(); |
177 |
tOPCRelations.Dispose(); |
178 |
tDrawingInfo.Dispose(); |
179 |
ReleaseCOMObjects(dApplication); |
180 |
dApplication = null; |
181 |
|
182 |
MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information); |
183 |
} |
184 |
|
185 |
public void ReleaseCOMObjects(params object[] objVars) |
186 |
{ |
187 |
int intNewRefCount = 0; |
188 |
foreach (object obj in objVars) |
189 |
{ |
190 |
if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj)) |
191 |
intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj); |
192 |
} |
193 |
} |
194 |
|
195 |
private void btnGetDrawingSize_Click(object sender, EventArgs e) |
196 |
{ |
197 |
dynamic application = Interaction.GetObject("", "PIDAutomation.Application"); |
198 |
WrapperApplication wApp = new WrapperApplication(application.Application); |
199 |
Ingr.RAD2D.Application radApp = wApp.RADApplication; |
200 |
|
201 |
if (radApp.ActiveSelectSet.Count > 0) |
202 |
{ |
203 |
DependencyObject line2D = radApp.ActiveSelectSet[0] as DependencyObject; |
204 |
if (line2D != null) |
205 |
{ |
206 |
double minX = 0; |
207 |
double minY = 0; |
208 |
double maxX = 0; |
209 |
double maxY = 0; |
210 |
line2D.Range(out minX, out minY, out maxX, out maxY); |
211 |
|
212 |
Settings.Default.DrawingX = maxX - minX; |
213 |
Settings.Default.DrawingY = maxY - minY; |
214 |
Settings.Default.Save(); |
215 |
|
216 |
textEditDrawingX.EditValue = Settings.Default.DrawingX; |
217 |
textEditDrawingY.EditValue = Settings.Default.DrawingY; |
218 |
} |
219 |
else |
220 |
MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning); |
221 |
} |
222 |
else |
223 |
MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning); |
224 |
} |
225 |
|
226 |
#region TEST |
227 |
[System.Runtime.InteropServices.DllImport("user32.dll")] |
228 |
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk); |
229 |
|
230 |
[System.Runtime.InteropServices.DllImport("user32.dll")] |
231 |
private static extern bool UnregisterHotKey(IntPtr hWnd, int id); |
232 |
|
233 |
enum KeyModifier |
234 |
{ |
235 |
None = 0, |
236 |
Alt = 1, |
237 |
Control = 2, |
238 |
Shift = 4, |
239 |
WinKey = 8 |
240 |
} |
241 |
|
242 |
protected override void WndProc(ref Message m) |
243 |
{ |
244 |
base.WndProc(ref m); |
245 |
if (m.Msg == 0x0312) |
246 |
{ |
247 |
Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); |
248 |
KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF); |
249 |
int id = m.WParam.ToInt32(); |
250 |
application.RunCommand(CommandConstants.igcmdGridSnap); |
251 |
} |
252 |
|
253 |
} |
254 |
|
255 |
private void ApplicationEvents_ApplicationExit(out bool cancel) |
256 |
{ |
257 |
UnregisterHotKey(this.Handle, 0); |
258 |
cancel = false; |
259 |
} |
260 |
|
261 |
private void simpleButton1_Click(object sender, EventArgs e) |
262 |
{ |
263 |
|
264 |
return; |
265 |
|
266 |
Placement placement = new Placement(); |
267 |
if (application.ActiveSelectSet.Count > 0) |
268 |
{ |
269 |
string modelItemId = null; |
270 |
Symbol2d symbol2D = application.ActiveSelectSet[0] as Symbol2d; |
271 |
DependencyObject dependency = application.ActiveSelectSet[0] as DependencyObject; |
272 |
application.ActiveSelectSet.RemoveAll(); |
273 |
if (symbol2D != null) |
274 |
{ |
275 |
return; |
276 |
foreach (var attributes in symbol2D.AttributeSets) |
277 |
{ |
278 |
foreach (var attribute in attributes) |
279 |
{ |
280 |
if (attribute.Name == "ModelID") |
281 |
modelItemId = attribute.GetValue().ToString(); |
282 |
} |
283 |
} |
284 |
|
285 |
if (modelItemId!=null) |
286 |
{ |
287 |
LMADataSource dataSource = new LMADataSource(); |
288 |
LMModelItem modelItem = dataSource.GetModelItem(modelItemId); |
289 |
foreach (LMRepresentation item in modelItem.Representations) |
290 |
{ |
291 |
if (item.ModelItemID == modelItemId) |
292 |
{ |
293 |
dataSource = placement.PIDDataSource; |
294 |
LMSymbol symbol = dataSource.GetSymbol(item.Id); |
295 |
double oldx = symbol.get_XCoordinate(); |
296 |
double oldy = symbol.get_YCoordinate(); |
297 |
symbol2D.Move(0, 0, 0, GridSetting.GetInstance().Length); |
298 |
|
299 |
dataSource = new LMADataSource(); |
300 |
//dataSource = placement.PIDDataSource; |
301 |
symbol = dataSource.GetSymbol(item.Id); |
302 |
double x = symbol.get_XCoordinate(); |
303 |
double y = symbol.get_YCoordinate(); |
304 |
} |
305 |
} |
306 |
} |
307 |
} |
308 |
else if (dependency != null) |
309 |
{ |
310 |
foreach (dynamic item in dependency.DrawingObjects) |
311 |
{ |
312 |
item.Move(0, 0, 0, GridSetting.GetInstance().Length); |
313 |
} |
314 |
} |
315 |
} |
316 |
|
317 |
return; |
318 |
Thread outThread = new Thread(func2 => |
319 |
{ |
320 |
for (int i = 100; i < 120; i++) |
321 |
{ |
322 |
Thread thread = new Thread(func => |
323 |
{ |
324 |
dynamic application = Interaction.GetObject("", "PIDAutomation.Application"); |
325 |
//WrapperApplication wApp = new WrapperApplication(application.Application); |
326 |
//Ingr.RAD2D.Application radApp = wApp.RADApplication; |
327 |
dynamic drawing = application.Drawings.Add("Unit1", "D-Size.pid", i.ToString(), i.ToString());//application.Drawings.OpenDrawing("123"); |
328 |
|
329 |
Thread.Sleep(5000); |
330 |
|
331 |
drawing.CloseDrawing(true); |
332 |
|
333 |
//application.Quit(); |
334 |
ReleaseCOMObjects(application); |
335 |
application = null; |
336 |
ReleaseCOMObjects(drawing); |
337 |
drawing = null; |
338 |
Thread.Sleep(5000); |
339 |
}); |
340 |
thread.Start(); |
341 |
thread.Join(); |
342 |
} |
343 |
}); |
344 |
outThread.Start(); |
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
//radApp.Documents.Add(@"\\server70\DEVDOF1Site\GS\PLANT\PLANT1\Unit1\11111.pid"); |
351 |
|
352 |
//radApp.ActiveDocument.Close(true); |
353 |
//drawing.CloseDrawing(true); |
354 |
|
355 |
//ReleaseCOMObjects(application); |
356 |
//ReleaseCOMObjects(drawing); |
357 |
//application = null; |
358 |
//wApp = null; |
359 |
//radApp = null; |
360 |
//drawing = null; |
361 |
} |
362 |
|
363 |
private void AutoJoinPipeRun() |
364 |
{ |
365 |
dynamic application = Interaction.GetObject("", "PIDAutomation.Application"); |
366 |
WrapperApplication wApp = new WrapperApplication(application.Application); |
367 |
Ingr.RAD2D.Application radApp = wApp.RADApplication; |
368 |
|
369 |
string modelItemId = null; |
370 |
List<double[]> vertices = new List<double[]>(); |
371 |
if (radApp.ActiveSelectSet.Count == 0) |
372 |
{ |
373 |
return; |
374 |
} |
375 |
dynamic OID = radApp.ActiveSelectSet[0].Key(); |
376 |
DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID]; |
377 |
foreach (var attributes in drawingObject.AttributeSets) |
378 |
{ |
379 |
foreach (var attribute in attributes) |
380 |
{ |
381 |
if (attribute.Name == "ModelID") |
382 |
modelItemId = attribute.GetValue().ToString(); |
383 |
} |
384 |
} |
385 |
radApp.ActiveSelectSet.RemoveAll(); |
386 |
|
387 |
} |
388 |
#endregion |
389 |
|
390 |
|
391 |
[DllImport("user32.dll")] |
392 |
public static extern int FindWindow(string lpClassName, string lpWindowName); |
393 |
|
394 |
[DllImport("user32.dll", SetLastError = true)] |
395 |
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId); |
396 |
|
397 |
private void ConverterDocking_Load(object sender, EventArgs e) |
398 |
{ |
399 |
#if DEBUG |
400 |
this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always; |
401 |
#else |
402 |
this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never; |
403 |
#endif |
404 |
|
405 |
} |
406 |
} |
407 |
} |