-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.xaml.cs
More file actions
392 lines (363 loc) · 16.8 KB
/
Copy pathModel.xaml.cs
File metadata and controls
392 lines (363 loc) · 16.8 KB
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using IOPath = System.IO.Path;
using System.Windows.Shapes;
using System.Windows.Input;
using System.Windows.Threading;
using Apply_Gule_And_Tape_PC.Class;
using Newtonsoft.Json;
using System.Windows.Media;
using System.Text.Json;
using MaterialDesignThemes.Wpf;
using Newtonsoft.Json.Linq;
namespace Apply_Gule_And_Tape_PC
{
/// <summary>
/// Interaction logic for Model.xaml
/// </summary>
///
public partial class Model : UserControl
{
Link_Path linkpath = new Link_Path();
public ObservableCollection<FileInformation> FilesList { get; set; }
Common Common = new Common();
public Model()
{
InitializeComponent();
Loaded += Model_Loaded; // Thêm sự kiện Loaded
Unloaded += Model_Unloaded;
}
private void YourMethod()
{
Models_Mode_2.AddHandler(DataGrid.SelectionChangedEvent, new SelectionChangedEventHandler(Model2_SelectionChanged));
Models_Mode_3.AddHandler(DataGrid.SelectionChangedEvent, new SelectionChangedEventHandler(Model3_SelectionChanged));
}
private void Model2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedRow = Models_Mode_2.SelectedItem as DataView_Mode2;
if (selectedRow != null)
{
// Lấy dữ liệu từ hàng được chọn
var data = selectedRow.Model;
txb_ModelID2.Text = data.ToString();
var data_ = selectedRow.RotoID;
txb_RotoID2.Text = data_.ToString();
}
}
private void Model3_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedRow = Models_Mode_3.SelectedItem as DataView_Mode3;
if (selectedRow != null)
{
// Lấy dữ liệu từ hàng được chọn
var data = selectedRow.Model;
txb_ModelID3.Text = data.ToString();
var data_ = selectedRow.RotoID;
txb_RotoID3.Text = data_.ToString();
}
}
private void Model_Loaded(object sender, RoutedEventArgs e)
{
foreach (var textBox in Common.FindVisualChildren<TextBox>(this))
{
textBox.TextChanged += TextBox_TextChanged;
}
//
YourMethod();
//
ListModel1.LoadModel();
Models_Mode_1.ItemsSource = ListModel1.FilesList;
//
Common.Load_View_Mode2(Models_Mode_2);
Common.Load_View_Mode3(Models_Mode_3);
}
private void Model_Unloaded(object sender, RoutedEventArgs e)
{
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = (TextBox)sender;
string textboxName = textBox.Name;
if (textboxName == "txb_ModelID2" || textboxName == "txb_RotoID2")
{
Fill_Value_Mode2();
}
if (textboxName == "txb_ModelID3" || textboxName == "txb_RotoID3")
{
Fill_Value_Mode3();
}
}
private void Fill_Value_Mode2()
{
try
{
string json = File.ReadAllText(linkpath.Mode2);
if (json.Length > 0)
{
JArray jsonArray = JArray.Parse(json);
foreach (JObject obj in jsonArray)
{
if ((string)obj["Model"] == txb_ModelID2.Text & (string)obj["Code"] == txb_RotoID2.Text)
{
txb_ModelID2.Text = (string)obj["Model"];
txb_RotoID2.Text = (string)obj["Code"];
txb_TGXStart2.Text = (string)obj["Pos_X_Start"];
txb_TGZStart2.Text = (string)obj["Pos_Z_Start"];
txb_DRoto2.Text = (string)obj["D_Roto"];
txb_LRoto2.Text = (string)obj["L_Roto"];
txb_Dis_Step2.Text = (string)obj["Dis_Step"];
txb_Speed_Step2.Text = (string)obj["Speed"];
}
}
}
}
catch
{
}
}
private void Fill_Value_Mode3()
{
try
{
string json = File.ReadAllText(linkpath.Mode3);
if (json.Length > 0)
{
JArray jsonArray = JArray.Parse(json);
foreach (JObject obj in jsonArray)
{
if ((string)obj["Model"] == txb_ModelID3.Text & (string)obj["Code"] == txb_RotoID3.Text)
{
txb_ModelID3.Text = (string)obj["Model"];
txb_RotoID3.Text = (string)obj["Code"];
txb_BaseXStart3.Text = (string)obj["Pos_XBase_Start"];
txb_BaseYStart3.Text = (string)obj["Pos_YBase_Start"];
txb_DRoto3.Text = (string)obj["D_Roto"];
txb_LRoto3.Text = (string)obj["L_Roto"];
txb_Dis_Step3.Text = (string)obj["Dis_Step"];
txb_F_D3.Text = (string)obj["F_D"];
txb_SoVongQuay.Text = (string)obj["K"];
txb_XBase_OF_Han.Text = string.Format("{0:F3}", (double)obj["OF_Xbase"]);
txb_YBase_OF_Han.Text = string.Format("{0:F3}", (double)obj["OF_Ybase"]);
txb_Rotate_OF_Han.Text = string.Format("{0:F3}", (double)obj["OF_Rotate"]);
}
}
}
}
catch
{
}
}
private void Save_Model2()
{
if(float.Parse(txb_Speed_Step2.Text)>=3 && float.Parse(txb_Speed_Step2.Text) <=20)
{
List_Model_Mode2 List_Model2 = new List_Model_Mode2();
List_Model2.Model = txb_ModelID2.Text;
List_Model2.Code = txb_RotoID2.Text;
List_Model2.Pos_X_Start = float.Parse(txb_TGXStart2.Text);
List_Model2.Pos_Z_Start = float.Parse(txb_TGZStart2.Text);
List_Model2.D_Roto = float.Parse(txb_DRoto2.Text);
List_Model2.L_Roto = float.Parse(txb_LRoto2.Text);
List_Model2.Dis_Step = float.Parse(txb_Dis_Step2.Text);
List_Model2.Speed = float.Parse(txb_Speed_Step2.Text);
string list_Model2_Json = JsonConvert.SerializeObject(List_Model2);
try
{
string json = File.ReadAllText(linkpath.Mode2);
var options = new JsonSerializerOptions { ReadCommentHandling = JsonCommentHandling.Skip };
var data = System.Text.Json.JsonSerializer.Deserialize<List_Model_Mode2_Temp[]>(json, options);
float flag = 0;
foreach (var item in data)
{
if (item.Model == txb_ModelID2.Text || item.Code == txb_RotoID2.Text)
{
item.Model = txb_ModelID2.Text;
item.Code = txb_RotoID2.Text;
item.Pos_X_Start = float.Parse(txb_TGXStart2.Text);
item.Pos_Z_Start = float.Parse(txb_TGZStart2.Text);
item.D_Roto = float.Parse(txb_DRoto2.Text);
item.L_Roto = float.Parse(txb_LRoto2.Text);
item.Dis_Step = float.Parse(txb_Dis_Step2.Text);
item.Speed = float.Parse(txb_Speed_Step2.Text);
var jsonOptions = new JsonSerializerOptions { WriteIndented = true };
string newJsonString = System.Text.Json.JsonSerializer.Serialize(data, jsonOptions);
File.WriteAllText(linkpath.Mode2, newJsonString);
MessageBox.Show("Đã Lưu Thành Công");
flag = 1;
break;
}
}
if (flag == 0)
{
json = json.Remove(json.Length - 1);
json = json + "," + list_Model2_Json + "]";
File.WriteAllText(linkpath.Mode2, json);
MessageBox.Show("Đã Lưu Và Tạo Model Mới Thành Công");
}
}
catch
{
string json_;
json_ = "[" + list_Model2_Json + "]";
File.WriteAllText(linkpath.Mode2, json_);
MessageBox.Show("Đã Lưu Và Tạo Model Mới Thành Công");
}
Common.Load_View_Mode2(Models_Mode_2);
}
else
{
MessageBox.Show("Giá trị tốc độ không phù hợp, giá trị hợp lệ trong khoảng 3-20");
}
}
private void Save_Model3()
{
List_Model_Mode3 List_Model3 = new List_Model_Mode3();
List_Model3.Model = txb_ModelID3.Text;
List_Model3.Code = txb_RotoID3.Text;
List_Model3.Pos_XBase_Start = float.Parse(txb_BaseXStart3.Text);
List_Model3.Pos_YBase_Start = float.Parse(txb_BaseYStart3.Text);
List_Model3.D_Roto = float.Parse(txb_DRoto3.Text);
List_Model3.L_Roto = float.Parse(txb_LRoto3.Text);
List_Model3.Dis_Step = float.Parse(txb_Dis_Step3.Text);
List_Model3.F_D = float.Parse(txb_F_D3.Text);
List_Model3.K = float.Parse(txb_SoVongQuay.Text);
List_Model3.OF_Xbase = float.Parse(txb_XBase_OF_Han.Text);
List_Model3.OF_Ybase = float.Parse(txb_YBase_OF_Han.Text);
List_Model3.OF_Rotate = float.Parse(txb_Rotate_OF_Han.Text);
string list_Model3_Json = JsonConvert.SerializeObject(List_Model3);
try
{
string json = File.ReadAllText(linkpath.Mode3);
var options = new JsonSerializerOptions { ReadCommentHandling = JsonCommentHandling.Skip };
var data = System.Text.Json.JsonSerializer.Deserialize<List_Model_Mode3_Temp[]>(json, options);
float flag = 0;
foreach (var item in data)
{
if (item.Model == txb_ModelID3.Text || item.Code == txb_RotoID3.Text)
{
item.Model = txb_ModelID3.Text;
item.Code = txb_RotoID3.Text;
item.Pos_XBase_Start = float.Parse(txb_BaseXStart3.Text);
item.Pos_YBase_Start = float.Parse(txb_BaseYStart3.Text);
item.D_Roto = float.Parse(txb_DRoto3.Text);
item.L_Roto = float.Parse(txb_LRoto3.Text);
item.Dis_Step = float.Parse(txb_Dis_Step3.Text);
item.F_D = float.Parse(txb_F_D3.Text);
item.K = float.Parse(txb_SoVongQuay.Text);
item.OF_Xbase = float.Parse(txb_XBase_OF_Han.Text);
item.OF_Ybase = float.Parse(txb_YBase_OF_Han.Text);
item.OF_Rotate = float.Parse(txb_Rotate_OF_Han.Text);
var jsonOptions = new JsonSerializerOptions { WriteIndented = true };
string newJsonString = System.Text.Json.JsonSerializer.Serialize(data, jsonOptions);
File.WriteAllText(linkpath.Mode3, newJsonString);
MessageBox.Show("Đã Lưu Thành Công");
flag = 1;
break;
}
}
if (flag == 0)
{
json = json.Remove(json.Length - 1);
json = json + "," + list_Model3_Json + "]";
File.WriteAllText(linkpath.Mode3, json);
MessageBox.Show("Đã Lưu Và Tạo Model Mới Thành Công");
}
}
catch
{
string json_;
json_ = "[" + list_Model3_Json + "]";
File.WriteAllText(linkpath.Mode3, json_);
MessageBox.Show("Đã Lưu Và Tạo Model Mới Thành Công");
}
Common.Load_View_Mode3(Models_Mode_3);
}
private void Clear_Model2()
{
MessageBoxResult result = MessageBox.Show("Bạn có chắc chắn muốn xóa mã Model: " + txb_ModelID2.Text + " và mã Roto: " + txb_RotoID2.Text + " ?", "Xác nhận xóa", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes & txb_ModelID2.Text.Length > 0 & txb_RotoID2.Text.Length > 0)
{
string json = File.ReadAllText(linkpath.Mode2);
var options = new JsonSerializerOptions { ReadCommentHandling = JsonCommentHandling.Skip };
var data = System.Text.Json.JsonSerializer.Deserialize<List_Model_Mode2_Temp[]>(json, options);
var newData = new List<List_Model_Mode2_Temp>();
foreach (var item in data)
{
if (item.Model != txb_ModelID2.Text || item.Code != txb_RotoID2.Text)
{
newData.Add(item);
}
}
var jsonOptions = new JsonSerializerOptions { WriteIndented = true };
string newJsonString = System.Text.Json.JsonSerializer.Serialize(newData, jsonOptions);
// Write back to file
File.WriteAllText(linkpath.Mode2, newJsonString);
Common.Load_View_Mode2(Models_Mode_2);
}
else
{
MessageBox.Show("Không tìm thấy mã Model: " + txb_ModelID2.Text + " và mã Roto: " + txb_RotoID2.Text + " cần xóa");
}
}
private void Clear_Model3()
{
MessageBoxResult result = MessageBox.Show("Bạn có chắc chắn muốn xóa mã Model: " + txb_ModelID3.Text + " và mã Roto: " + txb_RotoID3.Text + " ?", "Xác nhận xóa", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes & txb_ModelID3.Text.Length > 0 & txb_RotoID3.Text.Length > 0)
{
string json = File.ReadAllText(linkpath.Mode3);
var options = new JsonSerializerOptions { ReadCommentHandling = JsonCommentHandling.Skip };
var data = System.Text.Json.JsonSerializer.Deserialize<List_Model_Mode3_Temp[]>(json, options);
var newData = new List<List_Model_Mode3_Temp>();
foreach (var item in data)
{
if (item.Model != txb_ModelID3.Text || item.Code != txb_RotoID3.Text)
{
newData.Add(item);
}
}
var jsonOptions = new JsonSerializerOptions { WriteIndented = true };
string newJsonString = System.Text.Json.JsonSerializer.Serialize(newData, jsonOptions);
// Write back to file
File.WriteAllText(linkpath.Mode3, newJsonString);
Common.Load_View_Mode3(Models_Mode_3);
}
else
{
MessageBox.Show("Không tìm thấy mã Model: " + txb_ModelID3.Text + " và mã Roto: " + txb_RotoID3.Text + " cần xóa");
}
}
private void Set_Pos_Mode2_Click(object sender, RoutedEventArgs e)
{
txb_TGXStart2.Text = Data.Pos_X.ToString();
txb_TGZStart2.Text = Data.Pos_Z.ToString();
}
private void Set_Pos_Mode3_Click(object sender, RoutedEventArgs e)
{
txb_BaseXStart3.Text = Data.Pos_XBase.ToString();
txb_BaseYStart3.Text = Data.Pos_YBase.ToString();
}
private void bt_Save_Model2_Click(object sender, RoutedEventArgs e)
{
Save_Model2();
}
private void bt_Clear_Model2_Click(object sender, RoutedEventArgs e)
{
Clear_Model2();
}
private void bt_Save_Model3_Click(object sender, RoutedEventArgs e)
{
Save_Model3();
}
private void bt_Clear_Model3_Click(object sender, RoutedEventArgs e)
{
Clear_Model3();
}
}
}