上传界面显示

This commit is contained in:
2025-03-16 13:11:08 +08:00
parent ca6476b87c
commit c49c45d6e4
62 changed files with 9095 additions and 1204 deletions

View File

@ -1,17 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;X64</Platforms>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<BaseOutputPath>..\</BaseOutputPath>
<AppendTargetFrameworkToOutputPath>output</AppendTargetFrameworkToOutputPath>
<UseWindowsForms>true</UseWindowsForms>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>
<ItemGroup>
<Folder Include="Helper\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="OpenCvSharp4" Version="4.10.0.20241108" />
<PackageReference Include="OpenCvSharp4.Extensions" Version="4.10.0.20241108" />

View File

@ -0,0 +1,913 @@
using OpenCvSharp;
using System.ComponentModel;
using System.Drawing;
using static OpenCvSharp.AgastFeatureDetector;
using System.Text.RegularExpressions;
using System.Text;
using System.Drawing.Design;
namespace DH.Commons.Enums
{
public enum MLModelType
{
[Description("图像分类")]
ImageClassification = 1,
[Description("目标检测")]
ObjectDetection = 2,
//[Description("图像分割")]
//ImageSegmentation = 3
[Description("语义分割")]
SemanticSegmentation = 3,
[Description("实例分割")]
InstanceSegmentation = 4,
[Description("目标检测GPU")]
ObjectGPUDetection = 5
}
public class ModelLabel
{
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string LabelId { get; set; }
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
[Category("模型标签")]
[DisplayName("模型标签索引")]
[Description("模型识别的标签索引")]
public int LabelIndex { get; set; }
[Category("模型标签")]
[DisplayName("模型标签")]
[Description("模型识别的标签名称")]
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string LabelName { get; set; }
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
//[Category("模型配置")]
//[DisplayName("模型参数配置")]
//[Description("模型参数配置集合")]
//public ModelParamSetting ModelParamSetting { get; set; } = new ModelParamSetting();
public string GetDisplayText()
{
return $"{LabelId}-{LabelName}";
}
}
public class MLRequest
{
public int ImageChannels = 3;
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public Mat mImage;
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public int ResizeWidth;
public int ResizeHeight;
public float confThreshold;
public float iouThreshold;
//public int ImageResizeCount;
public bool IsCLDetection;
public int ProCount;
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string in_node_name;
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string out_node_name;
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string in_lable_path;
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public int ResizeImageSize;
public int segmentWidth;
public int ImageWidth;
// public List<labelStringBase> OkClassTxtList;
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public List<ModelLabel> LabelNames;
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
}
public enum ResultState
{
[Description("检测NG")]
DetectNG = -3,
//[Description("检测不足TBD")]
// ShortageTBD = -2,
[Description("检测结果TBD")]
ResultTBD = -1,
[Description("OK")]
OK = 1,
// [Description("NG")]
// NG = 2,
//统计结果
[Description("A类NG")]
A_NG = 25,
[Description("B类NG")]
B_NG = 26,
[Description("C类NG")]
C_NG = 27,
}
/// <summary>
/// 深度学习 识别结果明细 面向业务detect 面向深度学习Recongnition、Inference
/// </summary>
public class DetectionResultDetail
{
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string LabelBGR { get; set; }//识别到对象的标签BGR
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public int LabelNo { get; set; } // 识别到对象的标签索引
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string LabelName { get; set; }//识别到对象的标签名称
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public double Score { get; set; }//识别目标结果的可能性、得分
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string LabelDisplay { get; set; }//识别到对象的 显示信息
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public double Area { get; set; }//识别目标的区域面积
public Rectangle Rect { get; set; }//识别目标的外接矩形
public RotatedRect MinRect { get; set; }//识别目标的最小外接矩形(带角度)
public ResultState InferenceResult { get; set; }//只是模型推理 label的结果
public double DistanceToImageCenter { get; set; } //计算矩形框到图像中心的距离
public ResultState FinalResult { get; set; }//模型推理+其他视觉、逻辑判断后 label结果
}
public class MLResult
{
public bool IsSuccess = false;
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string ResultMessage;
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public Bitmap ResultMap;
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public List<DetectionResultDetail> ResultDetails = new List<DetectionResultDetail>();
}
public class MLInit
{
public string ModelFile;
public string InferenceDevice;
public int InferenceWidth;
public int InferenceHeight;
public string InputNodeName;
public int SizeModel;
public bool bReverse;//尺寸测量正反面
//目标检测Gpu
public bool IsGPU;
public int GPUId;
public float Score_thre;
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public MLInit(string modelFile, bool isGPU, int gpuId, float score_thre)
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
{
ModelFile = modelFile;
IsGPU = isGPU;
GPUId = gpuId;
Score_thre = score_thre;
}
public MLInit(string modelFile, string inputNodeName, string inferenceDevice, int inferenceWidth, int inferenceHeight)
{
ModelFile = modelFile;
InferenceDevice = inferenceDevice;
InferenceWidth = inferenceWidth;
InferenceHeight = inferenceHeight;
InputNodeName = inputNodeName;
}
}
public class DetectStationResult
{
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string Pid { get; set; }
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string TempPid { get; set; }
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
/// <summary>
/// 检测工位名称
/// </summary>
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string DetectName { get; set; }
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
/// <summary>
/// 深度学习 检测结果
/// </summary>
public List<DetectionResultDetail> DetectDetails = new List<DetectionResultDetail>();
public List<IShapeElement> DetectionResultShapes = new List<IShapeElement>();
/// <summary>
/// 工位检测结果
/// </summary>
public ResultState ResultState { get; set; } = ResultState.ResultTBD;
public double FinalResultfScore { get; set; } = 0.0;
public string ResultLabel { get; set; } = "";// 多个ng时根据label优先级设定当前检测项的label
public string ResultLabelCategoryId { get; set; } = "";// 多个ng时根据label优先级设定当前检测项的label
public int PreTreatState { get; set; }
public bool IsPreTreatDone { get; set; } = true;
public bool IsAfterTreatDone { get; set; } = true;
public bool IsMLDetectDone { get; set; } = true;
/// <summary>
/// 预处理阶段已经NG
/// </summary>
public bool IsPreTreatNG { get; set; } = false;
/// <summary>
/// 目标检测NG
/// </summary>
public bool IsObjectDetectNG { get; set; } = false;
public DateTime EndTime { get; set; }
public int StationDetectElapsed { get; set; }
public static string NormalizeAndClean(string input)
{
#pragma warning disable CS8603 // 可能返回 null 引用。
if (input == null) return null;
#pragma warning restore CS8603 // 可能返回 null 引用。
// Step 1: 标准化字符编码为 Form C (规范组合)
string normalizedString = input.Normalize(NormalizationForm.FormC);
// Step 2: 移除所有空白字符,包括制表符和换行符
string withoutWhitespace = Regex.Replace(normalizedString, @"\s+", "");
// Step 3: 移除控制字符 (Unicode 控制字符,范围 \u0000 - \u001F 和 \u007F)
string withoutControlChars = Regex.Replace(withoutWhitespace, @"[\u0000-\u001F\u007F]+", "");
// Step 4: 移除特殊的不可见字符(如零宽度空格等)
string cleanedString = Regex.Replace(withoutControlChars, @"[\u200B\u200C\u200D\uFEFF]+", "");
return cleanedString;
}
}
public class RelatedCamera
{
[Category("关联相机")]
[DisplayName("关联相机")]
[Description("关联相机描述")]
//[TypeConverter(typeof(CollectionCountConvert))]
public string CameraSourceId { get; set; } = "";
public RelatedCamera()
{
}
public RelatedCamera(string cameraSourceId)
{
CameraSourceId = cameraSourceId;
}
}
public class CustomizedPoint : INotifyPropertyChanged, IComparable<CustomizedPoint>
{
private double x = 0;
[Category("坐标设置")]
[Description("X坐标")]
public double X
{
get => x;
set
{
if (value != x)
{
x = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("X"));
}
}
}
private double y = 0;
[Category("坐标设置")]
[Description("Y坐标")]
public double Y
{
get => y;
set
{
if (value != y)
{
y = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Y"));
}
}
}
public CustomizedPoint() { }
public CustomizedPoint(double x, double y)
{
X = x;
Y = y;
}
//public CustomizedPoint(Point p)
//{
// X = p.X;
// Y = p.Y;
//}
public CustomizedPoint(PointF p)
{
X = p.X;
Y = p.Y;
}
public CustomizedPoint(CustomizedPoint p)
{
X = p.X;
Y = p.Y;
}
/// <summary>
/// 根据PLC的读取数值获取点位坐标
/// </summary>
/// <param name="plcValues">0X低位 1X高位 2Y低位 3Y高位</param>
//public CustomizedPoint(List<int> plcValues)
//{
// if (plcValues == null || plcValues.Count != 4)
// return;
// var list = plcValues.ParseUnsignShortListToInt();
// X = list[0];
// Y = list[1];
//}
public event PropertyChangedEventHandler PropertyChanged;
public static List<CustomizedPoint> GetPoints(List<double> Xs, List<double> Ys)
{
List<CustomizedPoint> points = new List<CustomizedPoint>();
for (int i = 0; i < Xs.Count && i < Ys.Count; i++)
{
points.Add(new CustomizedPoint(Xs[i], Ys[i]));
}
return points;
}
public static double GetDistance(CustomizedPoint p1, CustomizedPoint p2)
{
return Math.Sqrt(Math.Pow((p1.X - p2.X), 2) + Math.Pow((p1.Y - p2.Y), 2));
}
public override string ToString()
{
return GetDisplayText();
}
public virtual string GetDisplayText()
{
return string.Format("X:{0};Y:{1}", X, Y);
}
public virtual string GetCSVHead()
{
return "X,Y";
}
public virtual string GetCSVData()
{
return X.ToString("f3") + ";" + Y.ToString("f3");
}
//public static double GetCustomizedPointDistance(CustomizedPoint startPoint, CustomizedPoint endPoint)
//{
// return Math.Sqrt(Math.Pow(endPoint.X - startPoint.X, 2) + Math.Pow(endPoint.Y - startPoint.Y, 2));
//}
public CustomizedPoint OffsetClone(CustomizedPoint point)
{
return new CustomizedPoint(X + point.X, Y + point.Y);
}
public void Offset(CustomizedPoint point)
{
X += point.X;
Y += point.Y;
}
public int CompareTo(CustomizedPoint other)
{
return (X == other.X && Y == other.Y) ? 0 : 1;
}
public override int GetHashCode()
{
//return (int)(X * 10 + Y);
return (new Tuple<double, double>(X, Y)).GetHashCode();
}
public static CustomizedPoint operator -(CustomizedPoint p1, CustomizedPoint p2)
{
return new CustomizedPoint(p1.X - p2.X, p1.Y - p2.Y);
}
public static CustomizedPoint operator +(CustomizedPoint p1, CustomizedPoint p2)
{
return new CustomizedPoint(p1.X + p2.X, p1.Y + p2.Y);
}
}
public class PreTreatParam
{
/// <summary>
/// 参数名称
/// </summary>
///
[Category("预处理参数")]
[DisplayName("参数名称")]
[Description("参数名称")]
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string Name { get; set; }
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
/// <summary>
/// 参数值
/// </summary>
///
[Category("预处理参数")]
[DisplayName("参数值")]
[Description("参数值")]
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string Value { get; set; }
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
}
public class DetectionConfig
{
[ReadOnly(true)]
public string Id { get; set; } = Guid.NewGuid().ToString();
[Category("检测配置")]
[DisplayName("检测配置名称")]
[Description("检测配置名称")]
public string Name { get; set; }
[Category("关联相机")]
[DisplayName("关联相机")]
[Description("关联相机描述")]
public string CameraSourceId { get; set; } = "";
[Category("关联相机集合")]
[DisplayName("关联相机集合")]
[Description("关联相机描述")]
//[TypeConverter(typeof(DeviceIdSelectorConverter<CameraBase>))]
public List<RelatedCamera> CameraCollects { get; set; } = new List<RelatedCamera>();
[Category("启用配置")]
[DisplayName("是否启用GPU检测")]
[Description("是否启用GPU检测")]
public bool IsEnableGPU { get; set; } = false;
[Category("启用配置")]
[DisplayName("是否混料模型")]
[Description("是否混料模型")]
public bool IsMixModel { get; set; } = false;
[Category("启用配置")]
[DisplayName("是否启用该检测")]
[Description("是否启用该检测")]
public bool IsEnabled { get; set; }
[Category("启用配置")]
[DisplayName("是否加入检测工位")]
[Description("是否加入检测工位")]
public bool IsAddStation { get; set; } = true;
[Category("1.预处理(视觉算子)")]
[DisplayName("预处理-算法文件路径")]
// [Description("预处理算法文件路径配置")][Editor(typeof(FileDialogEditor), typeof(UITypeEditor))]
public string HalconAlgorithemPath_Pre { get; set; }
// [Category("1.预处理(视觉算子)")]
//[DisplayName("预处理-输出结果的SPEC标准")]
//[Description("预处理输出结果的SPEC标准配置")]
// public List<IndexedSpec> OutputSpec_Pre { get; set; } = new List<IndexedSpec>();
[Category("1.预处理(视觉算子)")]
[DisplayName("预处理-参数列表")]
[Description("预处理-参数列表")]
public List<PreTreatParam> PreTreatParams { get; set; } = new List<PreTreatParam>();
[Category("1.预处理(视觉算子)")]
[DisplayName("预处理-输出参数列表")]
[Description("预处理-输出参数列表")]
public List<PreTreatParam> OUTPreTreatParams { get; set; } = new List<PreTreatParam>();
[Category("2.中检测(深度学习)")]
[DisplayName("中检测-模型类型")]
[Description("模型类型ImageClassification-图片分类ObjectDetection目标检测Segmentation-图像分割")]
//[TypeConverter(typeof(EnumDescriptionConverter<MLModelType>))]
public MLModelType ModelType { get; set; } = MLModelType.ObjectDetection;
//[Category("2.中检测(深度学习)")]
//[DisplayName("中检测-GPU索引")]
//[Description("GPU索引")]
//public int GPUIndex { get; set; } = 0;
[Category("2.中检测(深度学习)")]
[DisplayName("中检测-模型文件路径")]
[Description("中处理 深度学习模型文件路径,路径中不可含有中文字符,一般情况可以只配置中检测模型,当需要先用预检测过滤一次时,请先配置好与预检测相关配置")]
public string ModelPath { get; set; }
[Category("2.中检测(深度学习)")]
[DisplayName("中检测-模型宽度")]
[Description("中处理-模型宽度")]
public int ModelWidth { get; set; } = 640;
[Category("2.中检测(深度学习)")]
[DisplayName("中检测-模型高度")]
[Description("中处理-模型高度")]
public int ModelHeight { get; set; } = 640;
[Category("2.中检测(深度学习)")]
[DisplayName("中检测-模型节点名称")]
[Description("中处理-模型节点名称")]
public string ModeloutNodeName { get; set; } = "output0";
[Category("2.中检测(深度学习)")]
[DisplayName("中检测-模型置信度")]
[Description("中处理-模型置信度")]
public float ModelconfThreshold { get; set; } = 0.5f;
[Category("2.中检测(深度学习)")]
[DisplayName("中检测-模型标签路径")]
[Description("中处理-模型标签路径")]
public string in_lable_path { get; set; }
[Category("4.最终过滤(逻辑过滤)")]
[DisplayName("过滤器集合")]
[Description("最后的逻辑过滤:可根据 识别出对象的 宽度、高度、面积、得分来设置最终检测结果,同一识别目标同一判定,多项过滤器之间为“或”关系")]
public List<DetectionFilter> DetectionFilterList { get; set; } = new List<DetectionFilter>();
//[Category("深度学习配置")]
//[DisplayName("检测配置标签")]
//[Description("检测配置标签关联")]
//public List<DetectConfigLabel> DetectConfigLabelList { get; set; } = new List<DetectConfigLabel>();
[Category("显示配置")]
[DisplayName("显示位置")]
[Description("检测信息显示位置。左上角为11向右向下为正方向")]
// [TypeConverter(typeof(ComplexObjectConvert))]
// [Editor(typeof(PropertyObjectEditor), typeof(UITypeEditor))]
public CustomizedPoint ShowLocation { get; set; } = new CustomizedPoint();
public DetectionConfig()
{
}
public DetectionConfig(string name, MLModelType modelType, string modelPath, bool isEnableGPU,string sCameraSourceId)
{
ModelPath = modelPath ?? string.Empty;
Name = name;
ModelType = modelType;
IsEnableGPU = isEnableGPU;
Id = Guid.NewGuid().ToString();
CameraSourceId = sCameraSourceId;
}
}
/// <summary>
/// 识别目标定义 class分类信息 Detection Segmentation要识别的对象
/// </summary>
public class RecongnitionLabel //: IComplexDisplay
{
[Category("检测标签定义")]
[Description("检测标签编码")]
[ReadOnly(true)]
public string Id { get; set; } = Guid.NewGuid().ToString();
[Category("检测标签定义")]
[DisplayName("检测标签名称")]
[Description("检测标签名称")]
public string LabelName { get; set; } = "";
[Category("检测标签定义")]
[DisplayName("检测标签描述")]
[Description("检测标签描述,中文描述")]
public string LabelDescription { get; set; } = "";
[Category("检测标签定义")]
[DisplayName("检测标签分类")]
[Description("检测标签分类id")]
//[TypeConverter(typeof(LabelCategoryConverter))]
public string LabelCategory { get; set; } = "";
}
/// <summary>
/// 检测项识别对象
/// </summary>
public class DetectConfigLabel //: IComplexDisplay
{
[Category("检测项标签")]
[DisplayName("检测项标签")]
[Description("检测标签Id")]
//[TypeConverter(typeof(DetectionLabelConverter))]
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string LabelId { get; set; }
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
[Browsable(false)]
//public string LabelName { get => GetLabelName(); }
[Category("检测项标签")]
[DisplayName("检测标签优先级")]
[Description("检测标签优先级,值越小,优先级越高")]
public int LabelPriority { get; set; } = 0;
//[Category("检测项标签")]
//[DisplayName("标签BGR值")]
//[Description("检测标签BGR值例如0,128,0")]
//public string LabelBGR { get; set; }
//[Category("模型配置")]
//[DisplayName("模型参数配置")]
//[Description("模型参数配置集合")]
//[TypeConverter(typeof(ComplexObjectConvert))]
//[Editor(typeof(PropertyObjectEditor), typeof(UITypeEditor))]
//public ModelParamSetting ModelParamSetting { get; set; } = new ModelParamSetting();
//public string GetDisplayText()
//{
// string dName = "";
// if (!string.IsNullOrWhiteSpace(LabelId))
// {
// using (var scope = GlobalVar.Container.BeginLifetimeScope())
// {
// IProcessConfig config = scope.Resolve<IProcessConfig>();
// var mlBase = config.DeviceConfigs.FirstOrDefault(c => c is VisionEngineInitialConfigBase) as VisionEngineInitialConfigBase;
// if (mlBase != null)
// {
// var targetLabel = mlBase.RecongnitionLabelList.FirstOrDefault(u => u.Id == LabelId);
// if (targetLabel != null)
// {
// dName = targetLabel.GetDisplayText();
// }
// }
// }
// }
// return dName;
//}
//public string GetLabelName()
//{
// var name = "";
// var mlBase = iConfig.DeviceConfigs.FirstOrDefault(c => c is VisionEngineInitialConfigBase) as VisionEngineInitialConfigBase;
// if (mlBase != null)
// {
// var label = mlBase.RecongnitionLabelList.FirstOrDefault(u => u.Id == LabelId);
// if (label != null)
// {
// name = label.LabelName;
// }
// }
// return name;
//}
}
/// <summary>
/// 识别对象定义分类信息 A类B类
/// </summary>
public class RecongnitionLabelCategory //: IComplexDisplay
{
[Category("检测标签分类")]
[Description("检测标签分类")]
[ReadOnly(true)]
public string Id { get; set; } = Guid.NewGuid().ToString();
[Category("检测标签分类")]
[DisplayName("检测标签分类名称")]
[Description("检测标签分类名称")]
public string CategoryName { get; set; } = "A-NG";
[Category("检测标签分类")]
[DisplayName("检测标签分类优先级")]
[Description("检测标签分类优先级,值越小,优先级越高")]
public int CategoryPriority { get; set; } = 0;
public string GetDisplayText()
{
return CategoryPriority + ":" + CategoryName;
}
}
/// <summary>
/// 检测过滤
/// </summary>
public class DetectionFilter ///: IComplexDisplay
{
[Category("过滤器基础信息")]
[DisplayName("检测标签")]
[Description("检测标签信息")]
//[TypeConverter(typeof(DetectionLabelConverter))]
public string LabelId { get; set; }
public string LabelName { get; set; }
[Category("过滤器基础信息")]
[DisplayName("是否启用过滤器")]
[Description("是否启用过滤器")]
public bool IsEnabled { get; set; }
[Category("过滤器判定信息")]
[DisplayName("判定结果")]
[Description("过滤器默认判定结果")]
public ResultState ResultState { get; set; } = ResultState.ResultTBD;
[Category("过滤条件")]
[DisplayName("过滤条件集合")]
[Description("过滤条件集合,集合之间为“且”关系")]
//[TypeConverter(typeof(CollectionCountConvert))]
// [Editor(typeof(ComplexCollectionEditor<FilterConditions>), typeof(UITypeEditor))]
public List<FilterConditions> FilterConditionsCollection { get; set; } = new List<FilterConditions>();
public bool FilterOperation(DetectionResultDetail recongnitionResult)
{
return FilterConditionsCollection.All(u =>
{
return u.FilterConditionCollection.Any(c =>
{
double compareValue = 0;
switch (c.FilterPropperty)
{
case DetectionFilterProperty.Width:
compareValue = recongnitionResult.Rect.Width;
break;
case DetectionFilterProperty.Height:
compareValue = recongnitionResult.Rect.Height;
break;
case DetectionFilterProperty.Area:
compareValue = recongnitionResult.Area;
break;
case DetectionFilterProperty.Score:
compareValue = recongnitionResult.Score;
break;
//case RecongnitionTargetFilterProperty.Uncertainty:
// compareValue = 0;
// //defect.Uncertainty;
// break;
}
return compareValue >= c.MinValue && compareValue <= c.MaxValue;
});
});
}
}
public class FilterConditions //: IComplexDisplay
{
[Category("过滤条件")]
[DisplayName("过滤条件集合")]
[Description("过滤条件集合,集合之间为“或”关系")]
//[TypeConverter(typeof(CollectionCountConvert))]
//[Editor(typeof(ComplexCollectionEditor<FilterCondition>), typeof(UITypeEditor))]
public List<FilterCondition> FilterConditionCollection { get; set; } = new List<FilterCondition>();
//public string GetDisplayText()
//{
// if (FilterConditionCollection.Count == 0)
// {
// return "空";
// }
// else
// {
// var desc = string.Join(" OR ", FilterConditionCollection.Select(u => u.GetDisplayText()));
// if (FilterConditionCollection.Count > 1)
// {
// desc = $"({desc})";
// }
// return desc;
// }
//}
}
public class FilterCondition //: IComplexDisplay
{
[Category("识别目标属性")]
[DisplayName("过滤属性")]
[Description("识别目标过滤针对的属性")]
//[TypeConverter(typeof(EnumDescriptionConverter<DetectionFilterProperty>))]
public DetectionFilterProperty FilterPropperty { get; set; } = DetectionFilterProperty.Width;
[Category("过滤值")]
[DisplayName("最小值")]
[Description("最小值")]
public double MinValue { get; set; } = 1;
[Category("过滤值")]
[DisplayName("最大值")]
[Description("最大值")]
public double MaxValue { get; set; } = 99999999;
//public string GetDisplayText()
//{
// return $"{FilterPropperty.GetEnumDescription()}:{MinValue}-{MaxValue}";
//}
}
public enum DetectionFilterProperty
{
[Description("宽度")]
Width = 1,
[Description("高度")]
Height = 2,
[Description("面积")]
Area = 3,
[Description("得分")]
Score = 4,
//[Description("不确定性")]
//Uncertainty = 5,
}
}

