BeingExecuted.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. using OpenCvSharp;
  2. using Resources;
  3. using SmartCoalApplication.Annotation;
  4. using SmartCoalApplication.Base.AutoMeasure;
  5. using SmartCoalApplication.Base.CommTool;
  6. using SmartCoalApplication.Base.MeasureModel;
  7. using SmartCoalApplication.Resources;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Reflection;
  16. using System.Text;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using System.Windows.Forms;
  20. namespace SmartCoalApplication.AutomaticMeasurement
  21. {
  22. public partial class BeingExecuted : Form
  23. {
  24. private MeasureInfoModel infoModel = new MeasureInfoModel();
  25. private List<Label> fenleimingchengs = new List<Label>();
  26. private List<Label> jindus = new List<Label>();
  27. private DateTime t;
  28. private DateTime dateTime = DateTime.Now;
  29. /// <summary>
  30. /// 图片总数
  31. /// </summary>
  32. private int allPictureNum = 0;
  33. /// <summary>
  34. /// 预估总时长(1张图片暂定5s)
  35. /// </summary>
  36. private int estimatedTimeNum = 0;
  37. /// <summary>
  38. /// 已分析完的图片数量
  39. /// </summary>
  40. private int hasFinishedPic = 0;
  41. /// <summary>
  42. /// 进行到第几步
  43. /// </summary>
  44. private int stepNum = 0;
  45. /// <summary>
  46. /// 进行到第几步
  47. /// </summary>
  48. private bool startS = true;
  49. private Task task;
  50. private CancellationTokenSource tokenSource;
  51. private AppWorkspace appWorkspace;
  52. private Dictionary<string, string> _listHoleType;
  53. private Dictionary<string, bool> _listHoleTypePos;
  54. private MeasureMaintenanceTreeData measureMaintenanceTreeData;
  55. public MeasureMaintenanceType measureMaintenanceType;
  56. public Dictionary<string, Dictionary<string, double>> pidRuleDic;
  57. public Task task1;
  58. private CancellationTokenSource tokenSource1;
  59. public string filePath;
  60. private bool needStop = false;
  61. /// <summary>
  62. /// 分析结果图
  63. /// </summary>
  64. private List<MeasureInfoResultModel> resultMatList = new List<MeasureInfoResultModel>();
  65. internal BeingExecuted(MeasureInfoModel infoModel,AppWorkspace appWorkspace, Dictionary<string, string> _listHoleType,MeasureMaintenanceType measureMaintenanceType, Dictionary<string, Dictionary<string, double>> pidRuleDic,string path, Dictionary<string, bool> _listHoleTypePos)
  66. {
  67. this.infoModel = infoModel;
  68. InitializeComponent();
  69. InitializeComponent2();
  70. this.Icon = PdnInfo.AppIcon;
  71. this.pidRuleDic = pidRuleDic;
  72. this.filePath = path;
  73. this.measureMaintenanceType = measureMaintenanceType;
  74. this.appWorkspace = appWorkspace;
  75. foreach (var item in this.infoModel.pictureNumber)
  76. {
  77. allPictureNum += item;
  78. }
  79. label1.Text = allPictureNum.ToString();
  80. estimatedTimeNum = 5 * allPictureNum;
  81. this._listHoleType = _listHoleType;
  82. this._listHoleTypePos = _listHoleTypePos;
  83. this.Shown += new EventHandler(DialogShown);
  84. string filePath;
  85. if (Program.instance.configModel.Language == 1)
  86. {
  87. filePath = Application.StartupPath + "\\Config\\" + Program.instance.SettingPrefix + "\\basicTreeDataSimple.xml";
  88. }
  89. else
  90. {
  91. filePath = Application.StartupPath + "\\Config\\" + Program.instance.SettingPrefix + "\\basicTreeDataTraditional.xml";
  92. }
  93. if (System.IO.File.Exists(filePath))
  94. {
  95. measureMaintenanceTreeData = XmlSerializeHelper.DESerializer<MeasureMaintenanceTreeData>(FileOperationHelper.ReadStringFromFile(filePath, FileMode.Open));
  96. }
  97. }
  98. private void InitializeComponent2()
  99. {
  100. this.Text = PdnResources.GetString("NewInProgress");
  101. this.groupBox1.Text = PdnResources.GetString("NewOverallProgress");
  102. this.labelZhengchang.Text = PdnResources.GetString("NewResultsNormalNumberSheets") + ":";
  103. this.labelYichang.Text = PdnResources.GetString("NewResultsAbnormalNumberSheets") + ":";
  104. this.labelYiceliangzhangshu.Text = PdnResources.GetString("NewNumberMeasuredSheets") + ":";
  105. this.labelZongzhangshu.Text = PdnResources.GetString("NewNumberPictures") + ":";
  106. this.groupBox2.Text = PdnResources.GetString("NewItemizedProgress");
  107. this.groupBox3.Text = PdnResources.GetString("NewOperation");
  108. this.buttonStop.Text = PdnResources.GetString("NewStopMeasuring");
  109. }
  110. private void DialogShown(object sender, EventArgs e)
  111. {
  112. tokenSource = new CancellationTokenSource();
  113. var token = tokenSource.Token;
  114. this.task = new Task(() =>
  115. {
  116. this.startAnalysis();
  117. }, token);
  118. task.Start();
  119. }
  120. private void BeingExecuted_Load(object sender, EventArgs e)
  121. {
  122. t = DateTime.Now;
  123. labelTime.Size = new System.Drawing.Size(98, 18);
  124. for (int i = 0; i < infoModel.pictureNumber.Length; i++)
  125. {
  126. Label newFenleimingcheng = new Label();
  127. newFenleimingcheng.AutoSize = true;
  128. newFenleimingcheng.Location = new System.Drawing.Point(3, 10 + 40 * i);
  129. newFenleimingcheng.Name = "labelFenlei"+i.ToString();
  130. newFenleimingcheng.Size = new System.Drawing.Size(98, 18);
  131. newFenleimingcheng.TabIndex = 2;
  132. newFenleimingcheng.Text = PdnResources.GetString("NewClassificationName") + ":" + infoModel.text[i];
  133. fenleimingchengs.Add(newFenleimingcheng);
  134. panel1.Controls.Add(fenleimingchengs[i]);
  135. Label newJindu = new Label();
  136. newJindu.AutoSize = true;
  137. newJindu.Location = new System.Drawing.Point(3, 30 + 40 * i);
  138. newJindu.Name = "labelJindu" + i.ToString();
  139. newJindu.Size = new System.Drawing.Size(98, 18);
  140. newJindu.TabIndex = 2;
  141. newJindu.Text = PdnResources.GetString("NewProgress") + ":";
  142. jindus.Add(newJindu);
  143. panel1.Controls.Add(jindus[i]);
  144. }
  145. }
  146. private void timer1_Tick(object sender, EventArgs e)
  147. {
  148. int now = Convert.ToInt32((DateTime.Now - t).TotalSeconds);
  149. int remainingTime = estimatedTimeNum - hasFinishedPic * 5;
  150. int allNum = now + remainingTime;
  151. label2.Text = hasFinishedPic.ToString();
  152. int success = 0;
  153. int fail = 0;
  154. foreach (var item in resultMatList)
  155. {
  156. var i = item.dataInforList.Where(m => m.value == 0).ToList().Count();
  157. if (i == 0)
  158. {
  159. success++;
  160. }
  161. else {
  162. fail++;
  163. }
  164. }
  165. label3.Text = fail.ToString();
  166. label4.Text = success.ToString();
  167. labelTime.Text = PdnResources.GetString("NewTimeUsed") + ":" + now.ToString() + "s," + PdnResources.GetString("NewEstimatedTimeRemaining") + remainingTime.ToString() + "s," + PdnResources.GetString("NewTotalTime") + ":" + allNum + "s。";
  168. if (jindus.Count > 0 && stepNum < jindus.Count)
  169. {
  170. jindus[stepNum].Text = PdnResources.GetString("NewProgress") + ":" + PdnResources.GetString("NewCommon") + infoModel.pictureNumber[stepNum].ToString() + PdnResources.GetString("NewZhang") + ","+ PdnResources.GetString("NewMeasured") + infoModel.yiceliangNumber[stepNum].ToString() + PdnResources.GetString("NewZhang") + ","+ PdnResources.GetString("NewAbnormalResults") + infoModel.failNumber[stepNum].ToString() + PdnResources.GetString("NewZhang") + ","+ PdnResources.GetString("NewNormalResults") + infoModel.successNumber[stepNum].ToString() + PdnResources.GetString("NewZhang") + "。";
  171. }
  172. if (!startS)
  173. {
  174. timer1.Stop();
  175. //this.Close();
  176. buttonStop_Click(null, null);
  177. }
  178. else
  179. {
  180. timer1.Start();
  181. }
  182. }
  183. //點擊停止測量
  184. private void buttonStop_Click(object sender, EventArgs e)
  185. {
  186. if (this.needStop)
  187. {
  188. return;
  189. }
  190. this.needStop = true;
  191. tokenSource.Cancel();
  192. ResultsView newFrom = new ResultsView(infoModel, resultMatList,this.appWorkspace,this, this._listHoleType);
  193. newFrom.StartPosition = FormStartPosition.CenterScreen;
  194. newFrom.ShowDialog();
  195. }
  196. /// <summary>
  197. /// 开始分析
  198. /// </summary>
  199. public void startAnalysis()
  200. {
  201. startS = true;
  202. foreach (var item in infoModel.everyHoleImg)
  203. {
  204. if (this.needStop)
  205. {
  206. break;
  207. }
  208. try
  209. {
  210. string className = InvariantData.path_auto + $".{this._listHoleType[item.Key]}";
  211. foreach (var data in item.Value)
  212. {
  213. if (this.needStop)
  214. {
  215. break;
  216. }
  217. GraphicsList graphicsList = new GraphicsList();
  218. bool isErzhichuli = false;
  219. bool isFanweibuchang = false;
  220. bool isConductive = false;
  221. int thisIndex = item.Value.IndexOf(data);
  222. var currentParam = (AutoMeasureAnalysis)Assembly.Load(InvariantData.auto_Data).CreateInstance(className);
  223. Mat thisMat = data.Clone();
  224. if (thisIndex < this.infoModel.everyHoleImgGraphicsList[stepNum].Count) {
  225. graphicsList = this.infoModel.everyHoleImgGraphicsList[stepNum][thisIndex];
  226. }
  227. if (thisIndex < this.infoModel.everyHoleImgisErzhichuli[stepNum].Count)
  228. {
  229. isErzhichuli = this.infoModel.everyHoleImgisErzhichuli[stepNum][thisIndex];
  230. }
  231. if (thisIndex < this.infoModel.everyHoleImgisFanweibuchang[stepNum].Count)
  232. {
  233. isFanweibuchang = this.infoModel.everyHoleImgisFanweibuchang[stepNum][thisIndex];
  234. }
  235. if (thisIndex < this.infoModel.everyHoleImgisConductive[stepNum].Count)
  236. {
  237. isConductive = this.infoModel.everyHoleImgisConductive[stepNum][thisIndex];
  238. }
  239. bool position = false;
  240. if (_listHoleTypePos.ContainsKey(item.Key))
  241. {
  242. position = _listHoleTypePos[item.Key];
  243. }
  244. currentParam.setErzhichuli(isErzhichuli);
  245. currentParam.setFanweibuchang(isFanweibuchang);
  246. currentParam.setBool(isConductive);
  247. currentParam.setIsNewSuanfa(position);
  248. try
  249. {
  250. tokenSource1 = new CancellationTokenSource();
  251. var token = tokenSource1.Token;
  252. task1 = Task.Run(() =>
  253. {
  254. if (graphicsList.Count == 0)
  255. {
  256. currentParam.Compute(thisMat, false, 0, 0);
  257. }
  258. else
  259. {
  260. int X = (int)graphicsList[0].Rectangle.X;
  261. int Y = (int)graphicsList[0].Rectangle.Y;
  262. int reWidth = (int)graphicsList[0].Rectangle.Width;
  263. int reHeight = (int)graphicsList[0].Rectangle.Height;
  264. Bitmap newBitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(thisMat);
  265. if (graphicsList[0].Rectangle.Contains(new RectangleF(0, 0, newBitmap.Width, newBitmap.Height)))
  266. {
  267. X = 0;
  268. Y = 0;
  269. reWidth = newBitmap.Width;
  270. reHeight = newBitmap.Height;
  271. }
  272. else
  273. {
  274. if (graphicsList[0].Rectangle.IntersectsWith(new RectangleF(0, 0, newBitmap.Width, newBitmap.Height)))
  275. {
  276. RectangleF rectangleF = new RectangleF(graphicsList[0].Rectangle.X, graphicsList[0].Rectangle.Y, graphicsList[0].Rectangle.Width, graphicsList[0].Rectangle.Height);
  277. rectangleF.Intersect(new RectangleF(0, 0, newBitmap.Width, newBitmap.Height));
  278. X = (int)rectangleF.X;
  279. Y = (int)rectangleF.Y;
  280. reWidth = (int)rectangleF.Width;
  281. reHeight = (int)rectangleF.Height;
  282. }
  283. }
  284. Mat mat = new Mat(thisMat, new Rect(X, Y, reWidth, reHeight));
  285. currentParam.Compute(mat, true, X, Y);
  286. if (mat != null && !mat.IsDisposed)
  287. {
  288. mat.Dispose();
  289. }
  290. }
  291. }, token);
  292. task1.Wait(TimeSpan.FromSeconds(30));
  293. }
  294. catch (Exception)
  295. {
  296. }
  297. hasFinishedPic++;
  298. infoModel.yiceliangNumber[stepNum]++;
  299. var wrongList = currentParam.dataInfors.Where(m => m.value == 0).ToList().Count();
  300. if (wrongList == 0)
  301. {
  302. infoModel.successNumber[stepNum]++;
  303. }
  304. else
  305. {
  306. infoModel.failNumber[stepNum]++;
  307. }
  308. Mat mats = thisMat.Clone();
  309. MeasureInfoResultModel measureInfoResultModel = new MeasureInfoResultModel();
  310. measureInfoResultModel.ID = Guid.NewGuid().ToString();
  311. measureInfoResultModel.imgStatus = true;
  312. measureInfoResultModel.OriginalImage = mats;
  313. measureInfoResultModel.parentId = item.Key;
  314. measureInfoResultModel.resultMat = mats;
  315. measureInfoResultModel.allLineNum = currentParam.number;
  316. measureInfoResultModel.successNum = currentParam.success;
  317. measureInfoResultModel.failNum = currentParam.wrongNumber;
  318. measureInfoResultModel.GraphicsListList = graphicsList;
  319. measureInfoResultModel.isConductive = isConductive;
  320. measureInfoResultModel.isFanweibuchang = isFanweibuchang;
  321. measureInfoResultModel.isErzhichuli = isErzhichuli;
  322. measureInfoResultModel.isPosition = position;
  323. List<DataInfor> list = new List<DataInfor>();
  324. foreach (var dataInfo in currentParam.dataInfors)
  325. {
  326. DataInfor dataInfor = new DataInfor();
  327. dataInfor.drawType = dataInfo.drawType;
  328. dataInfor.value = Math.Abs(dataInfo.value);
  329. dataInfor.point1 = dataInfo.point1;
  330. dataInfor.point2 = dataInfo.point2;
  331. dataInfor.name = dataInfo.name;
  332. dataInfor.aliasName = dataInfo.aliasName;
  333. dataInfor.ID = dataInfo.ID;
  334. dataInfor.deleteFlag = dataInfo.deleteFlag;
  335. list.Add(dataInfor);
  336. }
  337. measureInfoResultModel.dataInforList.AddRange(list);
  338. measureInfoResultModel.fileName = infoModel.everyHoleImgName[item.Key][item.Value.IndexOf(data)];
  339. resultMatList.Add(measureInfoResultModel);
  340. if (thisMat != null && !thisMat.IsDisposed)
  341. {
  342. thisMat.Dispose();
  343. }
  344. }
  345. if (task1 != null && task1.IsCompleted)
  346. {
  347. Thread.Sleep(1000);
  348. stepNum++;
  349. }
  350. else if (task1 == null) {
  351. Thread.Sleep(1000);
  352. stepNum++;
  353. }
  354. }
  355. catch (Exception ex)
  356. {
  357. writeLog(ex.Message, PdnResources.GetString("AnalizeError"));
  358. }
  359. finally {
  360. }
  361. }
  362. GC.Collect();
  363. startS = false;
  364. }
  365. private void writeLog(string error, string typeName = "")
  366. {
  367. string filePath = Application.StartupPath + "\\Config\\" + Program.instance.SettingPrefix + "\\ErrorLog";
  368. if (!Directory.Exists(filePath))
  369. {
  370. Directory.CreateDirectory(filePath);
  371. }
  372. using (StreamWriter sw = new StreamWriter(filePath + "\\" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + typeName + "log.txt"))
  373. {
  374. sw.WriteLine(error);
  375. }
  376. }
  377. }
  378. }