BoardManager.cs
19.2 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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
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;
using TSA_V.LoadCSVLibrary;
using System.Drawing.Imaging;
namespace TSA_V.DeviceLibrary
{
public class BoardManager
{
public static int PointDisplayType = ConfigAppSettings.GetIntValue(Setting_Init.PointDisplayType);
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>();
bool needSave = false;
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)
{
needSave = true;
board.smtList = new List<SMTPointInfo>();
}
foreach (SMTPointInfo sm in board.smtList)
{
if (String.IsNullOrEmpty(sm.TagNo) && (!String.IsNullOrEmpty(sm.PartNum)))
{
needSave = true;
sm.TagNo = sm.PartNum; ;
}
if (String.IsNullOrEmpty(sm.PN) && (!String.IsNullOrEmpty(sm.pointName)))
{
needSave = true;
sm.PN = sm.pointName; ;
}
}
if (board.boardId > MaxId)
{
needSave = true;
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(""))
{
needSave = true;
board.AOIProName = board.boardName + ".data";
}
idList.Add(board.boardId);
nameList.Add(board.boardName);
if (board.orgType == 0)
{
needSave = true;
board.orgType = 1;
}
if (board.LineWidth <= 0)
{
needSave = true;
board.LineWidth = board.boardWidth;
}
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);
}
if (needSave)
{
SaveListToFile(boardList);
LogUtil.info("保存程序集合到文件");
}
}
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);
}
/// <summary>
/// 导入程序
/// </summary>
/// <param name="board"></param>
public static void ImportBoard(BoardInfo board)
{
bool hasImg = false;
if (board.imageByte != null)
{
Image image = BoardManager.ByteToImg(board.imageByte);
if (image != null)
{
hasImg = true;
string configPath = ConfigAppSettings.GetValue(Setting_Init.BOARD_IMAGE_PATH);
string newName = board.boardName + ".png";
string newPath = Application.StartupPath + @"\" + configPath + @"\" + newName;
image.Save(newPath, ImageFormat.Png);
board.imgName = newName;
}
}
if (!hasImg)
{
string oldName = ConfigAppSettings.GetValue(Setting_Init.BOARD_IMAGE_DEFAULT);
string newName = board.boardName + Path.GetExtension(oldName);
string configPath = ConfigAppSettings.GetValue(Setting_Init.BOARD_IMAGE_PATH);
string oldPath = Application.StartupPath + @"\" + configPath + @"\" + oldName;
string newPath = Application.StartupPath + @"\" + configPath + @"\" + newName;
if (File.Exists(oldPath))
{
File.Copy(oldPath, newPath, true);
}
board.imgName = newName;
}
LogUtil.info("导入程序:程序名【"+board.boardName+"】,图片名【"+board.imgName+"】");
//判断bom是否存在
if (!CSVBomManager.BomNameIsExists(board.bomName))
{
List<ComponetInfo> componetInfos = new List<ComponetInfo>();
int i = 0;
foreach (SMTPointInfo p in board.smtList)
{
ComponetInfo c = new ComponetInfo();
if (p.componet != null)
{
c = p.componet;
}
else
{
c.Id = i + 1;
c.PN = p.PN;
c.PositionNum = CSVPositionReader<TSAVPosition>.getPositionKeyList()[0];
c.PositionX = p.PositionX;
c.PositionY = p.PositionY;
c.TagNo = p.TagNo;
c.Notes = "";
c.ComponentDes = "";
}
i++;
componetInfos.Add(c);
}
board.bomName = CSVBomManager.AutoAddBomList(board.bomName, componetInfos);
LogUtil.info("导入程序:程序名【" + board.boardName + "】,自动创建bom【"+board.bomName+"】");
}
board.boardId = BoardManager.GetNextId();
BoardManager.Add(board);
}
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;
}
/// <summary>
/// 新增元器件时,程序自动新增点位
/// </summary>
/// <param name="bomName"></param>
/// <param name="obj"></param>
//internal static void AddCom(string bomName, ComponetInfo obj)
//{
// List<BoardInfo> list = (from m in boardList where m.bomName.Equals(bomName) select m).ToList();
// foreach (BoardInfo board in list)
// {
// if (board.bomName.Equals(bomName))
// {
// int count = board.smtList.Count + 1;
// SMTPointInfo pointInfo = new SMTPointInfo(count, obj.TagNo, obj.PN, 5, 5, obj.PositionNum,1,5,5,2);
// pointInfo.Disable = true;
// board.smtList.Add(pointInfo);
// Update(board);
// LogUtil.info($"程序[{board.boardName}]使用元器件库[{bomName}],自动新增元器件[{obj.TagNo}-{obj.PN}],默认禁用");
// }
// }
//}
public static TSAVPosition getPointPosition(BoardInfo board, SMTPointInfo smtPoint)
{
ComponetInfo com = CSVBomManager.GetCom(board.bomName, smtPoint);
TSAVPosition position = null;
if (com != null)
{
position = CSVPositionReader<TSAVPosition>.GetPositonByNum(com.PositionNum);
}
else
{
position = CSVPositionReader<TSAVPosition>.GetPositonByNum(smtPoint.PositionNum);
}
return position;
}
/// <summary>
/// 图片路径转二进制
/// </summary>
/// <param name="strpath"></param>
/// <returns></returns>
public static byte[] ImgToByte(string strpath)
{
try
{
// 以二进制方式读文件
FileStream fsMyfile = new FileStream(strpath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
// 创建一个二进制数据流读入器,和打开的文件关联
BinaryReader brMyfile = new BinaryReader(fsMyfile);
// 把文件指针重新定位到文件的开始
brMyfile.BaseStream.Seek(0, SeekOrigin.Begin);
byte[] bytes = brMyfile.ReadBytes(Convert.ToInt32(fsMyfile.Length.ToString()));
// 关闭以上new的各个对象
brMyfile.Close();
return bytes;
}
catch (Exception ex)
{
LogUtil.error("图片【" + strpath + "】转为byte[]出错:" + ex.ToString());
return null;
}
}
/// <summary>
/// 二进制转为图片
/// </summary>
/// <param name="streamByte"></param>
/// <returns></returns>
public static System.Drawing.Image ByteToImg(byte[] streamByte)
{
try
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
return img;
}
catch (Exception ex)
{
LogUtil.error(" byte[]转为图片ByteToImg出错:" + ex.ToString());
return null;
}
}
}
}