plcsharp/PLCSharpWpf/VVMs/Vision/LocalVariableItem.cs
2026-07-04 19:27:04 +08:00

195 lines
6.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Newtonsoft.Json;
using Prism.Mvvm;
namespace PLCSharp.VVMs.Vision
{
/// <summary>
/// 局部变量表中的一条记录,保存在 <see cref="VisionParams.Variables"/> 中。
/// </summary>
public class LocalVariableItem : BindableBase
{
private string _Name;
/// <summary>
/// 变量名称
/// </summary>
public string Name
{
get { return _Name; }
set { SetProperty(ref _Name, value); }
}
private string _VarType;
/// <summary>
/// 变量类型:Point / Line / Circle / Rect / Double
/// </summary>
public string VarType
{
get { return _VarType; }
set { SetProperty(ref _VarType, value); }
}
private string _StrValue;
[JsonIgnore]
/// <summary>
/// 变量值的文本表示(UI 显示用,反序列化后自动计算)
/// </summary>
public string StrValue
{
get { return _StrValue; }
set { SetProperty(ref _StrValue, value); }
}
private object? _RawValue;
/// <summary>
/// 原始值
/// </summary>
public object? RawValue
{
get { return _RawValue; }
set
{
try
{
switch (_VarType)
{
case "Pos":
{
var r = new Pos();
if (value is Pos line)
{
r = line;
}
else
{
r = JsonConvert.DeserializeObject<Pos>(value.ToString());
}
SetProperty(ref _RawValue, r);
}
break;
case "Line":
{
var r = new Line();
if (value is Line line)
{
r = line;
}
else
{
r = JsonConvert.DeserializeObject<Line>(value.ToString());
}
SetProperty(ref _RawValue, r);
}
break;
case "Circle":
{
var r = new Circle();
if (value is Circle line)
{
r = line;
}
else
{
r = JsonConvert.DeserializeObject<Circle>(value.ToString());
}
SetProperty(ref _RawValue, r);
}
break;
case "Rect":
{
var r = new Rect();
if (value is Rect line)
{
r = line;
}
else
{
r = JsonConvert.DeserializeObject<Rect>(value.ToString());
}
SetProperty(ref _RawValue, r);
}
break;
case "Double":
SetProperty(ref _RawValue, value);
break;
}
StrValue = FormatValue(_RawValue, _VarType);
}
catch (Exception)
{
}
}
}
/// <summary>
/// 默认构造函数(用于 JSON 反序列化)
/// </summary>
public LocalVariableItem() { }
/// <summary>
/// 用指定名称、类型和初始值构造变量项,自动生成显示文本
/// </summary>
/// <param name="name">变量名称</param>
/// <param name="varType">变量类型(Point/Line/Circle/Rect/Double)</param>
/// <param name="rawValue">初始值</param>
public LocalVariableItem(string name, string varType, object? rawValue)
{
_Name = name;
_VarType = varType;
RawValue = rawValue;
}
private static string FormatValue(object? val, string type)
{
if (val == null) return "null";
try
{
switch (type)
{
case "Pos":
if(val is Pos pt)
return $"位置(X:{pt.X:F4}, Y:{pt.Y:F4}), 角度:({pt.Angle:F4})";
else
return val?.ToString() ?? "";
case "Line":
if (val is Line line)
return $"(X:{line.From.X:F4}, Y:{line.From.Y:F4}) -> (X:{line.To.X:F4}, Y:{line.To.Y:F4})";
else
return val?.ToString() ?? "";
case "Circle":
if (val is Circle c)
return $"中心(X:{c.Center.X:F4} Y:{c.Center.Y:F4}) 半径=({c.Radius:F4})";
else
return val?.ToString() ?? "";
case "Rect":
if (val is Rect r)
return $"中心:(X:{r.Center.X:F4}, Y:{r.Center.Y:F4}) 宽高:({r.Width:F4} × {r.Height:F4}) 角度:({r.Center.Angle:F4})";
else
return val?.ToString() ?? "";
case "Double":
return ((double)val).ToString("F4");
default:
return val?.ToString() ?? "";
}
}
catch (Exception)
{
return val?.ToString() ?? "";
}
}
}
}