using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CvBase;
using CWindowTool;
using HalconDotNet;
namespace CvImageTool.IntersectionLL
{
public class IntersectionLL : BaseImageOpt
{
private string cProcessSettingFilePath = null;
private string cImageOptSettingFilePath = null;
private IntersectionLLForm intersectionLLForm = null;
private IntersectionLLTool intersectionLLTool = null;
private int CProcessIndex;
public HObject MeasureLine = null;
public HObject ResultLine = null;
public HTuple IntersectionPoint;
public HTuple Angle;
public List<BaseProcess> processes;
public CWindowTool.CWindows[] cCWindows;
public IntersectionLL(int processIndex, int imageOptIndex, string description, EnumOrStruct.RunProcessOrign runProcessOrign)
{
switch (runProcessOrign)
{
case EnumOrStruct.RunProcessOrign.MainProcess:
processes = Refrence.Instance.baseProcesses.ToList();
cCWindows = Refrence.Instance.CWindows;
//获取流程配置文件和算法参数文件路径
cProcessSettingFilePath = Path.Combine(AppSetting.Instance.SubAppSettingRootPath, processes[processIndex].Name);
if (!Directory.Exists(cProcessSettingFilePath)) Directory.CreateDirectory(cProcessSettingFilePath);
cImageOptSettingFilePath = Path.Combine(cProcessSettingFilePath, description) + ".json";
cProcessSettingFilePath = Path.Combine(cProcessSettingFilePath, AppSetting.Instance.ProcessFormSettingName);
break;
//case EnumOrStruct.RunProcessOrign.CalibrationProcess:
// processes = Refrence.Instance.calProcesses.ToList();
// cCWindows = Refrence.Instance.calCWindows;
// cProcessSettingFilePath = Path.Combine(AppSetting.Instance.CalGroupedProcessDir, processes[processIndex].Name);
// if (!Directory.Exists(cProcessSettingFilePath)) Directory.CreateDirectory(cProcessSettingFilePath);
// cImageOptSettingFilePath = Path.Combine(cProcessSettingFilePath, description) + ".json";
// cProcessSettingFilePath = Path.Combine(cProcessSettingFilePath, AppSetting.Instance.ProcessSettingFileName);
// break;
}
this.Description = description;
this.CProcessIndex = processIndex;
this.ImageOptIndex = imageOptIndex;
//将当前算法的输入输出参数添加到公共资源类的输入输出列表
processes[processIndex].BaseImageOutParams.Add(new IntersectionLLOutput());
//创建算法抓圆图像处理工具
intersectionLLTool = new IntersectionLLTool(CProcessIndex, ImageOptIndex, cProcessSettingFilePath, cImageOptSettingFilePath, processes, cCWindows);
//创建算法抓圆窗体
intersectionLLForm = new IntersectionLLForm(CProcessIndex, ImageOptIndex, cProcessSettingFilePath, cImageOptSettingFilePath, description, processes, cCWindows);
}
/// </summary>
public override string Name { get; set; }
/// <summary>
/// 算法描述(参数文件的文件名/流程算法显示名称)
/// </summary>
public override string Description { get; set; }
/// <summary>
/// 英文名称,用于配置参数的读写和区分
/// </summary>
public override string EnglishName { get; set; }
/// <summary>
/// 算法所属流程名称
/// </summary>
public override string ParentProcessName { get; set; }
/// <summary>
/// 在所属流程中其索引
/// </summary>
public override int ImageOptIndex { get; set; }
/// <summary>
/// 算法运行
/// </summary>
public override void Running(bool isTest = false)
{
ReadFile();
#region 处理图像参数赋值
string[] imageSelects = intersectionLLTool.imageSelect.Split('_');
if (imageSelects.Length != 2) return;
if (imageSelects[0] == "相机")
{
if (imageSelects[1] == "灰度图")
{
intersectionLLTool.Image = processes[CProcessIndex].ImageGray;
}
else
{
intersectionLLTool.Image = processes[CProcessIndex].ImageHeight;
}
}
else
{
ProcessTool.Instance.FindImageOpt(processes[CProcessIndex], imageSelects[0], out BaseImageOpt paramByImageOpt1, out int paramByImageOptIndex1);
if (paramByImageOptIndex1 >= 0)
{
processes[CProcessIndex].BaseImageOutParams[paramByImageOptIndex1].GetHObjectValue(imageSelects[1], out HObject value);
intersectionLLTool.Image = value;
}
else
{
return;
}
}
#endregion
intersectionLLTool.Running(out MeasureLine, out ResultLine, out IntersectionPoint, out Angle);
#region 输出参数赋值
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].Dispose();
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].MeasureContours = MeasureLine;
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].ResultRegions = ResultLine;
if (IntersectionPoint.Length > 0)
{
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].CenterPoint = IntersectionPoint;
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].Angle = Angle;
}
#endregion
#region 添加显示
if (intersectionLLTool.isShowMeasureLines)
{
CObject cObject1 = new CObject();
cObject1._Object = MeasureLine;
cObject1._Color = "blue";
cObject1._Draw = false;
cCWindows[CProcessIndex].AObject.Add(cObject1);
}
if (intersectionLLTool.isShowResultLine)
{
CObject cObject1 = new CObject();
cObject1._Object = ResultLine;
cObject1._Color = "green";
cObject1._Draw = false;
cCWindows[CProcessIndex].AObject.Add(cObject1);
}
if (intersectionLLTool.isShowResult && IntersectionPoint.Length > 0)
{
CText cText01 = new CText();
cText01._Text = "X:" + IntersectionPoint[1].D.ToString("0.00") + " Y:" +
IntersectionPoint[0].D.ToString("0.00") + " A:" + Angle.D.ToString("0.00");
cText01._X = 5;
cText01._Y = 5;
cText01._Color = "blue";
cCWindows[CProcessIndex].AText.Add(cText01);
}
#endregion
cCWindows[CProcessIndex].ADisplay();
}
/// <summary>
/// 显示算法相关界面
/// </summary>
public override void show(int processIndex, int imageIndex)
{
intersectionLLForm.ShowDialog();
}
/// <summary>
/// 读取配置文件
/// </summary>
/// <param name="cProcessSettingFilePath"></param>
/// <param name="cImgeOptSettingFilePath"></param>
/// <returns></returns>
public override bool ReadFile()
{
return intersectionLLTool.ReadFile(Description);
}
}
}