plcsharp/PLCSharpWpf/VVMs/Vision/LocalVariableItem.cs
2026-08-04 01:22:23 +08:00

216 lines
7.5 KiB
C#
Raw Permalink 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 Prism.Mvvm;
using System.Text.Json;
using System.Text.Json.Serialization;
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]
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 pos)
{
r = pos;
}
else
{
if (value == null) break;
r = JsonSerializer.Deserialize<Pos>(value.ToString(), PLCSharp.Core.Tools.JsonOptions.Default);
}
SetProperty(ref _RawValue, r);
}
break;
case "Line":
{
var r = new Line();
if (value is Line line)
{
r = line;
}
else
{
if (value == null) break;
r = JsonSerializer.Deserialize<Line>(value.ToString(), PLCSharp.Core.Tools.JsonOptions.Default);
}
SetProperty(ref _RawValue, r);
}
break;
case "Circle":
{
var r = new Circle();
if (value is Circle line)
{
r = line;
}
else
{
if (value == null) break;
r = JsonSerializer.Deserialize<Circle>(value.ToString(), PLCSharp.Core.Tools.JsonOptions.Default);
}
SetProperty(ref _RawValue, r);
}
break;
case "Rect":
{
var r = new Rect();
if (value is Rect rect)
{
r = rect;
}
else
{
if (value == null) break;
r = JsonSerializer.Deserialize<Rect>(value.ToString(), PLCSharp.Core.Tools.JsonOptions.Default);
}
SetProperty(ref _RawValue, r);
}
break;
case "String":
case "Double":
SetProperty(ref _RawValue, value);
break;
case "Barcode":
{
var r = new Barcode();
if (value is Barcode barcode)
{
r = barcode;
}
else
{
if (value == null) break;
r = JsonSerializer.Deserialize<Barcode>(value.ToString(), PLCSharp.Core.Tools.JsonOptions.Default);
}
SetProperty(ref _RawValue, r);
}
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 "String":
return val?.ToString() ?? "";
default:
return val?.ToString() ?? "";
}
}
catch (Exception)
{
return val?.ToString() ?? "";
}
}
}
}