ParamManager.cs
11.0 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
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static OnlineStore.DeviceLibrary.CountParam;
namespace OnlineStore.DeviceLibrary
{
public class ParamManager{
public static string NoConfigPath = @"D:\NoConfigImg\";
private static Dictionary<string, CountParam> countParamMap = new Dictionary<string, CountParam>();
private static string ConfigFilePath = "";
public static void Init()
{
ConfigFilePath = Application.StartupPath + ConfigAppSettings.GetValue(Setting_Init.CounParamConfig);
countParamMap = LoadParamMap();
}
public static string[] GetPNlist() {
return countParamMap.Keys.ToArray();
}
public static string GetSufanStr(int type)
{
SignType sign=SignType.AUTO;
Enum.TryParse<SignType>(type.ToString(), out sign);
return GetSufanStr(sign);
}
public static string GetSufanStr(SignType type)
{
if (type.Equals(0))
{
return "算法A";
}
else if (type.Equals(1))
{
return "算法B";
}
else if (type.Equals(2))
{
return "算法C";
}
else if (type.Equals(3))
{
return "算法D";
}
else if (type.Equals(4))
{
return "算法E";
}
return "默认算法";
}
public static CountParam GetParamByPN(string pn)
{
if (countParamMap.ContainsKey(pn))
{
CountParam lastParam = countParamMap[pn];
LogUtil.debug("【" + pn + "】使用参数 " + lastParam.ToStr() + " ");
return lastParam;
}
return null;
}
public static CountParam GetParamByCode(string codeStr)
{
//bool result = false;
//CountParam lastParam = new CountParam("", ThresholdValue, 3);
string pn = GetCodeStrPN(codeStr);
//var pns = pn.Split('.');
//if (pns.Length == 3)
// pn = pns[0] +"."+ pns[2];
if (countParamMap.ContainsKey(pn))
{
CountParam lastParam = countParamMap[pn];
LogUtil.debug("【" + codeStr + "】使用参数 " + lastParam.ToStr() + " ");
return lastParam;
}
return null;
}
internal static string GetCodeStrPN(string codeStr)
{
string[] codeArray = codeStr.Split(';');
if (codeArray.Length.Equals(2))
{
string pn = codeArray[0];
return pn;
}
return "";
}
private static List<CountParam> ErrPnParamList = new List<CountParam>();
public static void AddErrPNParam(CountParam errParam)
{
}
public static void UpdateParam(CountParam param)
{
try
{
OldPNList.AddOne(param.PN);
if (countParamMap.ContainsKey(param.PN))
{
countParamMap[param.PN] = param;
}
else
{
countParamMap.Add(param.PN, param);
}
LogUtil.info("UpdateParam " + param.ToStr() + " ");
}
catch (Exception ex)
{
LogUtil.error("UpdateParam 出错:" + ex.ToString());
}
}
public static void SaveMapToFile()
{
try
{
List<CountParam> pList = new List<CountParam>(countParamMap.Values);
int length = pList.Count + 1;
List<string> lineList = new List<string>();
lineList.Add(CountParam.GetCSVTitle());
foreach (CountParam p in pList)
{
lineList.Add(p.ToCSVStr());
}
File.WriteAllLines(ConfigFilePath, lineList);
LogUtil.info("SaveMapToFile 【" + ConfigFilePath + "】");
CSVBomManager.BackFile(ConfigFilePath, lineList.ToArray());
}
catch (Exception ex)
{
LogUtil.error("SaveMapToFile 出错:" + ex.ToString());
}
}
internal static Dictionary<string, CountParam> LoadParamMap( )
{
Dictionary<string, CountParam> countParamMap = new Dictionary<string, CountParam>();
if (File.Exists(ConfigFilePath))
{
try
{
string[] lines = File.ReadAllLines(ConfigFilePath);
int index = -1;
foreach (string line in lines)
{
index++;
if (index.Equals(0))
{
continue;
}
CountParam param = CountParam.NewParam(line);
if (param != null)
{
if (!countParamMap.ContainsKey(param.PN))
{
countParamMap.Add(param.PN, param);
}
else
countParamMap[param.PN] = param;
}
}
}
catch (Exception ex)
{
LogUtil.error("解析文件【" + ConfigFilePath + "】出错:" + ex.ToString());
}
LogUtil.info("加载点料参数共【" + countParamMap.Count + "】条有效数据" + ConfigFilePath);
}
else
{
LogUtil.error("未找到文件:" + ConfigFilePath);
}
return countParamMap;
}
/// <summary>
/// 获取最优算法
/// </summary>
/// <param name="currPN"></param>
/// <returns></returns>
public static CountParam GetOptimalParamByPN(string currPN)
{
CountParam param = GetParamByPN(currPN);
if (param == null)
{
//TODO
ComponetInfo com = CSVBomManager.GetCom(currPN);
if (com == null)
{
LogUtil.info("元器件库未找到 PN=" + currPN + " 的元器件");
return param;
}
List<ComponetInfo> FZList = new List<ComponetInfo>();
Dictionary<int, int> sufaCount = new Dictionary<int, int>();
SignType maxSufa = SignType.AUTO;
int maxCount = 0;
foreach (ComponetInfo obj in CSVBomManager.allComMap.Values)
{
if (obj.Encapsulations.Trim().Equals(com.Encapsulations.Trim()))
{
CountParam p = GetParamByPN(obj.PartNum);
if (p != null)
{
obj.Sufan = (int)p.Sign;
if (sufaCount.ContainsKey(obj.Sufan))
{
sufaCount[obj.Sufan]++;
}
else
{
sufaCount.Add(obj.Sufan, 1);
}
if (sufaCount[obj.Sufan] > maxCount)
{
maxCount = sufaCount[obj.Sufan];
Enum.TryParse<SignType>(obj.Sufan.ToString(),out maxSufa);
}
}
FZList.Add(obj);
}
}
if (maxCount >= 1)
{
LogUtil.info("元器件【" + com.ToStr() + "】查找到最优算法:【" + maxSufa + "】");
param = new CountParam(currPN, RobotManager.robot.XrayBean.ThresholdValue, 3, maxSufa);
}
}
return param;
}
public static Image FormImage(string fileName)
{
try
{
Image img = Image.FromFile(fileName);
return img;
}
catch (Exception ex)
{
LogUtil.info(ex.ToString());
}
return null;
}
}
public class CountParam
{
public CountParam(string pn, int th = 0, int size = 0, SignType sign=SignType.AUTO, int value = 0)
{
//var pns = pn.Split('.');
//if (pns.Length == 3)
// pn = pns[0] + "." + pns[2];
this.PN = pn;
this.Threshold = th;
this.WindowSize = size;
this.Sign = sign;
this.Value = value;
}
public string PN = "";
public int Threshold = 0;
public int WindowSize = 0;
public string AreaValue = "";
public SignType Sign = 0;
public int Value = 0;
public string ToStr()
{
return "【PN=" + PN + ",sign=" + Sign.ToString()+ ",value=" + Value + ",th=" + Threshold + ",wsize=" + WindowSize + "】";
}
public string ToCSVStr()
{
//type,sign,value,threshold,windowSize
return PN + spilt + Sign + spilt + Value + spilt + Threshold + spilt + WindowSize;
}
private static char spilt = ',';
internal static CountParam NewParam(string line)
{
try
{
string[] array = line.Split(spilt);
if (array.Length >= 5)
{
string pn = array[0].Trim();
//var pns = pn.Split('.');
//if (pns.Length == 3)
// pn = pns[0] + "." + pns[2];
SignType sign = SignType.AUTO;
Enum.TryParse<SignType>(array[1].Trim(), out sign);
int value = 0;// Convert.ToInt32(array[2].Trim());
int th =0;// Convert.ToInt32(array[3].Trim());
int size = 0;//Convert.ToInt32(array[4].Trim());
return new CountParam(pn, th, size, sign, value);
}
}
catch (Exception ex)
{
}
return null;
}
public enum SignType
{
IP_SMALL_PARTS=0,
IP_LARGE_PARTS = 1,
IP_LONG_PARTS = 2,
IP_SQUARE_PARTS = 3,
IP_GEN_PARTS = 4,
AUTO = 99,
}
internal static string GetCSVTitle()
{
return "type,sign,value,threshold,windowSize";
}
}
}