BoardManager.cs
11.9 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
using log4net;
using TSA_V.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TSA_V.DeviceLibrary
{
public class BoardManager
{
private static int MaxId = 0;
/// <summary>
/// 板子列表
/// </summary>
public static List<BoardInfo> boardList = new List<BoardInfo>();
public static Image defaultImage = null;
/// <summary>
/// 当前正在使用的板子
/// </summary>
public static BoardInfo CurrBoard = null;
public static int GetNextId()
{
MaxId++;
return MaxId;
}
public static string GetConfigPath()
{
string appPath = Application.StartupPath;
string filePath = appPath + ConfigAppSettings.GetValue(Setting_Init.Board_ConfigPath);
return filePath;
}
public static void LoadBoard()
{
LogUtil.info("开始加载电路板列表");
// CurrBoardId = ConfigAppSettings.GetIntValue(Setting_Init.Default_BoardID);
string filePath = GetConfigPath();
if (!File.Exists(filePath))
{
LogUtil.error("加载产品失败:文件[" + filePath + "]不存在");
return;
}
string[] lines = File.ReadAllLines(filePath, Encoding.GetEncoding("gbk"));
boardList = new List<BoardInfo>();
List<int> idList = new List<int>();
List<string> nameList = new List<string>();
foreach (string str in lines)
{
try
{
string newStr = str;
string errMsg = "\"System.Drawing.Bitmap\"";
if (str.Contains(errMsg))
{
newStr = str.Replace(errMsg, "null");
}
BoardInfo board = JsonHelper.DeserializeJsonToObject<BoardInfo>(newStr);
if (board != null && board.boardName != null)
{
if (board.smtList == null)
{
board.smtList = new List<SMTPointInfo>();
}
foreach (SMTPointInfo sm in board.smtList)
{
if (sm.PartNum == null)
{
sm.PartNum = "";
}
}
if (board.boardId > MaxId)
{
MaxId = board.boardId;
}
if (idList.Contains(board.boardId))
{
LogUtil.error("加载产品配置出错:id=" + board.boardId + "的数据重复!");
}
if (nameList.Contains(board.boardName))
{
LogUtil.error("加载产品配置出错:name=" + board.boardName + "的数据重复!");
}
if (board.AOIProName.Equals(""))
{
board.AOIProName = board.boardName + ".data";
}
idList.Add(board.boardId);
nameList.Add(board.boardName);
if (board.orgType == 0)
{
board.orgType = 1;
}
boardList.Add(board);
}
}
catch (Exception ex)
{
LogUtil.error("出错:" + ex.ToString());
}
}
LogUtil.info("加载电路板完成,共加载【" + boardList.Count + "】块电路板配置");
string path = ConfigAppSettings.GetValue(Setting_Init.BOARD_IMAGE_PATH);
string imagePath = Application.StartupPath + @"\" + path + ConfigAppSettings.GetValue(Setting_Init.BOARD_IMAGE_DEFAULT);
if (File.Exists(imagePath))
{
defaultImage = Image.FromFile(imagePath);
}
else
{
LogUtil.error("未找到默认图片:" + imagePath);
}
}
public static void Update(BoardInfo board)
{
int index = 0;
board.myImage = null;
foreach (BoardInfo bo in boardList)
{
if (bo.boardId.Equals(board.boardId))
{
boardList[index] = board;
break;
}
index++;
}
SaveListToFile(boardList);
}
public static void Add(BoardInfo board)
{
boardList.Add(board);
SaveListToFile(boardList);
}
private static bool SaveListToFile(List<BoardInfo> list)
{
string[] lines = new string[boardList.Count];
int index = 0;
foreach (BoardInfo bo in list)
{
lines[index] = JsonHelper.SerializeObject(bo);
index++;
}
string filePath = GetConfigPath();
try
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
File.WriteAllLines(filePath, lines, Encoding.GetEncoding("gbk"));
//备份保存
try
{
FileInfo file = new FileInfo(filePath);
string date = DateTime.Now.ToString("yyyy-MM-dd");
string targetBackPath = @"C:\configBack\" + date + @"\";
if (!Directory.Exists(targetBackPath))
{
Directory.CreateDirectory(targetBackPath);
}
string fileName = file.Name;
string backFile = targetBackPath + fileName;
if (File.Exists(backFile))
{
File.Delete(backFile);
}
File.WriteAllLines(backFile, lines, Encoding.GetEncoding("gbk"));
}
catch (Exception e)
{
LogUtil.error("备份配置到C:configBack出错:" + e.ToString());
}
//备份保存
try
{
FileInfo file = new FileInfo(filePath);
string date = DateTime.Now.ToString("yyyy-MM-dd");
string targetBackPath = @"D:\configBack\" + date + @"\";
if (!Directory.Exists(targetBackPath))
{
Directory.CreateDirectory(targetBackPath);
}
string fileName = file.Name;
string backFile = targetBackPath + fileName;
if (File.Exists(backFile))
{
File.Delete(backFile);
}
File.WriteAllLines(backFile, lines, Encoding.GetEncoding("gbk"));
}
catch (Exception e)
{
LogUtil.error("备份配置到D:configBack出错:" + e.ToString());
}
}
catch (Exception ex)
{
LogUtil.error("出错:" + ex.ToString());
return false;
}
return true;
}
public static void Delete(BoardInfo board)
{
boardList.Remove(board);
string[] lines = new string[boardList.Count];
int index = 0;
foreach (BoardInfo bo in boardList)
{
lines[index] = JsonHelper.SerializeObject(bo);
index++;
}
SaveListToFile(boardList);
}
public static bool HasId(int boardId)
{
foreach (BoardInfo board in boardList)
{
if (board.boardId.Equals(boardId))
{
return true;
}
}
return false;
}
public static BoardInfo getBoardById(int PreboardId)
{
foreach (BoardInfo board in boardList)
{
if (board.boardId.Equals(PreboardId))
{
return board;
}
}
return null;
}
public static BoardInfo getBoardByName(string name)
{
foreach (BoardInfo board in boardList)
{
if (board.boardName.Equals(name))
{
return board;
}
}
return null;
}
/// <summary>
/// 修正前备份
/// </summary>
private static bool UpdateBack(List<BoardInfo> list, double xUpdate, double yUpdate)
{
string[] lines = new string[boardList.Count];
int index = 0;
foreach (BoardInfo bo in list)
{
lines[index] = JsonHelper.SerializeObject(bo);
index++;
}
string appPath = Application.StartupPath;
string date = DateTime.Now.ToString("yyyy-MM-dd-HH-mm");
string filePath = appPath + ConfigAppSettings.GetValue(Setting_Init.Board_ConfigPath) + "." + date + "(" + xUpdate.ToString() + "," + yUpdate.ToString() + ")" + ".back";
try
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
File.WriteAllLines(filePath, lines, Encoding.GetEncoding("gbk"));
}
catch (Exception ex)
{
LogUtil.error("出错:" + ex.ToString());
return false;
}
return true;
}
public static void UpdatePointSize(int boardId, int pSizeX, int pSizeY, int pointType, int penWidth)
{
foreach (BoardInfo board in boardList)
{
if (boardId.Equals(-1) || board.boardId.Equals(boardId))
{
LogUtil.info("电路板【" + board.boardName + "】点大小更改为: [" + pSizeX + "," + pSizeY + "] 点类型更改为: [" + pointType + "]画笔宽度:[" + penWidth + "]");
foreach (SMTPointInfo point in board.smtList)
{
point.PointSizeX = pSizeX;
point.PointSizeY = pSizeY;
point.PointType = pointType;
point.PenWidth = penWidth;
}
}
}
SaveListToFile(boardList);
}
public static BoardInfo StringToBoard(string str, out bool IsNeedUpdate)
{
IsNeedUpdate = false;
BoardInfo board = null;
try
{
string newStr = str;
string errMsg = "\"System.Drawing.Bitmap\"";
if (str.Contains(errMsg))
{
newStr = str.Replace(errMsg, "null");
}
board = JsonHelper.DeserializeJsonToObject<BoardInfo>(newStr);
// bool isUpdate = false;
if (board != null)
{
if (board.boardId > MaxId)
{
MaxId = board.boardId;
}
}
}
catch (Exception ex)
{
LogUtil.error("出错:" + ex.ToString());
}
return board;
}
}
}