View File

@ -5,7 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XKRS.Common.Model.SolidMotionCard
namespace DH.Commons.Enums
{

View File

@ -1,6 +1,6 @@

namespace XKRS.Common.Model
namespace DH.Commons.Enums
{
public static class GlobalVar
{

View File

@ -0,0 +1,771 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace DH.Commons.Enums
{
public static class EnumHelper
{
/// <summary>
/// 根据枚举的代码名称获取枚举列表信息
/// </summary>
/// <param name="enumName"></param>
/// <returns></returns>
public static List<dynamic> GetEnumListByName(string enumName, string assemblyName = "Common.Model.Helper.EnumHelper+")
{
List<dynamic> list = new List<dynamic>();
string fullName = assemblyName + enumName;
Type t = typeof(EnumHelper).Assembly.GetType(fullName);
t.GetEnumNames().ToList().ForEach(e =>
{
int value = Convert.ToInt32(Enum.Parse(t, e));
string desc = ((Enum)Enum.Parse(t, e)).GetEnumDescription();
var item = new { Value = value, Desc = desc, Code = e };
list.Add(item);
});
return list;
}
/// <summary>
/// 获取枚举的列表信息,一般供下拉列表使用
/// </summary>
/// <param name="enumType">枚举类型</param>
/// <returns>Desc中文描述 Value整形值 Code枚举值</returns>
public static List<dynamic> GetEnumListByType(Type enumType)
{
List<dynamic> list = new List<dynamic>();
enumType.GetEnumNames().ToList().ForEach(e =>
{
int value = Convert.ToInt32(Enum.Parse(enumType, e));
string desc = ((Enum)Enum.Parse(enumType, e)).GetEnumDescription();
var item = new { Value = value, Desc = desc, Code = e };
list.Add(item);
});
return list;
}
/// <summary>
/// 获取具体某一枚举的中文描述
/// </summary>
/// <param name="enumObj"></param>
/// <returns></returns>
public static string GetEnumDescription(this Enum enumObj)
{
Type t = enumObj.GetType();
FieldInfo f = t.GetField(enumObj.ToString());
if (f == null)
{
return enumObj.ToString();
}
DescriptionAttribute attr = f.GetCustomAttribute<DescriptionAttribute>();
if (attr != null)
{
return attr.Description;
}
else
{
return enumObj.ToString();
}
}
//public static System.Windows.Media.Color GetEnumSelectedMediaColor(this Enum enumObj)
//{
// Type t = enumObj.GetType();
// FieldInfo f = t.GetField(enumObj.ToString());
// ColorSelectAttribute attr = f.GetCustomAttribute<ColorSelectAttribute>();
// if (attr != null)
// {
// var prop = typeof(System.Windows.Media.Colors).GetProperties().FirstOrDefault(p => p.Name == attr.SelectedColor);
// if (prop != null)
// {
// return (System.Windows.Media.Color)prop.GetValue(null);
// }
// }
// return System.Windows.Media.Colors.Transparent;
//}
//public static System.Drawing.Color GetEnumSelectedColor(this Enum enumObj)
//{
// Type t = enumObj.GetType();
// FieldInfo f = t.GetField(enumObj.ToString());
// ColorSelectAttribute attr = f.GetCustomAttribute<ColorSelectAttribute>();
// if (attr != null)
// {
// return System.Drawing.Color.FromName(attr.SelectedColor);
// }
// else
// {
// return System.Drawing.Color.Transparent;
// }
//}
//public static System.Drawing.Color GetEnumSelectedFontColor(this Enum enumObj)
//{
// Type t = enumObj.GetType();
// FieldInfo f = t.GetField(enumObj.ToString());
// var attr = f.GetCustomAttribute<FontColorSelectAttribute>();
// if (attr != null)
// {
// return System.Drawing.Color.FromName(attr.SelectedColor);
// }
// else
// {
// return System.Drawing.Color.Transparent;
// }
//}
//public static string GetEnumSelectedColorString(this Enum enumObj)
//{
// Type t = enumObj.GetType();
// FieldInfo f = t.GetField(enumObj.ToString());
// ColorSelectAttribute attr = f.GetCustomAttribute<ColorSelectAttribute>();
// if (attr != null)
// {
// return attr.SelectedColor;
// }
// else
// {
// return "Transparent";
// }
//}
/// <summary>
/// 当枚举牵涉到状态变换,检查现状态是否满足待转换的状态的前置状态要求
/// </summary>
/// <param name="stateToBe"></param>
/// <param name="currentState"></param>
/// <returns></returns>
//public static bool CheckPreStateValid(this Enum stateToBe, int currentState)
//{
// Type t = stateToBe.GetType();
// FieldInfo f = t.GetField(stateToBe.ToString());
// PreStateAttribute attr = f.GetCustomAttribute<PreStateAttribute>();
// if (attr == null)
// {
// return true;
// }
// else
// {
// return attr.CheckPreStateValid(currentState);
// }
//}
/// <summary>
/// 设备状态定义
/// 未初始化和异常状态无前置状态要求
/// 初始化操作前置状态必须是未初始化、关闭状态和异常状态
/// 打开前置必须是初始化和暂停
/// 关闭前置必须是打开和暂停和异常
/// 暂停前置必须是打开
/// </summary>
//public enum DeviceState
//{
// TBD = -1,
// [ColorSelect("Gray")]
// [FontColorSelect("Black")]
// [Description("未初始化")]
// DSUninit = 1,
// [ColorSelect("Gold")]
// [FontColorSelect("White")]
// [PreState(1 + 2 + 4 + 8 + 32)]
// [Description("初始化")]
// DSInit = 2,
// [ColorSelect("Lime")]
// [FontColorSelect("Black")]
// [PreState(2 + 4 + 16)]
// [Description("运行中")]
// DSOpen = 4,
// [ColorSelect("Gray")]
// [FontColorSelect("White")]
// [PreState(1 + 4 + 8 + 16 + 32)]
// [Description("关闭")]
// DSClose = 8,
// [ColorSelect("Gold")]
// [FontColorSelect("White")]
// [PreState(4 + 16)]
// [Description("暂停")]
// DSPause = 16,
// [ColorSelect("Red")]
// [FontColorSelect("White")]
// [Description("异常")]
// DSExcept = 32
//}
///// <summary>
///// 工序状态
///// </summary>
//public enum RunState
//{
// [ColorSelect("Gold")]
// [Description("空闲")]
// Idle = 1,
// [ColorSelect("Lime")]
// [Description("运行中")]
// Running = 2,
// [ColorSelect("Gray")]
// [Description("停止")]
// Stop = 3,
// [ColorSelect("Red")]
// [Description("宕机")]
// Down = 99,
//}
[Flags]
public enum DeviceAttributeType
{
[Description("设备驱动")]
Device = 1,
[Description("初始配置")]
InitialConfig = 2,
[Description("操作配置")]
OperationConfig = 4,
[Description("设备配置")]
DeviceConfig = 8,
[Description("输入配置")]
InputConfig = 16,
[Description("初始配置控件")]
InitialConfigCtrl = 32,
[Description("操作配置控件")]
OperationConfigCtrl = 64,
[Description("设备配置控件")]
DeviceConfigCtrl = 128,
[Description("输入配置控件")]
InputConfigCtrl = 256,
[Description("运行控件")]
RunCtrl = 512,
[Description("操作配置面板")]
OperationConfigPanel = 1024,
}
public enum EnableState
{
[Description("启用")]
Enabled = 0,
[Description("禁用")]
Disabled = 1,
}
public enum EnableStateFilter
{
[Description("全部")]
All = -1,
[Description("启用")]
Enabled = 0,
[Description("禁用")]
Disabled = 1,
}
public enum OutputResult
{
[Description("OK")]
OK = 1,
[Description("NG")]
NG = 2,
}
/// <summary>
/// PLC项目的值的类型
/// </summary>
public enum PLCItemType
{
[Description("布尔类型")]
Bool = 0,
[Description("整型")]
Integer = 1,
[Description("字符串型")]
String = 2,
}
/// <summary>
/// 对PLC项目的操作类型
/// </summary>
public enum PLCOpType
{
[Description("读取")]
Read = 1,
[Description("写入")]
Write = 2,
[Description("监控")]
Monitor = 4,
/// <summary>
/// 报警监控 1+8
/// </summary>
[Description("报警监控")]
WarningMonitor = 9,
/// <summary>
/// CT监控 1+16
/// </summary>
[Description("CT监控")]
CTMonitor = 17,
}
/// <summary>
/// 相机运行模式
/// </summary>
public enum CameraOpMode
{
[Description("单次拍照")]
SingleSnapShot = 1,
[Description("连续模式")]
ContinuousMode = 2,
}
/// <summary>
/// 日志类型
/// </summary>
public enum LogType
{
[Description("警告信息")]
Exception_Warning = 1,
[Description("错误信息")]
Exception_Error = 2,
[Description("致命信息")]
Exception_Fatal = 3,
[Description("设备信息")]
Info_Device = 11,
[Description("工序信息")]
Info_Process = 12,
[Description("操作信息")]
Info_Operation = 13,
[Description("用户操作信息")]
User_Operation = 21,
[Description("测量结果")]
MeasureResult = 31,
}
//public enum CameraDriverType
//{
// Halcon,
// //HalconPlugIn,
// Hik,
//}
//public enum ImageType
//{
// Bmp,
// Png,
// Jpeg,
//}
//public enum ReplyValue
//{
// OK = 1,
// NG = -1,
// IGNORE = -999,
//}
public enum PriorityDirection
{
X,
Y,
}
public enum ElementState
{
New = 1,
MouseHover = 2,
MouseInSide = 3,
Selected = 4,
Moving = 5,
Editing = 6,
Normal = 11,
Measuring = 21,
MeasureDoneOK = 22,
MeasureDoneNG = 23,
CanStretchLeft = 41,
CanStretchRight = 42,
CanStretchTop = 43,
CanStretchBottom = 44,
CanStretchLeftUpperCorner = 45,
CanStretchLeftLowerCorner = 46,
CanStretchRightUpperCorner = 47,
CanStretchRightLowerCorner = 48,
StretchingLeft = 31,
StretchingRight = 32,
StretchingTop = 33,
StretchingBottom = 34,
StretchingLeftUpperCorner = 35,
StretchingLeftLowerCorner = 36,
StretchingRightUpperCorner = 37,
StretchingRightLowerCorner = 38,
}
public enum MouseState
{
Normal = 1,
HoverElement = 2,
InSideElement = 3,
MoveElement = 4,
StretchingLeft = 11,
StretchingRight = 12,
StretchingTop = 13,
StretchingBottom = 14,
StretchingLeftUpperCorner = 15,
StretchingLeftLowerCorner = 16,
StretchingRightUpperCorner = 17,
StretchingRightLowerCorner = 18,
New = 21,
Editing = 22,
//SelectedElement = 23,
MovingAll = 31,
SelectionZone = 41,
SelectionZoneDoing = 42,
}
public enum RunMode
{
[Description("正常运行模式")]
NormalMode = 1,
[Description("调试模式")]
DebugMode = 0,
[Description("模拟模式")]
DemoMode = 2,
}
public enum MoveType
{
[Description("绝对运动")]
AbsoluteMove = 1,
[Description("机器人坐标系相对运动")]
RobotRelativeMove = 2,
[Description("相对某个基准点位的相对运动")]
BasedPointRelativeMove = 3,
[Description("回原点")]
Origin = 4,
[Description("左侧姿势")]
LeftPose = 6,
[Description("右侧姿势")]
RightPose = 5,
[Description("前侧姿势")]
FrontPose = 7,
[Description("相机坐标系相对运动")]
CameraRelativeMove = 12,
}
/// <summary>
/// 马达/运动板卡运行模式
/// </summary>
public enum MotionMode
{
/// <summary>
/// 普通点位运动
/// </summary>
[Description("普通点位运动")]
P2P = 1,
/// <summary>
/// 找正限位运动
/// </summary>
[Description("找正限位运动")]
FindPositive = 4,
/// <summary>
/// 离开正限位
/// </summary>
[Description("离开正限位")]
LeavePositive = 5,
/// <summary>
/// 找负限位运动
/// </summary>
[Description("找负限位运动")]
FindNegative = 6,
/// <summary>
/// 离开负限位
/// </summary>
[Description("离开负限位")]
LeaveNegative = 7,
/// <summary>
/// 找原点运动
/// </summary>
[Description("回原点运动")]
GoHome = 8,
/// <summary>
/// Jog模式
/// </summary>
[Description("Jog模式")]
Jog = 9,
///// <summary>
///// 读数头找原点方式
///// </summary>
//[Description("找原点inde")]
//FindOriIndex = 10,
///// <summary>
///// 插补模式
///// </summary>
//[Description("插补模式")]
//Coordinate = 11
}
/// <summary>
/// IO预定义类型 主要针对输出
/// </summary>
public enum IOPrestatement
{
[Description("自定义")]
Customized = 0,
[Description("指示灯-黄")]
Light_Yellow = 1,
[Description("指示灯-绿")]
Light_Green = 2,
[Description("指示灯-红")]
Light_Red = 3,
[Description("蜂鸣器")]
Beep = 4,
[Description("照明灯")]
Light = 5,
[Description("急停")]
EmergencyStop = 99,
}
///// <summary>
///// GTS运动板卡控制返回控制码
///// </summary>
//public enum GTSRetCode
//{
// [Description("指令执行成功")]
// GRCRunOK = 0, // 指令执行成功
// [Description("指令执行错误")]
// GRCRunErr = 1, // 指令执行错误
// [Description("icense不支持")]
// GRCNotSupport = 2, // icense不支持
// [Description("指令参数错误")]
// GRCInvalidParam = 7, // 指令参数错误
// [Description("主机和运动控制器通讯失败")]
// GRCCommErr = -1, // 主机和运动控制器通讯失败
// [Description("打开控制器失败")]
// GRCOpenErr = -6, // 打开控制器失败
// [Description("运动控制器没有响应")]
// GRCNotAck = -7 // 运动控制器没有响应
//}
/// <summary>
/// 运动板卡 IO 类型IN OUT
/// </summary>
public enum IOType
{
[Description("INPUT")]
INPUT = 0,
[Description("OUTPUT")]
OUTPUT = 1
}
public enum IOValue
{
[Description("关闭")]
FALSE = 0,
[Description("开启")]
TRUE = 1,
[Description("反转")]
REVERSE = 2,
}
/// <summary>
/// PubSubCenter事件中心的消息类型
/// </summary>
public enum PubSubCenterMessageType
{
/// <summary>
/// 运行界面更新产品下拉
/// </summary>
[Description("更新产品下拉")]
UpdateProductionCodes,
/// <summary>
/// 流程是否关闭
/// </summary>
[Description("流程是否打开")]
IsProcessOpened,
/// <summary>
/// 清除数据
/// </summary>
[Description("清除数据")]
ClearData,
/// <summary>
/// 更新批次信息
/// </summary>
[Description("更新批次信息")]
UpdateBatchNO,
/// <summary>
/// 请求批次信息
/// </summary>
[Description("更新批次信息")]
RequestBatchNO,
/// <summary>
/// 模型检测异常
/// </summary>
[Description("模型检测异常")]
MLDetectException,
}
public enum SizeEnum
{
[Description("圆形测量")]
Circle = 1,
[Description("直线测量")]
Line = 2,
[Description("线线测量")]
LineLine = 3,
[Description("线圆测量")]
LineCircle = 4,
[Description("高度测量")]
Height = 5,
}
public enum MachineState
{
Unknown = 0,
Ready = 1,
Running = 2,
Alarm = 3,
Pause = 4,
Resetting = 5,
Closing = 6,
ClearProduct = 7,
Warning = 8,
}
public enum ResultState
{
[Description("NA")]
NA = -5,
[Description("尺寸NG")]
SizeNG = -4,
[Description("检测NG")]
DetectNG = -3,
//[Description("检测不足TBD")]
// ShortageTBD = -2,
[Description("检测结果TBD")]
ResultTBD = -1,
[Description("OK")]
OK = 1,
// [Description("NG")]
// NG = 2,
//统计结果
[Description("A类NG")]
A_NG = 25,
[Description("B类NG")]
B_NG = 26,
[Description("C类NG")]
C_NG = 27,
}
public enum HikCameraType
{
[Description("HikCamera-Gige")]
Gige = 0,
[Description("HikCamera-USB")]
USB = 1
}
/// <summary>
/// 光源操作
/// </summary>
public enum LightOperation
{
[Description("打开")]
Open = 1,
[Description("关闭")]
Close = 2,
[Description("写入")]
Write = 3,
[Description("读取")]
Read = 4,
[Description("频闪")]
Flash = 5,
}
/// <summary>
/// 监听反馈数据值
/// </summary>
public enum ReturnValue
{
OKVALUE = 1,
NGVALUE = -1,
EXCEPTIONVALUE = -2,
UNAUTHORIZATION = -10,
IGNORE = -999,
}
//public enum LogLevel
//{
// [Description("详细")]
// [ColorSelect("White")]
// [FontColorSelect("Green")]
// Detail = 2,
// [Description("信息")]
// [ColorSelect("White")]
// [FontColorSelect("Dark")]
// Information = 3,
// [Description("辅助")]
// [ColorSelect("White")]
// [FontColorSelect("Blue")]
// Assist = 4,
// [Description("动作")]
// [ColorSelect("DarkGreen")]
// [FontColorSelect("Yellow")]
// Action = 5,
// [Description("错误")]
// [ColorSelect("Orange")]
// [FontColorSelect("White")]
// Error = 6,
// [Description("警报")]
// [ColorSelect("Brown")]
// [FontColorSelect("White")]
// Warning = 7,
// [Description("异常")]
// [ColorSelect("Red")]
// [FontColorSelect("White")]
// Exception = 8,
//}
}
}

View File

@ -5,7 +5,7 @@ using System.Drawing.Imaging;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
namespace XKRS.Common.Model
namespace DH.Commons.Enums
{
public class HDevEngineTool : IDisposable
{

View File

@ -9,7 +9,7 @@ using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace XKRS.Common.Model
namespace DH.Commons.Enums
{
public class OpenCVEngineTool : IDisposable
{

View File

@ -0,0 +1,123 @@
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DH.Commons.Enums
{
public static class UIHelper
{
public static void SetCombo(ComboBox cbo, object dataSource, string display, string value)
{
cbo.DataSource = dataSource;
cbo.DisplayMember = display;
if (!string.IsNullOrWhiteSpace(value))
{
cbo.ValueMember = value;
}
}
public static void SetCombo(ToolStripComboBox cbo, object dataSource, string display, string value)
{
cbo.ComboBox.DataSource = dataSource;
cbo.ComboBox.DisplayMember = display;
if (!string.IsNullOrWhiteSpace(value))
{
cbo.ComboBox.ValueMember = value;
}
}
public static void SetCombo(DataGridViewComboBoxColumn cbo, object dataSource, string display, string value)
{
cbo.DataSource = dataSource;
cbo.DisplayMember = display;
if (!string.IsNullOrWhiteSpace(value))
{
cbo.ValueMember = value;
}
}
}
#region DataGridView设置列头为复选框
public class DataGridViewCheckboxHeaderEventArgs : EventArgs
{
private bool checkedState = false;
public bool CheckedState
{
get { return checkedState; }
set { checkedState = value; }
}
}
public delegate void DataGridViewCheckboxHeaderEventHander(object sender, DataGridViewCheckboxHeaderEventArgs e);
public class DataGridViewCheckboxHeaderCell : DataGridViewColumnHeaderCell
{
Point checkBoxLocation;
Size checkBoxSize;
bool _checked = false;
Point _cellLocation = new Point();
System.Windows.Forms.VisualStyles.CheckBoxState _cbState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
public event DataGridViewCheckboxHeaderEventHander OnCheckBoxClicked;
        //绘制列头checkbox
        protected override void Paint(System.Drawing.Graphics graphics,
System.Drawing.Rectangle clipBounds,
System.Drawing.Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates dataGridViewElementState,
object value,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
dataGridViewElementState, value,
formattedValue, errorText, cellStyle,
advancedBorderStyle, paintParts);
Point p = new Point();
Size s = CheckBoxRenderer.GetGlyphSize(graphics, System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
p.X = cellBounds.Location.X + (cellBounds.Width / 2) - (s.Width / 2) - 1;   //列头checkbox的X坐标
            p.Y = cellBounds.Location.Y + (cellBounds.Height / 2) - (s.Height / 2);     //列头checkbox的Y坐标
            _cellLocation = cellBounds.Location;
checkBoxLocation = p;
checkBoxSize = s;
if (_checked)
_cbState = System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal;
else
_cbState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
CheckBoxRenderer.DrawCheckBox(graphics, checkBoxLocation, _cbState);
}
        ///
        /// 点击列头checkbox单击事件
        ///
        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
if (p.X >= checkBoxLocation.X && p.X <= checkBoxLocation.X + checkBoxSize.Width
&& p.Y >= checkBoxLocation.Y && p.Y <= checkBoxLocation.Y + checkBoxSize.Height)
{
_checked = !_checked;
                //获取列头checkbox的选择状态
                DataGridViewCheckboxHeaderEventArgs ex = new DataGridViewCheckboxHeaderEventArgs();
ex.CheckedState = _checked;
object sender = new object();//此处不代表选择的列头checkbox只是作为参数传递。因为列头checkbox是绘制出来的无法获得它的实例
                if (OnCheckBoxClicked != null)
{
OnCheckBoxClicked(sender, ex);//触发单击事件
                    DataGridView.InvalidateCell(this);
}
}
base.OnMouseClick(e);
}
}
#endregion
}

View File

@ -0,0 +1,39 @@
using System;
using System.ComponentModel;
using System.Drawing;
using static DH.Commons.Enums.EnumHelper;
namespace DH.Commons.Enums
{
public interface IShapeElement : INotifyPropertyChanged, ICloneable
{
string ID { get; set; }
int Index { get; set; }
int GroupIndex { get; set; }
string Name { get; set; }
void OnMouseDown(PointF point);
void OnMouseUp(PointF point);
void OnMouseMove(PointF point);
void OnMouseDoubleClick(PointF point);
bool IsIntersect(RectangleF rect);
bool IsEnabled { get; set; }
void Draw(Graphics g);
void Translate(float x, float y);
/// <summary>
/// WPF中标识该对象是否已经加入渲染需要显示
/// </summary>
bool IsShowing { get; set; }
void Initial();
bool IsCreatedDone();
ElementState State { get; set; }
}
}