CSVReaderManager.cs
18.7 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.Common;
using TSA_V.DeviceLibrary;
namespace TSA_V.LoadCSVLibrary
{
public class CSVReaderBomManager : CSVReaderBase
{
/// <summary>
/// 所有的位置集合,key=位置
/// </summary>
public static Dictionary<string, Dictionary<string, ComponetInfo>> allComMap = new Dictionary<string, Dictionary<string, ComponetInfo>>();
public static List<ComponetInfo> GetComList(string bomName)
{
if (allComMap.ContainsKey(bomName))
{
return new List<ComponetInfo>(allComMap[bomName].Values);
}
else
{
string fileName = getFilePath(bomName);
if (File.Exists(fileName))
{
List<ComponetInfo> list = ReadFile(fileName);
Dictionary<string, ComponetInfo> map = new Dictionary<string, ComponetInfo>();
foreach (ComponetInfo com in list)
{
map.Add(com.PartNum, com);
}
allComMap.Add(bomName, map);
return list;
}
}
return null;
}
public static void LoadAllCom()
{
allComMap = new Dictionary<string, Dictionary<string, ComponetInfo>>();
string appPath = Application.StartupPath;
string filePath = appPath + ConfigAppSettings.GetValue(Setting_Init.ComPath_Config);
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
//加载所有数据
string[] fileList = Directory.GetFiles(filePath);
if (fileList.Length > 0)
{
foreach (string fileName in fileList)
{
try {
string extension = Path.GetExtension(fileName); //-->.xml
if (extension.ToLower().Equals(".csv"))
{
string bomName = Path.GetFileNameWithoutExtension(fileName); //-->BenXHCMS
List<ComponetInfo> list = ReadFile(fileName);
Dictionary<string, ComponetInfo> map = new Dictionary<string, ComponetInfo>();
foreach (ComponetInfo com in list)
{
if (com == null||com.PartNum==null||com.PositionNum==null)
{
continue;
}
if (map.ContainsKey(com.PartNum))
{
LogUtil.error("元器件库【" + bomName + "】【" + com.PartNum + "】重复");
}
else
{
map.Add(com.PartNum, com);
}
}
if (map.Count > 0)
{
allComMap.Add(bomName, map);
}
}
}catch(Exception ex)
{
LogUtil.error("加载出错:"+ex.ToString());
}
}
}
}
public static string getFilePath(string bomName)
{
string appPath = Application.StartupPath;
string filePath = appPath + ConfigAppSettings.GetValue(Setting_Init.ComPath_Config);
return filePath + bomName + ".csv";
}
/// <summary>
/// 添加一个csv文件的数据到位置集合中
/// </summary>
/// <param name="filePath">cvs文件路径+文件名</param>
/// <returns></returns>
public static List<ComponetInfo> ReadFile(string filePath)
{
System.Type type = typeof(ComponetInfo);
List<ComponetInfo> comList = new List<ComponetInfo>();
Dictionary<string, string> proTitleMap = getProAttributeMap(typeof(ComponetInfo));
List<string> cvsTitleList = new List<string>(proTitleMap.Values);
List<string> propertyList = new List<string>(proTitleMap.Keys);
string[] lines = ReadCSVFile(filePath);
int index = 0;
Dictionary<string, int> titleIndex = new Dictionary<string, int>();
foreach (var line in lines)
{
var array = line.Split(Spilt_Char);
if (index == 0)
{
titleIndex = GetTitleIndex(line, cvsTitleList);
}
else
{
try
{
if (array.Length >= titleIndex.Count)
{
if (array.Length > 0 && array[0].Equals(""))
{
continue;
}
var bllIns = type.Assembly.CreateInstance(type.FullName);
//取得属性集合
PropertyInfo[] props = type.GetProperties();
int listIndex = 0;
foreach (string key in cvsTitleList)
{
if (titleIndex.ContainsKey(key))
{
int titIndex = titleIndex[key];
string value = array[titIndex];
string proName = propertyList[listIndex];
PropertyInfo prop = props.First(c => c.Name == proName);//获取同名属性
if (prop != null)
{
string typeName = prop.PropertyType.Name;
//判断值是否为空
if (value.Equals("") && (typeName.ToUpper().Equals("INT32") || typeName.ToUpper().Equals("DOUBLE")))
{
value = "0";
}
//如果属性存在
prop.SetValue(bllIns, Convert.ChangeType(value, prop.PropertyType), null);//赋值****在这里需要考虑类型问题
}
}
listIndex++;
}
comList.Add((ComponetInfo)bllIns);
}
else
{
LOGGER.Error("读取csv,index=" + index + ",数据格式不匹配!,line=" + line);
}
}
catch (Exception ex)
{
LOGGER.Debug("CSV 读取行【" + line + "】行转换失败");
}
}
index++;
}
return comList;
}
public static bool SaveComponet(string bomName, ComponetInfo com)
{
if (!allComMap.ContainsKey(bomName))
{
return false;
}
Dictionary<string, ComponetInfo> oldMap = allComMap[bomName];
System.Type type = typeof(ComponetInfo);
Dictionary<string, string> proTitleMap = getProAttributeMap(typeof(ComponetInfo));
List<string> cvsTitleList = new List<string>(proTitleMap.Values);
List<string> propertyList = new List<string>(proTitleMap.Keys);
int IdIndex = propertyList.IndexOf("PartNum");
int csvIndex = -1;
string filePath = getFilePath(bomName);
List<ComponetInfo> oldList = GetComList(bomName);
int idIndex = propertyList.IndexOf("PartNum");
string[] lines = ReadCSVFile(filePath);
int index = 0;
Dictionary<string, int> titleIndex = new Dictionary<string, int>();
foreach (var line in lines)
{
var array = line.Split(',');
if (index == 0)
{
titleIndex = GetTitleIndex(line, cvsTitleList);
}
else
{
if (array.Length == titleIndex.Count)
{
if (csvIndex < 0)
{
csvIndex = titleIndex[cvsTitleList[idIndex]];
}
string value = array[csvIndex];
if (value.Equals(com.PartNum))
{
//更新缓存
oldMap.Remove(com.PartNum);
oldMap.Add(com.PartNum, com);
string newValue = ObjToString(com, titleIndex, proTitleMap);
lines[index] = newValue;
return WriteCSVFile(filePath, lines);
}
}
}
index++;
}
return true;
}
public static bool SaveComponet(string bomName, List<ComponetInfo> positionList)
{
string filePath = getFilePath(bomName);
System.Type type = typeof(ComponetInfo);
Dictionary<string, string> proTitleMap = getProAttributeMap(typeof(ComponetInfo));
List<string> cvsTitleList = new List<string>(proTitleMap.Values);
List<string> propertyList = new List<string>(proTitleMap.Keys);
Dictionary<string, int> titleIndex = new Dictionary<string, int>();
string[] readLines = ReadCSVFile(filePath);
string[] lines = new string[positionList.Count + 1];
foreach (var line in readLines)
{
lines[0] = line;
var array = line.Split(',');
titleIndex = GetTitleIndex(line, cvsTitleList);
break;
}
int index = 1;
foreach (ComponetInfo position in positionList)
{
string newValue = ObjToString(position, titleIndex, proTitleMap);
lines[index] = newValue;
index++;
}
return WriteCSVFile(filePath, lines);
}
private static string ObjToString(ComponetInfo position, Dictionary<string, int> titleIndex, Dictionary<string, string> proTitleMap)
{
//取得属性集合
PropertyInfo[] props = typeof(ComponetInfo).GetProperties();
List<string> cvsTitleList = new List<string>(proTitleMap.Values);
List<string> propertyList = new List<string>(proTitleMap.Keys);
String[] array = new String[titleIndex.Count];
foreach (string proName in proTitleMap.Keys)
{
PropertyInfo prop = props.First(c => c.Name == proName);//获取同名属性
if (prop != null)
{//如果属性存在
string value = prop.GetValue(position,null).ToString();
int index = titleIndex[proTitleMap[proName]];
array[index] = value;
}
}
string newStr = "";
foreach (string str in array)
{
if (newStr.Equals(""))
{
newStr = str;
}
else
{
newStr = newStr + Spilt_Char + str;
}
}
return newStr;
}
/// <summary>
/// 获取此文件第一行的数据
/// </summary>
private static string GetHeaderString(List<string> cvsValues)
{
string newStr = "";
foreach (string str in cvsValues)
{
if (newStr.Equals(""))
{
newStr = str;
}
else
{
newStr = newStr + Spilt_Char + str;
}
}
return newStr;
}
/// <summary>
/// 增加一个元器件库
/// </summary>
public static bool AddBomList(string bomName, List<ComponetInfo> comList)
{
try
{
RemoveBom(bomName);
string filePath = getFilePath(bomName);
if (File.Exists(filePath))
{
//删除原来的文件
File.Delete(filePath);
LogUtil.error("新增元器件库:" + bomName + ",发现文件【" + filePath + "】已存在,删除原来的文件");
}
System.Type type = typeof(ComponetInfo);
Dictionary<string, string> proTitleMap = getProAttributeMap(typeof(ComponetInfo));
List<string> cvsTitleList = new List<string>(proTitleMap.Values);
List<string> propertyList = new List<string>(proTitleMap.Keys);
Dictionary<string, int> titleIndexMap = new Dictionary<string, int>();
int titleIndex=0;
foreach (string str in cvsTitleList)
{
titleIndexMap.Add(str, titleIndex);
titleIndex++;
}
string[] lines = new string[comList.Count + 1];
lines[0] = GetHeaderString(cvsTitleList);
int index = 1;
Dictionary<string, ComponetInfo> map = new Dictionary<string, ComponetInfo>();
foreach (ComponetInfo com in comList)
{
map.Add(com.PartNum,com);
lines[index] = ObjToString(com, titleIndexMap, proTitleMap);
index++;
}
bool result =WriteCSVFile(filePath, lines);
if (result)
{
allComMap.Add(bomName, map);
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
LogUtil.error("增加元器件库出错 :【"+bomName+"】"+ex.ToString());
return false;
}
}
public static void RemoveBom(string bomName)
{
string fileName = getFilePath(bomName);
if (allComMap.ContainsKey(bomName))
{
allComMap.Remove(bomName);
}
if (File.Exists(fileName))
{
File.Delete(fileName);
}
}
public static bool RemoveCom(string bomName, ComponetInfo obj)
{
string fileName = getFilePath(bomName);
File.Delete(bomName);
if (allComMap.ContainsKey(bomName))
{
Dictionary<string, ComponetInfo> map = allComMap[bomName];
map.Remove(obj.PartNum);
List<ComponetInfo> newList = new List<ComponetInfo>(map.Values);
AddBomList(bomName, newList);
return true;
}
return false;
}
public static ComponetInfo GetCom(string bomName, string partNum)
{
if (allComMap.ContainsKey(bomName))
{
Dictionary<string, ComponetInfo> map = allComMap[bomName];
if (map.ContainsKey(partNum))
{
return map[partNum];
}
} return null;
}
/// <summary>
/// 更新元器件数量
/// </summary>
public static bool UpdateCount(string bomName, string comName, int count)
{
List<ComponetInfo> comList = CSVReaderBomManager.GetComList(bomName);
if (comList == null)
{
return false;
}
List<ComponetInfo> newList = new List<ComponetInfo>();
foreach (ComponetInfo obj in comList)
{
if (obj.ComponentName.Equals(comName))
{
obj.ComCount = count;
}
newList.Add(obj);
}
CSVReaderBomManager.SaveComponet(bomName, newList);
return true;
}
/// <summary>
/// 减去此电路板使用的元器件数量
/// </summary>
/// <param name="currBoard"></param>
public static bool DelUseCount(DeviceLibrary.BoardInfo currBoard,int maxIndex)
{
try
{
string bomName = currBoard.bomName;
List<ComponetInfo> comList = CSVReaderBomManager.GetComList(bomName);
if (comList == null)
{
return false;
}
Dictionary<string, int> useCount = new Dictionary<string, int>();
int index = 0;
List<SMTPointInfo> list = currBoard.GetSmtList();
foreach (SMTPointInfo smt in list)
{
if (index > maxIndex)
{
break;
}
ComponetInfo com = CSVReaderBomManager.GetCom(BoardManager.CurrBoard.bomName, smt.PartNum);
if (useCount.ContainsKey(com.ComponentName))
{
useCount[com.ComponentName]++;
}
else
{
useCount.Add(com.ComponentName, 1);
}
index++;
}
List<ComponetInfo> newList = new List<ComponetInfo>();
foreach (ComponetInfo obj in comList)
{
if (useCount.ContainsKey(obj.ComponentName))
{
obj.ComCount = obj.ComCount - useCount[obj.ComponentName];
if (obj.ComCount < 0)
{
obj.ComCount = 0;
}
}
newList.Add(obj);
}
CSVReaderBomManager.SaveComponet(bomName, newList);
}catch(Exception ex)
{
}
return true;
}
}
}