123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- using OpenCvSharp;
- using Resources;
- using SmartCoalApplication.Annotation;
- using SmartCoalApplication.Base.AutoMeasure;
- using SmartCoalApplication.Base.CommTool;
- using SmartCoalApplication.Base.MeasureModel;
- using SmartCoalApplication.Resources;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace SmartCoalApplication.AutomaticMeasurement
- {
- public partial class BeingExecuted : Form
- {
- private MeasureInfoModel infoModel = new MeasureInfoModel();
- private List<Label> fenleimingchengs = new List<Label>();
- private List<Label> jindus = new List<Label>();
- private DateTime t;
- private DateTime dateTime = DateTime.Now;
- /// <summary>
- /// 图片总数
- /// </summary>
- private int allPictureNum = 0;
- /// <summary>
- /// 预估总时长(1张图片暂定5s)
- /// </summary>
- private int estimatedTimeNum = 0;
- /// <summary>
- /// 已分析完的图片数量
- /// </summary>
- private int hasFinishedPic = 0;
- /// <summary>
- /// 进行到第几步
- /// </summary>
- private int stepNum = 0;
- /// <summary>
- /// 进行到第几步
- /// </summary>
- private bool startS = true;
- private Task task;
- private CancellationTokenSource tokenSource;
- private AppWorkspace appWorkspace;
- private Dictionary<string, string> _listHoleType;
- private Dictionary<string, bool> _listHoleTypePos;
- private MeasureMaintenanceTreeData measureMaintenanceTreeData;
- public MeasureMaintenanceType measureMaintenanceType;
- public Dictionary<string, Dictionary<string, double>> pidRuleDic;
- public Task task1;
- private CancellationTokenSource tokenSource1;
- public string filePath;
- private bool needStop = false;
- /// <summary>
- /// 分析结果图
- /// </summary>
- private List<MeasureInfoResultModel> resultMatList = new List<MeasureInfoResultModel>();
- internal BeingExecuted(MeasureInfoModel infoModel,AppWorkspace appWorkspace, Dictionary<string, string> _listHoleType,MeasureMaintenanceType measureMaintenanceType, Dictionary<string, Dictionary<string, double>> pidRuleDic,string path, Dictionary<string, bool> _listHoleTypePos)
- {
- this.infoModel = infoModel;
- InitializeComponent();
- InitializeComponent2();
- this.Icon = PdnInfo.AppIcon;
- this.pidRuleDic = pidRuleDic;
- this.filePath = path;
- this.measureMaintenanceType = measureMaintenanceType;
- this.appWorkspace = appWorkspace;
- foreach (var item in this.infoModel.pictureNumber)
- {
- allPictureNum += item;
- }
- label1.Text = allPictureNum.ToString();
- estimatedTimeNum = 5 * allPictureNum;
- this._listHoleType = _listHoleType;
- this._listHoleTypePos = _listHoleTypePos;
- this.Shown += new EventHandler(DialogShown);
- string filePath;
- if (Program.instance.configModel.Language == 1)
- {
- filePath = Application.StartupPath + "\\Config\\" + Program.instance.SettingPrefix + "\\basicTreeDataSimple.xml";
- }
- else
- {
- filePath = Application.StartupPath + "\\Config\\" + Program.instance.SettingPrefix + "\\basicTreeDataTraditional.xml";
- }
- if (System.IO.File.Exists(filePath))
- {
- measureMaintenanceTreeData = XmlSerializeHelper.DESerializer<MeasureMaintenanceTreeData>(FileOperationHelper.ReadStringFromFile(filePath, FileMode.Open));
- }
- }
- private void InitializeComponent2()
- {
- this.Text = PdnResources.GetString("NewInProgress");
- this.groupBox1.Text = PdnResources.GetString("NewOverallProgress");
- this.labelZhengchang.Text = PdnResources.GetString("NewResultsNormalNumberSheets") + ":";
- this.labelYichang.Text = PdnResources.GetString("NewResultsAbnormalNumberSheets") + ":";
- this.labelYiceliangzhangshu.Text = PdnResources.GetString("NewNumberMeasuredSheets") + ":";
- this.labelZongzhangshu.Text = PdnResources.GetString("NewNumberPictures") + ":";
- this.groupBox2.Text = PdnResources.GetString("NewItemizedProgress");
- this.groupBox3.Text = PdnResources.GetString("NewOperation");
- this.buttonStop.Text = PdnResources.GetString("NewStopMeasuring");
- }
- private void DialogShown(object sender, EventArgs e)
- {
- tokenSource = new CancellationTokenSource();
- var token = tokenSource.Token;
- this.task = new Task(() =>
- {
- this.startAnalysis();
- }, token);
- task.Start();
- }
- private void BeingExecuted_Load(object sender, EventArgs e)
- {
- t = DateTime.Now;
- labelTime.Size = new System.Drawing.Size(98, 18);
- for (int i = 0; i < infoModel.pictureNumber.Length; i++)
- {
- Label newFenleimingcheng = new Label();
- newFenleimingcheng.AutoSize = true;
- newFenleimingcheng.Location = new System.Drawing.Point(3, 10 + 40 * i);
- newFenleimingcheng.Name = "labelFenlei"+i.ToString();
- newFenleimingcheng.Size = new System.Drawing.Size(98, 18);
- newFenleimingcheng.TabIndex = 2;
- newFenleimingcheng.Text = PdnResources.GetString("NewClassificationName") + ":" + infoModel.text[i];
- fenleimingchengs.Add(newFenleimingcheng);
- panel1.Controls.Add(fenleimingchengs[i]);
- Label newJindu = new Label();
- newJindu.AutoSize = true;
- newJindu.Location = new System.Drawing.Point(3, 30 + 40 * i);
- newJindu.Name = "labelJindu" + i.ToString();
- newJindu.Size = new System.Drawing.Size(98, 18);
- newJindu.TabIndex = 2;
- newJindu.Text = PdnResources.GetString("NewProgress") + ":";
- jindus.Add(newJindu);
- panel1.Controls.Add(jindus[i]);
- }
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- int now = Convert.ToInt32((DateTime.Now - t).TotalSeconds);
- int remainingTime = estimatedTimeNum - hasFinishedPic * 5;
- int allNum = now + remainingTime;
- label2.Text = hasFinishedPic.ToString();
- int success = 0;
- int fail = 0;
- foreach (var item in resultMatList)
- {
- var i = item.dataInforList.Where(m => m.value == 0).ToList().Count();
- if (i == 0)
- {
- success++;
- }
- else {
- fail++;
- }
- }
- label3.Text = fail.ToString();
- label4.Text = success.ToString();
- labelTime.Text = PdnResources.GetString("NewTimeUsed") + ":" + now.ToString() + "s," + PdnResources.GetString("NewEstimatedTimeRemaining") + remainingTime.ToString() + "s," + PdnResources.GetString("NewTotalTime") + ":" + allNum + "s。";
- if (jindus.Count > 0 && stepNum < jindus.Count)
- {
- 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") + "。";
- }
- if (!startS)
- {
- timer1.Stop();
- //this.Close();
- buttonStop_Click(null, null);
- }
- else
- {
- timer1.Start();
- }
- }
- //點擊停止測量
- private void buttonStop_Click(object sender, EventArgs e)
- {
- if (this.needStop)
- {
- return;
- }
- this.needStop = true;
- tokenSource.Cancel();
- ResultsView newFrom = new ResultsView(infoModel, resultMatList,this.appWorkspace,this, this._listHoleType);
- newFrom.StartPosition = FormStartPosition.CenterScreen;
- newFrom.ShowDialog();
- }
- /// <summary>
- /// 开始分析
- /// </summary>
- public void startAnalysis()
- {
- startS = true;
- foreach (var item in infoModel.everyHoleImg)
- {
- if (this.needStop)
- {
- break;
- }
- try
- {
- string className = InvariantData.path_auto + $".{this._listHoleType[item.Key]}";
- foreach (var data in item.Value)
- {
- if (this.needStop)
- {
- break;
- }
- GraphicsList graphicsList = new GraphicsList();
- bool isErzhichuli = false;
- bool isFanweibuchang = false;
- bool isConductive = false;
- int thisIndex = item.Value.IndexOf(data);
- var currentParam = (AutoMeasureAnalysis)Assembly.Load(InvariantData.auto_Data).CreateInstance(className);
- Mat thisMat = data.Clone();
- if (thisIndex < this.infoModel.everyHoleImgGraphicsList[stepNum].Count) {
- graphicsList = this.infoModel.everyHoleImgGraphicsList[stepNum][thisIndex];
- }
- if (thisIndex < this.infoModel.everyHoleImgisErzhichuli[stepNum].Count)
- {
- isErzhichuli = this.infoModel.everyHoleImgisErzhichuli[stepNum][thisIndex];
- }
- if (thisIndex < this.infoModel.everyHoleImgisFanweibuchang[stepNum].Count)
- {
- isFanweibuchang = this.infoModel.everyHoleImgisFanweibuchang[stepNum][thisIndex];
- }
- if (thisIndex < this.infoModel.everyHoleImgisConductive[stepNum].Count)
- {
- isConductive = this.infoModel.everyHoleImgisConductive[stepNum][thisIndex];
- }
- bool position = false;
- if (_listHoleTypePos.ContainsKey(item.Key))
- {
- position = _listHoleTypePos[item.Key];
- }
- currentParam.setErzhichuli(isErzhichuli);
- currentParam.setFanweibuchang(isFanweibuchang);
- currentParam.setBool(isConductive);
- currentParam.setIsNewSuanfa(position);
- try
- {
- tokenSource1 = new CancellationTokenSource();
- var token = tokenSource1.Token;
- task1 = Task.Run(() =>
- {
- if (graphicsList.Count == 0)
- {
- currentParam.Compute(thisMat, false, 0, 0);
- }
- else
- {
- int X = (int)graphicsList[0].Rectangle.X;
- int Y = (int)graphicsList[0].Rectangle.Y;
- int reWidth = (int)graphicsList[0].Rectangle.Width;
- int reHeight = (int)graphicsList[0].Rectangle.Height;
- Bitmap newBitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(thisMat);
- if (graphicsList[0].Rectangle.Contains(new RectangleF(0, 0, newBitmap.Width, newBitmap.Height)))
- {
- X = 0;
- Y = 0;
- reWidth = newBitmap.Width;
- reHeight = newBitmap.Height;
- }
- else
- {
- if (graphicsList[0].Rectangle.IntersectsWith(new RectangleF(0, 0, newBitmap.Width, newBitmap.Height)))
- {
- RectangleF rectangleF = new RectangleF(graphicsList[0].Rectangle.X, graphicsList[0].Rectangle.Y, graphicsList[0].Rectangle.Width, graphicsList[0].Rectangle.Height);
- rectangleF.Intersect(new RectangleF(0, 0, newBitmap.Width, newBitmap.Height));
- X = (int)rectangleF.X;
- Y = (int)rectangleF.Y;
- reWidth = (int)rectangleF.Width;
- reHeight = (int)rectangleF.Height;
- }
- }
- Mat mat = new Mat(thisMat, new Rect(X, Y, reWidth, reHeight));
- currentParam.Compute(mat, true, X, Y);
- if (mat != null && !mat.IsDisposed)
- {
- mat.Dispose();
- }
- }
- }, token);
- task1.Wait(TimeSpan.FromSeconds(30));
- }
- catch (Exception)
- {
- }
- hasFinishedPic++;
- infoModel.yiceliangNumber[stepNum]++;
- var wrongList = currentParam.dataInfors.Where(m => m.value == 0).ToList().Count();
- if (wrongList == 0)
- {
- infoModel.successNumber[stepNum]++;
- }
- else
- {
- infoModel.failNumber[stepNum]++;
- }
- Mat mats = thisMat.Clone();
- MeasureInfoResultModel measureInfoResultModel = new MeasureInfoResultModel();
- measureInfoResultModel.ID = Guid.NewGuid().ToString();
- measureInfoResultModel.imgStatus = true;
- measureInfoResultModel.OriginalImage = mats;
- measureInfoResultModel.parentId = item.Key;
- measureInfoResultModel.resultMat = mats;
- measureInfoResultModel.allLineNum = currentParam.number;
- measureInfoResultModel.successNum = currentParam.success;
- measureInfoResultModel.failNum = currentParam.wrongNumber;
- measureInfoResultModel.GraphicsListList = graphicsList;
- measureInfoResultModel.isConductive = isConductive;
- measureInfoResultModel.isFanweibuchang = isFanweibuchang;
- measureInfoResultModel.isErzhichuli = isErzhichuli;
- measureInfoResultModel.isPosition = position;
- List<DataInfor> list = new List<DataInfor>();
- foreach (var dataInfo in currentParam.dataInfors)
- {
- DataInfor dataInfor = new DataInfor();
- dataInfor.drawType = dataInfo.drawType;
- dataInfor.value = Math.Abs(dataInfo.value);
- dataInfor.point1 = dataInfo.point1;
- dataInfor.point2 = dataInfo.point2;
- dataInfor.name = dataInfo.name;
- dataInfor.aliasName = dataInfo.aliasName;
- dataInfor.ID = dataInfo.ID;
- dataInfor.deleteFlag = dataInfo.deleteFlag;
- list.Add(dataInfor);
- }
- measureInfoResultModel.dataInforList.AddRange(list);
- measureInfoResultModel.fileName = infoModel.everyHoleImgName[item.Key][item.Value.IndexOf(data)];
- resultMatList.Add(measureInfoResultModel);
- if (thisMat != null && !thisMat.IsDisposed)
- {
- thisMat.Dispose();
- }
- }
-
- if (task1 != null && task1.IsCompleted)
- {
- Thread.Sleep(1000);
- stepNum++;
- }
- else if (task1 == null) {
- Thread.Sleep(1000);
- stepNum++;
- }
- }
- catch (Exception ex)
- {
- writeLog(ex.Message, PdnResources.GetString("AnalizeError"));
- }
- finally {
- }
- }
- GC.Collect();
- startS = false;
- }
- private void writeLog(string error, string typeName = "")
- {
- string filePath = Application.StartupPath + "\\Config\\" + Program.instance.SettingPrefix + "\\ErrorLog";
- if (!Directory.Exists(filePath))
- {
- Directory.CreateDirectory(filePath);
- }
- using (StreamWriter sw = new StreamWriter(filePath + "\\" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + typeName + "log.txt"))
- {
- sw.WriteLine(error);
- }
- }
- }
- }
|