using Autofac;
|
using Bro.Common.Helper;
|
using Bro.Common.Interface;
|
using Bro.Common.Model;
|
using System.ComponentModel;
|
using System.Drawing.Design;
|
using System.Reflection.Metadata.Ecma335;
|
|
namespace Bro.M141.Process
|
{
|
public class CheckPoint : IComplexDisplay
|
{
|
[Browsable(false)]
|
public string Id { get; set; } = Guid.NewGuid().ToString();
|
|
[Category("检测点位")]
|
[Description("点位名称")]
|
[DisplayName("点位名称")]
|
public string Name { get; set; } = "";
|
|
[Category("检测点位")]
|
[Description("点位坐标,检测点位的运动坐标")]
|
[DisplayName("点位坐标")]
|
[TypeConverter(typeof(ExpandableObjectConverter))]
|
public CustomizedPoint PointPosition { get; set; } = new CustomizedPoint();
|
|
[Category("检测点位")]
|
[Description("点位坐标,检测点位的Z轴坐标")]
|
[DisplayName("点位坐标Z")]
|
public double PointZ { get; set; }
|
|
[Category("标定设置")]
|
[Description("该检测点位的标定矩阵,图像UV坐标转换为大板坐标系XY的标定矩阵")]
|
[DisplayName("点位标定矩阵")]
|
[TypeConverter(typeof(SimpleCollectionConvert<double>))]
|
[Editor(typeof(SimpleCollectionEditor<double>), typeof(UITypeEditor))]
|
public List<double> Matrix { get; set; } = new List<double>();
|
|
[Category("启用配置")]
|
[Description("true:启用该点位 false:禁用该点位")]
|
[DisplayName("点位启用标志")]
|
public bool IsEnabled { get; set; } = true;
|
|
public string GetDisplayText()
|
{
|
return $"{(IsEnabled ? "" : "禁用 ")}{Name} {PointPosition.GetDisplayText()}";
|
}
|
}
|
|
public enum ContourEdge
|
{
|
X,
|
Y,
|
Z
|
}
|
public class ContourPoint : IComplexDisplay, IImportFromFileInEditor
|
{
|
[Category("点位配置")]
|
[Description("点位名称")]
|
[DisplayName("点位名称")]
|
public string Name { get; set; } = "";
|
|
[Category("点位数据")]
|
[Description("X数据")]
|
[DisplayName("X数据")]
|
public double X { get; set; } = 0;
|
|
[Category("点位数据")]
|
[Description("Y数据")]
|
[DisplayName("Y数据")]
|
public double Y { get; set; } = 0;
|
|
[Browsable(false)]
|
public double ActualZ { get; set; } = -999;
|
|
|
[Category("点位配置")]
|
[Description("算法输出点位索引,从0开始")]
|
[DisplayName("索引")]
|
public int index { get; set; } = 0;
|
|
|
|
[Category("补偿值设置")]
|
[Description("Z补偿值")]
|
[DisplayName("Z补偿值")]
|
public double compensateZ { get; set; } = 0;
|
|
|
|
public ContourPoint Copy()
|
{
|
ContourPoint point = new ContourPoint();
|
|
point.Name = Name;
|
point.X = X;
|
point.Y = Y;
|
point.ActualZ = -999;
|
point.compensateZ = compensateZ;
|
return point;
|
}
|
|
[Browsable(false)]
|
public double Error { get; set; } = 0;
|
|
public string GetDisplayText()
|
{
|
return $"{Name} 序号{index} 数据{X},{Y}";
|
}
|
|
|
public IImportFromFileInEditor GetImportObject(string data, out string msg)
|
{
|
msg = "";
|
var dataList = data.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
if (dataList.Length < 5)
|
{
|
msg = $"每行至少包含5段内容:点位名称,索引,X,Y,Z补偿值 {data}";
|
throw new Exception(msg);
|
}
|
ContourPoint ret = new ContourPoint();
|
try
|
{
|
ret.Name = dataList[0];
|
ret.index = int.Parse(dataList[1]);
|
ret.X = double.Parse(dataList[2]);
|
ret.Y = double.Parse(dataList[3]);
|
ret.compensateZ = double.Parse(dataList[4]);
|
}
|
catch
|
{
|
msg = $"数据内容:点位名称,索引,X,Y,Z补偿值 {data}";
|
throw new Exception(msg + "数据类型异常");
|
}
|
|
return ret;
|
}
|
|
public bool ICSVOutput(object o)
|
{
|
try
|
{
|
if (o is List<ContourPoint> Pl)
|
{
|
SaveFileDialog saveFileDialog = new SaveFileDialog();
|
saveFileDialog.Filter = "CSV files (*.csv)|*.csv"; // 设置文件过滤器,只显示CSV文件
|
saveFileDialog.Title = "Save CSV File"; // 设置对话框标题
|
saveFileDialog.FileName = "ContourPoints"; // 默认文件名
|
saveFileDialog.DefaultExt = "csv"; // 默认文件扩展名
|
string filePath = "";
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
{
|
filePath = saveFileDialog.FileName; // 获取用户选择的文件路径
|
}
|
else
|
{
|
return false;
|
}
|
|
using (StreamWriter writer = new StreamWriter(filePath))
|
{
|
// 写入标题行
|
writer.WriteLine("点位名称,索引,X,Y,Z补偿值");
|
// 写入数据行
|
foreach (var row in Pl)
|
{
|
writer.WriteLine($"{row.Name},{row.index},{row.X},{row.Y},{row.compensateZ}");
|
}
|
}
|
}
|
}
|
catch
|
{
|
|
}
|
return true;
|
}
|
|
|
|
|
|
|
|
}
|
|
public class MeasureItemBind : IComplexDisplay, IHalconToolPath, IImportFromFileInEditor
|
{
|
[Category("检测标准")]
|
[DisplayName("\t检测标准")]
|
[Description("检测标准选择")]
|
[TypeConverter(typeof(SpecCodeSelectorConverter))]
|
public string SpecCode { get; set; }
|
|
[Category("算法配置")]
|
[DisplayName("算法路径")]
|
[Description("算法路径")]
|
[Editor(typeof(FileDialogEditor), typeof(UITypeEditor))]
|
public string AlgorithemPath { get; set; }
|
|
[Category("检测点位")]
|
[DisplayName("检测点位集合")]
|
[Description("该检测项所涉及的所有检测点位集合")]
|
[TypeConverter(typeof(ComplexObjectConvert))]
|
[Editor(typeof(ComplexCollectionEditor<MeasurePointSelector>), typeof(UITypeEditor))]
|
public List<MeasurePointSelector> MeasurePointNameCollection { get; set; } = new List<MeasurePointSelector>();
|
|
|
public IImportFromFileInEditor GetImportObject(string data, out string msg)
|
{
|
msg = "";
|
var dataList = data.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
|
if (dataList.Count < 3)
|
{
|
msg = $"每行至少包含3段内容:检测标准,检测点位,轮廓 {data}";
|
throw new Exception(msg);
|
}
|
MeasureItemBind ret = new MeasureItemBind();
|
try
|
{
|
using (var scope = GlobalVar.Container.BeginLifetimeScope())
|
{
|
var config = scope.Resolve<IProcessConfig>();
|
if (config is M141Config iConfig)
|
{
|
if (iConfig.MeasureItemBinds.Any(u => u.SpecCode == dataList[0]))
|
{
|
ret = iConfig.MeasureItemBinds.FirstOrDefault(u => u.SpecCode == dataList[0]);
|
|
MeasurePointSelector mes = new MeasurePointSelector();
|
mes.MeasurePointName = dataList[1];
|
|
if (dataList[2].ToUpper() == "X")
|
{
|
mes.ContourEdge = ContourEdge.X;
|
}
|
else if (dataList[2].ToUpper() == "Y")
|
{
|
mes.ContourEdge = ContourEdge.Y;
|
}
|
else if (dataList[2].ToUpper() == "Z")
|
{
|
mes.ContourEdge = ContourEdge.Z;
|
}
|
else
|
{
|
throw new Exception("检测标准,检测点位,轮廓 " + data + " 轮廓选择异常");
|
}
|
ret.MeasurePointNameCollection.Add(mes);
|
|
iConfig.MeasureItemBinds.Remove(ret);
|
}
|
else
|
{
|
if (!iConfig.GetSpecList().Any(u => u.Code == dataList[0]))
|
{
|
throw new Exception("检测标准,检测点位,轮廓 " + data + "不存在于点位集合中,不可录入");
|
}
|
|
if (!iConfig.MeasurePointCollection.Any(u => u.Name == dataList[1]))
|
{
|
throw new Exception("检测标准,检测点位,轮廓 " + data + "不存在于点位集合中,不可录入");
|
}
|
|
MeasurePointSelector mes = new MeasurePointSelector();
|
ret.SpecCode = dataList[0];
|
mes.MeasurePointName = dataList[1];
|
|
if (dataList[2].ToUpper() == "X")
|
{
|
mes.ContourEdge = ContourEdge.X;
|
}
|
else if (dataList[2].ToUpper() == "Y")
|
{
|
mes.ContourEdge = ContourEdge.Y;
|
}
|
else if (dataList[2].ToUpper() == "Z")
|
{
|
mes.ContourEdge = ContourEdge.Z;
|
}
|
else
|
{
|
throw new Exception("检测标准,检测点位,轮廓 " + data + " 轮廓选择异常");
|
}
|
ret.MeasurePointNameCollection.Add(mes);
|
|
}
|
}
|
}
|
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
return ret;
|
}
|
|
public bool ICSVOutput(object o)
|
{
|
try
|
{
|
if (o is List<MeasureItemBind> Pl)
|
{
|
SaveFileDialog saveFileDialog = new SaveFileDialog();
|
saveFileDialog.Filter = "CSV files (*.csv)|*.csv"; // 设置文件过滤器,只显示CSV文件
|
saveFileDialog.Title = "Save CSV File"; // 设置对话框标题
|
saveFileDialog.FileName = "MeasureItemBinds"; // 默认文件名
|
saveFileDialog.DefaultExt = "csv"; // 默认文件扩展名
|
string filePath = "";
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
{
|
filePath = saveFileDialog.FileName; // 获取用户选择的文件路径
|
}
|
else
|
{
|
return false;
|
}
|
|
using (StreamWriter writer = new StreamWriter(filePath))
|
{
|
// 写入标题行
|
writer.WriteLine("检测标准,检测点位,轮廓");
|
// 写入数据行
|
foreach (var row in Pl)
|
{
|
foreach (var item in row.MeasurePointNameCollection)
|
{
|
string temC = "X";
|
if (item.ContourEdge == ContourEdge.X)
|
{
|
temC = "X";
|
}
|
else if (item.ContourEdge == ContourEdge.Y)
|
{
|
temC = "Y";
|
}
|
else if (item.ContourEdge == ContourEdge.Z)
|
{
|
temC = "Z";
|
}
|
writer.WriteLine($"{row.SpecCode},{item.MeasurePointName},{temC}");
|
}
|
}
|
}
|
}
|
}
|
catch
|
{
|
|
}
|
return true;
|
}
|
|
public string GetDisplayText()
|
{
|
return $"{SpecCode} {string.Join(",", MeasurePointNameCollection.Select(u => u.GetDisplayText()))} {AlgorithemPath}";
|
}
|
|
public List<string> GetHalconToolPathList()
|
{
|
return new List<string>() { AlgorithemPath };
|
}
|
}
|
|
public class MeasurePointSelector : IComplexDisplay
|
{
|
[Category("检测点位")]
|
[Description("检测点位")]
|
[DisplayName("检测点位")]
|
[TypeConverter(typeof(MeasurePointNameSelectorConvertor))]
|
public string MeasurePointName { get; set; } = "";
|
|
[Category("检测点位")]
|
[Description("轮廓选择。选择参与计算的数据")]
|
[DisplayName("轮廓选择")]
|
public ContourEdge ContourEdge { get; set; } = ContourEdge.Z;
|
|
|
public string GetDisplayText()
|
{
|
return $"{MeasurePointName}";
|
}
|
}
|
|
public class MeasurePointNameSelectorConvertor : ComboBoxItemTypeConvert
|
{
|
public override Dictionary<string, string> GetConvertDict(ITypeDescriptorContext context)
|
{
|
Dictionary<string, string> dict = new Dictionary<string, string>();
|
|
using (var scope = GlobalVar.Container.BeginLifetimeScope())
|
{
|
var config = scope.Resolve<IProcessConfig>();
|
if (config is M141Config iConfig)
|
{
|
iConfig.MeasurePointCollection.ForEach(u =>
|
{
|
dict[u.Name] = u.Name;
|
});
|
}
|
}
|
dict[""] = "未指定";
|
|
return dict;
|
}
|
}
|
}
|