-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
508 lines (442 loc) · 23.9 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SolidWorks.Interop.cosworks;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System.Collections;
using System.Threading;
using Excel = Microsoft.Office.Interop.Excel;
using System.Timers;
namespace W_Form_analyse_get_TrainingDaten
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private void Window_Closed(object sender, EventArgs e)
{
try
{
swApp.ExitApp();
}
catch (Exception)
{
}
}
#region Variable definition
SldWorks swApp;
int m;//how many cells
int Count; // how many data
double width, thickness, l1, l2, l3, F1, F2, F3;
double l4, l1_, l2_, l3_, l4_, cell_length, cell_height, x_o, y_o, x_o_, y_o_;
const double MeshEleSize = 2.0;
const double MeshTol = 0.1;
string strMaterialLib = null;
object[] Disp = null;
object[] Stress = null;
ModelDoc2 swModel = null;
SelectionMgr selectionMgr = null;
CosmosWorks COSMOSWORKS = null;
CwAddincallback COSMOSObject = default(CwAddincallback);
CWModelDoc ActDoc = default(CWModelDoc);
CWStudyManager StudyMngr = default(CWStudyManager);
CWStudy Study = default(CWStudy);
CWSolidManager SolidMgr = default(CWSolidManager);
CWSolidBody SolidBody = default(CWSolidBody);
CWSolidComponent SolidComp = default(CWSolidComponent);
CWForce cwForce = default(CWForce);
CWLoadsAndRestraintsManager LBCMgr = default(CWLoadsAndRestraintsManager);
CWResults CWFeatobj = default(CWResults);
bool isSelected;
float maxDisp = 0.0f;
float maxStress = 0.0f;
int intStatus = 0;
int errors = 0;
int errCode = 0;
int warnings = 0;
/* change to double
int ran_m, ran_width, ran_thickness, ran_l1, ran_l2, ran_l3, ran_F1, ran_F2, ran_F3;
*/
int ran_m;
double ran_width, ran_thickness, ran_l1, ran_l2, ran_l3, ran_F1, ran_F2, ran_F3;
Excel.Application exlApp;
Excel.Workbook exlBook;
Excel.Worksheet exlSheet;
#endregion
public MainWindow()
{
InitializeComponent();
}
private void button_create_Geom_Click(object sender, RoutedEventArgs e)
{
#region a test example, not important
m = 9;
width = 8 / 1000.0;
thickness = 1 / 1000.0;
l1 = 30 / 1000.0;
l2 = 16 / 1000.0;
l3 = 21 / 1000.0;
F1 = 1;
F2 = 37;
F3 = 49;
l4 = l2;
l1_ = l1 - 2 * width;
l2_ = l2;
l3_ = l3 + 2 * width;
l4_ = l4;
cell_length = l1 + l3;
cell_height = l2 + width;
#endregion
Count = int.Parse(textBox_count.Text);
Random randomSeed = new Random();
Random random_m = new Random(randomSeed.Next());
Random random_width = new Random(randomSeed.Next());
Random random_thickness = new Random(randomSeed.Next());
Random random_l1 = new Random(randomSeed.Next());
Random random_l2 = new Random(randomSeed.Next());
Random random_l3 = new Random(randomSeed.Next());
Random random_F1 = new Random(randomSeed.Next());
Random random_F2 = new Random(randomSeed.Next());
Random random_F3 = new Random(randomSeed.Next());
for (int j = 0; j < Count; j++)
{
Console.WriteLine(string.Format("---------------------------------now No.{0}", j));
#region generate random Data
try
{
/* change to double
ran_m = random_m.Next(int.Parse(textBox_hm_cells_min.Text), int.Parse(textBox_hm_cellls_max.Text));
ran_width = random_width.Next(int.Parse(textBox_width_min.Text), int.Parse(textBox_width_max.Text));
ran_thickness = random_thickness.Next(int.Parse(textBox_thickness_min.Text), int.Parse(textBox_thickness_max.Text));
ran_l1 = random_l1.Next(int.Parse(textBox_l1_min.Text), int.Parse(textBox_l1_max.Text));
ran_l2 = random_l2.Next(int.Parse(textBox_l2_min.Text), int.Parse(textBox_l2_max.Text));
ran_l3 = random_l3.Next(int.Parse(textBox_l3_min.Text), int.Parse(textBox_l3_max.Text));
ran_F1 = random_F1.Next(int.Parse(textBox_F1_min.Text), int.Parse(textBox_F1_max.Text));
ran_F2 = random_F2.Next(int.Parse(textBox_F2_min.Text), int.Parse(textBox_F2_max.Text));
ran_F3 = random_F3.Next(int.Parse(textBox_F3_min.Text), int.Parse(textBox_F3_max.Text));
*/
ran_m = random_m.Next(int.Parse(textBox_hm_cells_min.Text), int.Parse(textBox_hm_cellls_max.Text));
ran_width = random_width.NextDouble() * (int.Parse(textBox_width_max.Text) - int.Parse(textBox_width_min.Text)) + double.Parse(textBox_width_min.Text);
ran_thickness = random_thickness.NextDouble() * (int.Parse(textBox_thickness_max.Text) - int.Parse(textBox_thickness_min.Text)) + double.Parse(textBox_thickness_min.Text);
ran_l1 = random_l1.NextDouble() * (int.Parse(textBox_l1_max.Text) - int.Parse(textBox_l1_min.Text)) + double.Parse(textBox_l1_min.Text);
ran_l2 = random_l2.NextDouble() * (int.Parse(textBox_l2_max.Text) - int.Parse(textBox_l2_min.Text)) + double.Parse(textBox_l2_min.Text);
ran_l3 = random_l3.NextDouble() * (int.Parse(textBox_l3_max.Text) - int.Parse(textBox_l3_min.Text)) + double.Parse(textBox_l3_min.Text);
ran_F1 = random_F1.NextDouble() * (int.Parse(textBox_F1_max.Text) - int.Parse(textBox_F1_min.Text)) + double.Parse(textBox_F1_min.Text);
ran_F2 = random_F2.NextDouble() * (int.Parse(textBox_F2_max.Text) - int.Parse(textBox_F2_min.Text)) + double.Parse(textBox_F2_min.Text);
ran_F3 = random_F3.NextDouble() * (int.Parse(textBox_F3_max.Text) - int.Parse(textBox_F3_min.Text)) + double.Parse(textBox_F3_min.Text);
}
catch (Exception)
{
Console.WriteLine("Input Format Error");
return;
}
m = ran_m;
width = (double)(ran_width / 1000.0);
thickness = (double)(ran_thickness / 1000.0);
l1 = (double)(ran_l1 / 1000.0);
l2 = (double)(ran_l2 / 1000.0);
l3 = (double)(ran_l3 / 1000.0);
F1 = (double)ran_F1;
F2 = (double)ran_F2;
F3 = (double)ran_F3;
F3 = 0;
l4 = l2;
l1_ = l1 - 2 * width;
l2_ = l2;
l3_ = l3 + 2 * width;
l4_ = l4;
cell_length = l1 + l3;
cell_height = l2 + width;
#endregion
Console.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}", m, width, thickness, l1, l2, l3));
#region geometrie
Console.WriteLine("Start creating new Germetrie");
try
{
swModel = swApp.NewPart();
}
catch (Exception)
{
Console.WriteLine("starting SolidWorks");
try
{
swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application");
}
catch (Exception)
{
swApp = new SldWorks();
swApp.Visible = false;
}
//get MaterialLib
strMaterialLib = swApp.GetExecutablePath() + "\\lang\\english\\sldmaterials\\solidworks materials.sldmat";
Console.WriteLine("SolidWorks successfully started");
j--;
continue;
}
swModel.Extension.SelectByID("前视基准面", "PLANE", 0, 0, 0, false, 1, null);
swModel.InsertSketch2(true);
#region sketch
for (int i = 0; i < m; i++)
{
if (i == 0)
{
swModel.SketchManager.CreateLine(0, 0, 0, l1 - width, 0, 0);
swModel.SketchManager.CreateLine(l1 - width, 0, 0, l1 - width, -l2, 0);
swModel.SketchManager.CreateLine(l1 - width, -l2, 0, l1 + l3 - width, -l2, 0);
swModel.SketchManager.CreateLine(l1 + l3 - width, -l2, 0, l1 + l3 - width, 0, 0);
swModel.SketchManager.CreateLine(0, 0, 0, 0, -width, 0);
swModel.SketchManager.CreateLine(0, -width, 0, l1_, -width, 0);
swModel.SketchManager.CreateLine(l1_, -width, 0, l1_, -(width + l2_), 0);
swModel.SketchManager.CreateLine(l1_, -(width + l2_), 0, l1_ + l3_, -(width + l2_), 0);
swModel.SketchManager.CreateLine(l1_ + l3_, -(width + l2_), 0, l1_ + l3_, -width, 0);
}
else
{
x_o = i * cell_length - width;
y_o = 0;
x_o_ = x_o + width;
y_o_ = y_o - width;
swModel.SketchManager.CreateLine(x_o, y_o, 0, x_o + l1, 0, 0);
swModel.SketchManager.CreateLine(x_o + l1, 0, 0, x_o + l1, -l2, 0);
swModel.SketchManager.CreateLine(x_o + l1, -l2, 0, x_o + l1 + l3, -l2, 0);
swModel.SketchManager.CreateLine(x_o + l1 + l3, -l2, 0, x_o + l1 + l3, 0, 0);
swModel.SketchManager.CreateLine(x_o_, y_o_, 0, x_o_ + l1_, y_o_, 0);
swModel.SketchManager.CreateLine(x_o_ + l1_, y_o_, 0, x_o_ + l1_, y_o_ - l2_, 0);
swModel.SketchManager.CreateLine(x_o_ + l1_, y_o_ - l2_, 0, x_o_ + l1_ + l3_, y_o_ - l2_, 0);
swModel.SketchManager.CreateLine(x_o_ + l1_ + l3_, y_o_ - l2_, 0, x_o_ + l1_ + l3_, y_o_, 0);
}
}
swModel.SketchManager.CreateLine(x_o_ + l1_ + l3_, y_o_, 0, x_o_ + l1_ + l3_, 0, 0);
swModel.SketchManager.CreateLine(x_o_ + l1_ + l3_, 0, 0, x_o + l1 + l3, 0, 0);
#endregion
swModel.FeatureManager.FeatureExtrusion2(
true, false, false, 0, 0, thickness, 0, false, false, false, false, 0, 0, false, false, false, false, true, true, true, 0, 0, true
);
swModel.SaveAsSilent("D:\\TUD\\7.Semeter\\SA\\SA_code\\c#\\W_Form_analyse_get_TrainingDaten\\Geometrie.sldprt", true);
swApp.CloseAllDocuments(true);
swApp.OpenDoc("D:\\TUD\\7.Semeter\\SA\\SA_code\\c#\\W_Form_analyse_get_TrainingDaten\\Geometrie.sldprt", (int)swOpenDocOptions_e.swOpenDocOptions_Silent);
Console.WriteLine("Geometrie success");
#endregion
#region simulaiton
string path_to_cosworks_dll = @"C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\Simulation\cosworks.dll";
errors = swApp.LoadAddIn(path_to_cosworks_dll);
COSMOSObject = (CwAddincallback)swApp.GetAddInObject("SldWorks.Simulation");
try
{
COSMOSWORKS = (CosmosWorks)COSMOSObject.CosmosWorks;
}
catch (Exception)
{
Console.WriteLine("something wrong in Simulaiton Add In, start a new one");
continue;
}
COSMOSWORKS = COSMOSObject.CosmosWorks;
//Get active document
ActDoc = (CWModelDoc)COSMOSWORKS.ActiveDoc;
//Create new static study
StudyMngr = (CWStudyManager)ActDoc.StudyManager;
Study = (CWStudy)StudyMngr.CreateNewStudy("static study", (int)swsAnalysisStudyType_e.swsAnalysisStudyTypeStatic, 0, out errCode);
//Add materials
SolidMgr = Study.SolidManager;
SolidComp = SolidMgr.GetComponentAt(0, out errCode);
SolidBody = SolidComp.GetSolidBodyAt(0, out errCode);
intStatus = SolidBody.SetLibraryMaterial(strMaterialLib, "AISI 1020");
//fixed restraints
LBCMgr = Study.LoadsAndRestraintsManager;
swModel = (ModelDoc2)swApp.ActiveDoc;
swModel.ShowNamedView2("", (int)swStandardViews_e.swIsometricView);
selectionMgr = (SelectionMgr)swModel.SelectionManager;
isSelected = swModel.Extension.SelectByID2("", "FACE", 0, -width / 2.0, thickness / 2.0, false, 0, null, 0);
if (isSelected)
{
object selectedFace = (object)selectionMgr.GetSelectedObject6(1, -1);
object[] fixedFaces = { selectedFace };
CWRestraint restraint = (CWRestraint)LBCMgr.AddRestraint((int)swsRestraintType_e.swsRestraintTypeFixed, fixedFaces, null, out errCode);
}
swModel.ClearSelection2(true);
//add force
selectionMgr = (SelectionMgr)swModel.SelectionManager;
isSelected = swModel.Extension.SelectByID2("", "FACE", x_o + l1 + l3 + (width / 2.0), 0, thickness / 2.0, false, 0, null, 0);
if (isSelected)
{
object selectedFace = (object)selectionMgr.GetSelectedObject6(1, -1);
object[] forceAdd = { selectedFace };
selectionMgr = (SelectionMgr)swModel.SelectionManager;
swModel.Extension.SelectByID2("", "FACE", x_o + l1 + l3 + width, -(l4 + width) / 2.0, thickness / 2.0, false, 0, null, 0);
object selectedFaceToForceDir = (object)selectionMgr.GetSelectedObject6(1, -1);
double[] distValue = null;
double[] forceValue = null;
double[] Force = { F2, F3, F1 };
cwForce = (CWForce)LBCMgr.AddForce3((int)swsForceType_e.swsForceTypeForceOrMoment, (int)swsSelectionType_e.swsSelectionFaceEdgeVertexPoint,
2, 0, 0, 0,
(distValue),
(forceValue),
false, true,
(int)swsBeamNonUniformLoadDef_e.swsTotalLoad,
0, 7, 0.0,
Force,
false, false,
(forceAdd),
(selectedFaceToForceDir),
false, out errCode);//i have tried to figure out these arguments for one day, keep them and dont't change them.
//the way to check cwForce : cwForce.GetForceComponentValues()
//ForceComponet: [int b1, // 1 if x-direction hat Force-komponent, else 0
// int b2, // 1 if y-direction hat Force-komponent, else 0
// int b3, // 1 if z-direction hat Force-komponent, else 0
// double d1, // Force-komponent in x
// double d2, // Force-komponent in y
// double d3 // Force-komponent in z
// ]
//PS: the definition of xyz seems like not the same as the global XYZ system in SW.
swModel.ClearSelection2(true);
//meshing
CWMesh CWMeshObj = default(CWMesh);
CWMeshObj = Study.Mesh;
CWMeshObj.MesherType = (int)swsMesherType_e.swsMesherTypeStandard;
CWMeshObj.Quality = (int)swsMeshQuality_e.swsMeshQualityDraft;
errCode = Study.CreateMesh(0, MeshEleSize, MeshTol);
CWMeshObj = null;
//run analysis
errCode = Study.RunAnalysis();
if (errCode != 0)
{
Console.WriteLine(string.Format("RunAnalysis errCode = {0}", errCode));
Console.WriteLine(string.Format("RunAnalysis failed"));
swApp.CloseAllDocuments(true);
errors = swApp.UnloadAddIn(path_to_cosworks_dll);
Console.WriteLine(string.Format("ready to start a new one"));
continue;
}
Console.WriteLine("RunAnalysis successed, ready to get results");
//get results
CWFeatobj = Study.Results;
//get max von Mieses stress
Stress = (object[])CWFeatobj.GetMinMaxStress((int)swsStressComponent_e.swsStressComponentVON,
0, 0, null,
(int)swsStrengthUnit_e.swsStrengthUnitNewtonPerSquareMillimeter,
out errCode);
maxStress = (float)Stress[3]; //Stress: {node_with_minimum_stress, minimum_stress, node_with_maximum_stress, maximum_stress}
Console.WriteLine(maxStress);
/*
if (maxStress >= 351.6)
{
Console.WriteLine("out of yield stress, start a new example");
errors = swApp.UnloadAddIn(path_to_cosworks_dll);
swApp.CloseAllDocuments(true);
j--;
continue;
}
*/
//get max URES displacement
Disp = (object[])CWFeatobj.GetMinMaxDisplacement((int)swsDisplacementComponent_e.swsDisplacementComponentURES,
0, null,
(int)swsLinearUnit_e.swsLinearUnitMillimeters,
out errCode);
maxDisp = (float)Disp[3]; //Disp: {node_with_minimum_displacement, minimum_displacement, node_with_maximum_displacement, maximum_displacement}
CWFeatobj = null;
Console.WriteLine(string.Format("max Displacement: {0:f4} mm", maxDisp));
//output to Excel
exlSheet.Cells[j + 2, 1] = m;
exlSheet.Cells[j + 2, 2] = width * 1000;
exlSheet.Cells[j + 2, 3] = thickness * 1000;
exlSheet.Cells[j + 2, 4] = l1 * 1000;
exlSheet.Cells[j + 2, 5] = l2 * 1000;
exlSheet.Cells[j + 2, 6] = l3 * 1000;
exlSheet.Cells[j + 2, 7] = F1;
exlSheet.Cells[j + 2, 8] = F2;
exlSheet.Cells[j + 2, 9] = F3;
exlSheet.Cells[j + 2, 10] = maxStress;
exlSheet.Cells[j + 2, 11] = maxDisp;
if (j % 5 == 0)
{
exlBook.Save();//C:\Users\Zhao\Documents\工作簿1.xlsx
}
errors = swApp.UnloadAddIn(path_to_cosworks_dll);
swApp.CloseAllDocuments(true);
}
#endregion
}
exlBook.SaveCopyAs("D:\\TUD\\7.Semeter\\SA\\SA_code\\c#\\W_Form_analyse_get_TrainingDaten\\W_Form_simulationDaten_" + GetTimeStamp() +"_F3=0" + ".xlsx");
exlBook.Save();//C:\Users\zhaojie\Documents\工作簿1.xlsx
exlApp.Quit();
}
private void button_start_SW_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine("starting SolidWorks");
try
{
swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application");
}
catch (Exception)
{
swApp = new SldWorks();
swApp.Visible = false;
}
//get MaterialLib
strMaterialLib = swApp.GetExecutablePath() + "\\lang\\english\\sldmaterials\\solidworks materials.sldmat";
Console.WriteLine("SolidWorks successfully started");
}
private void button_start_Excel_Click(object sender, RoutedEventArgs e)
{
exlApp = new Microsoft.Office.Interop.Excel.Application();
exlApp.Visible = false;
exlBook = exlApp.Workbooks.Add();
exlSheet = exlBook.ActiveSheet;
exlSheet.Cells[1, 1] = "cells number";
exlSheet.Cells[1, 2] = "width(mm)";
exlSheet.Cells[1, 3] = "thickness(mm)";
exlSheet.Cells[1, 4] = "l1(mm)";
exlSheet.Cells[1, 5] = "l2(mm)";
exlSheet.Cells[1, 6] = "l3(mm)";
exlSheet.Cells[1, 7] = "F1(N)";
exlSheet.Cells[1, 8] = "F2(N)";
exlSheet.Cells[1, 9] = "F3(N)";
exlSheet.Cells[1, 10] = "maxStress(MPa)";
exlSheet.Cells[1, 11] = "maxDisp(mm)";
}
#region help function
public static void ErrorMsg(SldWorks SwApp, string Message)
{
SwApp.SendMsgToUser2(Message, 0, 0);
SwApp.RecordLine("'*** WARNING - General");
SwApp.RecordLine("'*** " + Message);
SwApp.RecordLine("");
}
/// <summary>
/// 获得13位的时间戳
/// </summary>
/// <returns></returns>
public static string GetTimeStamp()
{
System.DateTime time = System.DateTime.Now;
long ts = ConvertDateTimeToInt(time);
return ts.ToString();
}
/// <summary>
/// 将c# DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name="time">时间</param>
/// <returns>long</returns>
private static long ConvertDateTimeToInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
return t;
}
#endregion
}
}