10.20PLC+相机2.3视觉修改

This commit is contained in:
17860779768
2025-08-25 16:33:58 +08:00
committed by Maikouce China
commit dca4b2afac
52 changed files with 11698 additions and 0 deletions

View File

@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.ComponentModel.TypeConverter;
namespace Check.Main.Common
{
/// <summary>
/// 一个自定义的 TypeConverter用于在 PropertyGrid 中为模型选择提供一个下拉列表。
/// </summary>
public class ModelSelectionConverter : Int32Converter // 我们转换的是整数 (ModelId)
{
// 告诉 PropertyGrid我们支持从一个标准值集合中进行选择
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
// 告诉 PropertyGrid我们只允许用户从列表中选择不允许手动输入
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
// 这是最核心的方法:提供标准值的列表
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
try
{
// 从中央配置管理器获取所有已配置的模型
var modelSettings = ConfigurationManager.GetCurrentConfig().ModelSettings;
if (modelSettings == null || modelSettings.Count == 0)
{
// 如果没有模型,返回一个包含默认值 "0"(代表“无”)的列表
return new StandardValuesCollection(new[] { 0 });
}
// 使用LINQ从模型列表中提取所有模型的ID
// .Prepend(0) 在列表开头添加一个 "0",代表“未选择”的选项
var modelIds = modelSettings.Select(m => m.Id).Prepend(0).ToArray();
return new StandardValuesCollection(modelIds);
}
catch
{
// 如果在获取过程中发生任何异常,返回一个安全的默认值
return new StandardValuesCollection(new[] { 0 });
}
}
// --- 为了让下拉列表显示模型名称而不是ID我们还需要重写以下方法 ---
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
}
// 将模型ID转换为其对应的名称字符串以便在UI上显示
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is int modelId)
{
if (modelId == 0)
{
return "未选择"; // 对ID为0的特殊处理
}
// 查找ID对应的模型名称
var model = ConfigurationManager.GetCurrentConfig()
.ModelSettings?
.FirstOrDefault(m => m.Id == modelId);
// 返回 "名称 (ID)" 的格式,更清晰
return model != null ? $"{model.Name} (ID: {model.Id})" : "未知模型";
}
return base.ConvertTo(context, culture, value, destinationType);
}
// 将用户在UI上选择的名称字符串转换回其对应的模型ID以便保存
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string displayString)
{
if (displayString == "未选择")
{
return 0;
}
// 从 "名称 (ID: X)" 格式中解析出ID
if (displayString.Contains("(ID: ") && displayString.EndsWith(")"))
{
var startIndex = displayString.IndexOf("(ID: ") + 5;
var endIndex = displayString.Length - 1;
var idString = displayString.Substring(startIndex, endIndex - startIndex);
if (int.TryParse(idString, out int modelId))
{
return modelId;
}
}
}
return base.ConvertFrom(context, culture, value);
}
}
}