using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CvBase;
using System.IO;
using HalconDotNet;
using CWindowTool;
using Newtonsoft.Json;
namespace CvImageTool.AngleLx
{
public class AngleLxTool
{
public FileINI INI = new FileINI();
public AngleLxParam angleLxParam; //匹配参数
private string cProcessSettingFilePath; //当前流程配置文件的路径
private string cImageOptSettingFilePath; //当前算法参数文件路径
public string imageSelect = ""; //图像输入参数选择项
public string line1Select = ""; //直线1输入参数选择项
private int cProcessIndex = -1; //当前归属流程的索引
private int cImageOptIndex = -1; //当前归属算法的索引
public bool isShowResult = false;
public bool isShowMeasureLines = false;
public HObject Image;
public HTuple Line01;
private List<BaseProcess> process;
private CWindows[] cCWindows;
public AngleLxTool(int cProcessIndex, int cImageOptIndex,
string cProcessSettingFilePath, string cImageOptSettingFilePath,
List<BaseProcess> processes, CWindows[] cWindows)
{
this.cProcessSettingFilePath = cProcessSettingFilePath;
this.cImageOptSettingFilePath = cImageOptSettingFilePath;
this.cProcessIndex = cProcessIndex;
this.cImageOptIndex = cImageOptIndex;
this.process = processes;
this.cCWindows = cWindows;
angleLxParam = new AngleLxParam();
Image = new HObject();
}
/// <summary>
/// 读取配置文件
/// </summary>
/// <param name="cProcessSettingFilePath"></param>
/// <param name="cImgeOptSettingFilePath"></param>
/// <returns></returns>
public bool ReadFile(string cProcessDescription)
{
imageSelect = ""; line1Select = "";
if (!File.Exists(cProcessSettingFilePath) || !File.Exists(cImageOptSettingFilePath)) return false;
imageSelect = FileINI.ReadValueFromIniFile(cProcessDescription, "Image", "", cProcessSettingFilePath);
line1Select = FileINI.ReadValueFromIniFile(cProcessDescription, "Line1", "", cProcessSettingFilePath);
isShowResult = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowResult", "", cProcessSettingFilePath));
isShowMeasureLines = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowMeasureLines", "", cProcessSettingFilePath));
angleLxParam = JsonConvert.DeserializeObject<AngleLxParam>(File.ReadAllText(cImageOptSettingFilePath));
return true;
}
/// <summary>
/// 写配置文件
/// </summary>
/// <param name="cProcessSettingFilePath"></param>
/// <param name="cImgeOptSettingFilePath"></param>
/// <returns></returns>
public bool WriteFile(string cProcessDescription)
{
FileINI.WriteValueFromIniFile(cProcessDescription, "Image", imageSelect, cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "Line1", line1Select, cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "isShowResult", isShowResult.ToString(), cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "isShowMeasureLines", isShowMeasureLines.ToString(), cProcessSettingFilePath);
var json = JsonConvert.SerializeObject(angleLxParam, Formatting.Indented);
File.WriteAllText(cImageOptSettingFilePath, json);
return true;
}
public void Running(out HObject MearsureLine, out HTuple angle)
{
MearsureLine = new HObject(); angle = new HTuple();
//获取直线1
string[] imageOptAndParamNameL1 = line1Select.Split('_');
ProcessTool.Instance.FindImageOpt(process[cProcessIndex], imageOptAndParamNameL1[0], out BaseImageOpt byImageOpt, out int byIndex);
if (byIndex >= 0)
process[cProcessIndex].BaseImageOutParams[byIndex].GetHTupleValue(imageOptAndParamNameL1[1], out Line01);
else
Line01 = null;
if (Line01 == null || Line01.Length < 4) return;
HOperatorSet.AngleLx(Line01[0], Line01[1], Line01[2], Line01[3], out angle);
angle = angle.D / Math.PI * 180.0;
//计算直线距离
//HOperatorSet.GenRegionLine(out MearsureLine, Line01[0], Line01[1], Line01[2], Line01[3]);
HTuple lRow = new HTuple();
HTuple lCol = new HTuple();
lRow.Append(Line01[0]);
lRow.Append(Line01[2]);
lCol.Append(Line01[1]);
lCol.Append(Line01[3]);
HOperatorSet.GenContourPolygonXld(out MearsureLine, lRow, lCol);
}
}
}