using System.Text.Json;
using System.Text.Json.Serialization;
using OpenCvSharp;
using OpenCvSharp.WpfExtensions;
using PLCSharp.Core.Common;
using PLCSharp.Core.Tools;
using PLCSharp.Core.UserControls;
using PLCSharp.VVMs.Homepage.TableControl;
using Prism.Mvvm;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Brush = System.Windows.Media.Brush;
using FontFamily = System.Windows.Media.FontFamily;
namespace PLCSharp.VVMs.Homepage
{
///
/// Custom控件
///
public class CustomControl : BindableBase
{
///
/// 唯一标识
///
[Key]
public Guid ID { get; set; } = Guid.NewGuid();
///
/// 配方标识
///
public Guid RecipeID { get; set; }
private string _Name;
///
/// 自定义控件属性
///
public string Name
{
get { return _Name; }
set
{
if (_Name != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _Name, value);
}
}
private ControlType _Type;
///
/// 自定义控件属性
///
public ControlType Type
{
get { return _Type; }
set { SetProperty(ref _Type, value); }
}
private string _Prompt;
///
/// 提示
///
[NotMapped]
public string Prompt
{
get { return _Prompt; }
set { SetProperty(ref _Prompt, value); }
}
private int _Row;
///
/// 自定义控件属性
///
public int Row
{
get { return _Row; }
set { SetProperty(ref _Row, value); }
}
private int _Column;
///
/// 自定义控件属性
///
public int Column
{
get { return _Column; }
set { SetProperty(ref _Column, value); }
}
private int _RowSpan;
///
/// 自定义控件属性
///
public int RowSpan
{
get { return _RowSpan; }
set { SetProperty(ref _RowSpan, value); }
}
private int _ColumnSpan;
///
/// 自定义控件属性
///
public int ColumnSpan
{
get { return _ColumnSpan; }
set { SetProperty(ref _ColumnSpan, value); }
}
private string _Comment;
///
/// 备注
///
public string Comment
{
get { return _Comment; }
set
{
if (_Comment != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _Comment, value);
}
}
#region Params
///
/// 序列化Params
///
[Column("Params")]
public string SerializedParams
{
get => JsonSerializer.Serialize(Params, JsonOptions.Default); // 自动序列化
set => Params = JsonSerializer.Deserialize(value, JsonOptions.Default); // 自动反序列化
}
private ControlParams _Params;
///
/// 参数集合
///
[JsonIgnore]
[NotMapped]
public ControlParams Params
{
get
{
_Params ??= new ControlParams();
return _Params;
}
set
{
SetProperty(ref _Params, value);
}
}
#endregion
private ObservableCollection _RangeValues = [];
///
/// RangeValues
///
[NotMapped]
public ObservableCollection RangeValues
{
get { return _RangeValues; }
set { if (value != null) SetProperty(ref _RangeValues, value); }
}
///
/// 序列化RangeValues
///
[Column("RangeValues")]
public string SerializedRangeValues
{
get => JsonSerializer.Serialize(RangeValues, JsonOptions.Default); // 自动序列化
set => RangeValues = JsonSerializer.Deserialize>(value, JsonOptions.Default); // 自动反序列化
}
///
/// 设置CellState
///
/// 当前索引
/// cellState
public void SetCellState(int index, CellState cellState)
{
var cell = Params.CellInfos.Where(c => c.Index == index).FirstOrDefault();
if (cell != null)
{
cell.State = cellState;
}
else
{
throw new Exception($"未找到索引{index}的单元格");
}
}
private WriteableBitmap _ImgSrc;
///
/// 显示的图像源
///
[NotMapped]
public WriteableBitmap ImgSrc
{
get { return _ImgSrc; }
set { SetProperty(ref _ImgSrc, value); }
}
private static void Dispatch(Action action)
{
_= Application.Current.Dispatcher.InvokeAsync(action);
}
///
/// 显示的图像矩阵
///
/// _显示矩阵
public void ShowMat(Mat _ShowMat)
{
if (_ShowMat == null) return;
Dispatch(() =>
{
if (_ImgSrc != null
&& _ShowMat.Width == _ImgSrc.PixelWidth
&& _ShowMat.Height == _ImgSrc.PixelHeight
&& _ShowMat.Channels() == _ImgSrc.Format.BitsPerPixel / 8
)
{
WriteableBitmapConverter.ToWriteableBitmap(_ShowMat, _ImgSrc);
}
else
{
ImgSrc = WriteableBitmapConverter.ToWriteableBitmap(_ShowMat);
}
});
}
///
/// 显示圆
///
/// 中心点X
/// 中心点Y
/// 半径X
/// 半径Y
/// 颜色
/// 线径
public void ShowEllipse(double centerRow, double centerColumn, double radiusRow, double radiusColumn, Brush color, double thickness = 3)
{
Dispatch(() =>
{
Path path = new()
{
StrokeThickness = thickness,
Stroke = color
};
var g = new EllipseGeometry
{
Center = new() { X = centerRow, Y = centerColumn },
RadiusX = radiusRow,
RadiusY = radiusColumn
};
path.Data = g;
var ui = UIElement as ImageEdit;
if (ui == null) return;
ui.Draw(path);
});
}
///
/// 显示线段
///
///
///
///
///
///
///
///
public void ShowLine(double fromRow, double fromColumn, double toRow, double toColumn, Brush color, double thickness = 3)
{
Dispatch(() =>
{
Path path = new()
{
StrokeThickness = thickness,
Stroke = color//设置Path基础属性
};
var g = new LineGeometry
{
StartPoint = new() { X = fromRow, Y = fromColumn },
EndPoint = new() { X = toRow, Y = toColumn },
};
path.Data = g;
if (UIElement is not ImageEdit ui) return;
ui.Draw(path);
});
}
private static XPoint[] GetArrowPoints(XPoint end, XPoint start, float size)
{
XPoint[] points = new XPoint[2];
// 计算箭头的角度
double angle = Math.Atan2(end.Y - start.Y, end.X - start.X);
// 计算箭头顶点的位置
// 计算箭头左侧端点位置
points[0] = new XPoint
{
X = (float)(end.X - size * Math.Cos(angle + Math.PI / 8)),
Y = (float)(end.Y - size * Math.Sin(angle + Math.PI / 8))
};
// 计算箭头右侧端点位置
points[1] = new XPoint
{
X = (float)(end.X - size * Math.Cos(angle - Math.PI / 8)),
Y = (float)(end.Y - size * Math.Sin(angle - Math.PI / 8))
};
return points;
}
///
/// 显示Arrow直线
///
/// 名称
/// fromRow
/// fromColumn
/// toRow
/// toColumn
/// color
/// thickness
public void ShowArrowLine(string name, double fromRow, double fromColumn, double toRow, double toColumn, Brush color, double thickness = 3)
{
Dispatch(() =>
{
Path path = new()
{
StrokeThickness = thickness,
Stroke = color,
Tag = name
};
var g = new LineGeometry
{
StartPoint = new() { X = fromColumn, Y = fromRow },
EndPoint = new() { X = toColumn, Y = toRow },
};
path.Data = g;
var ui = UIElement as ImageEdit;
if (ui == null) return;
ui.Draw(path);
var headlen = 30;//箭头线的长度
var startPoint = new XPoint() { X = fromColumn, Y = fromRow };
var endPoint = new XPoint() { X = toColumn, Y = toRow };
XPoint[] points = GetArrowPoints(endPoint, startPoint, headlen);
var line2 = new Line
{
Stroke = color,
StrokeThickness = thickness,
X1 = points[1].X,
Y1 = points[1].Y,
X2 = toColumn,
Y2 = toRow,
Tag = name
};
ui.Draw(line2);
var line3 = new Line
{
Stroke = color,
StrokeThickness = thickness,
X1 = points[0].X,
Y1 = points[0].Y,
X2 = toColumn,
Y2 = toRow,
Tag = name
};
ui.Draw(line3);
});
}
///
/// 显示Cross
///
/// 点位Row
/// 点位Column
/// 宽度
/// 高度
/// color
/// thickness
public void ShowCross(int pointRow, int pointColumn, int width, int height, Brush color, double thickness = 3)
{
Dispatch(() =>
{
var g1 = new LineGeometry
{
StartPoint = new() { X = pointRow - width / 2, Y = pointColumn },
EndPoint = new() { X = pointRow + width / 2, Y = pointColumn },
};
var g2 = new LineGeometry
{
StartPoint = new() { X = pointRow, Y = pointColumn - height / 2 },
EndPoint = new() { X = pointRow, Y = pointColumn + height / 2 },
};
var ui = UIElement as ImageEdit;
if (ui == null) return;
Path path1 = new()
{
StrokeThickness = thickness,
Stroke = color,
Data = g1
};
ui.Draw(path1);
Path path2 = new()
{
StrokeThickness = thickness,
Stroke = color
};
path2.Data = g2;
ui.Draw(path2);
});
}
///
/// 显示Rectangle
///
/// 点位Row
/// 点位Column
/// 宽度
/// 高度
/// color
/// thickness
public void ShowRectangle(int pointRow, int pointColumn, int width, int height, Brush color, double thickness = 3)
{
Dispatch(() =>
{
var g1 = new RectangleGeometry
{
Rect = new System.Windows.Rect(pointColumn - width / 2, pointRow - height / 2, width, height),
RadiusX = 0,
RadiusY = 0
};
Path path1 = new()
{
StrokeThickness = thickness,
Stroke = color
};
path1.Data = g1;
var ui = UIElement as ImageEdit;
if (ui == null) return;
ui.Draw(path1);
});
}
///
/// 显示圆弧
///
/// 启动点位
/// middle点位
/// end点位
/// color
/// thickness
public void ShowArc(XPoint startPoint, XPoint middlePoint, XPoint endPoint, Brush color, double thickness = 3)
{
Dispatch(() =>
{
PathGeometry geometry = new();
PathFigure figure = new()
{
StartPoint = new() { X = startPoint.X, Y = startPoint.Y }
};
figure.Segments.Add(new ArcSegment()
{
Point = new() { X = endPoint.X, Y = endPoint.Y },
Size = new() { Width = middlePoint.X, Height = middlePoint.Y },
});
geometry.Figures.Add(figure);
Path path1 = new()
{
StrokeThickness = thickness,
Stroke = color
};
path1.Data = geometry;
var ui = UIElement as ImageEdit;
if (ui == null) return;
ui.Draw(path1);
});
}
///
/// 显示Text
///
/// 点位Row
/// 点位Column
/// text
/// font尺寸
/// color
/// thickness
public void ShowText(int pointRow, int pointColumn, string text, double fontSize, Brush color, double thickness = 3)
{
Dispatch(() =>
{
TextBlock textBlock = new()
{
Text = text,
FontSize = fontSize,
FontFamily = new FontFamily("Arial"),
Foreground = color
};
var ui = UIElement as ImageEdit;
if (ui == null) return;
ui.Draw(textBlock, pointRow, pointColumn);
});
}
///
/// 设置CellState
///
/// row
/// column
/// cellState
public void SetCellState(int row, int column, CellState cellState)
{
var cell = Params.CellInfos.Where(c => c.Row == row && c.Column == column).FirstOrDefault();
if (cell != null)
{
cell.State = cellState;
}
else
{
throw new Exception($"未找到行{row}列{column}的单元格");
}
}
///
/// 设置测量值 — 供外部模块(视觉/运动)按名称推送测量结果
///
/// RangeValue 名称
/// 测量值
public void SetMeasurementValue(string name, double value)
{
var rv = RangeValues.FirstOrDefault(r => r.Name == name);
if (rv != null)
{
Dispatch(() =>
{
rv.ShowValue = value;
rv.GetResult();
});
}
else
{
throw new Exception($"未找到名称为 '{name}' 的 RangeValue");
}
}
///
/// UIElement
///
[NotMapped]
public UIElement UIElement { get; set; }
///
/// 控件Params
///
public class ControlParams : BindableBase
{
private int _Rows;
///
/// 自定义控件属性
///
public int Rows
{
get { return _Rows; }
set { SetProperty(ref _Rows, value); }
}
private int _Columns;
///
/// 自定义控件属性
///
public int Columns
{
get { return _Columns; }
set { SetProperty(ref _Columns, value); }
}
private int _Layout;
///
/// 左 -> 右 上 -> 下 0
/// 左 -> 右 下 -> 上 1
/// 右 -> 左 上 -> 下 2
/// 右 -> 左 下 -> 上 3
/// 上 -> 下 左 -> 右 4
/// 上 -> 下 右 -> 左 5
/// 下 -> 上 左 -> 右 6
/// 下 -> 上 右 -> 左 7
///
public int Layout
{
get { return _Layout; }
set { SetProperty(ref _Layout, value); }
}
private ObservableCollection _CellInfos = [];
///
/// CellInfos
///
public ObservableCollection CellInfos
{
get { return _CellInfos; }
set
{
if (value != null)
SetProperty(ref _CellInfos, value);
}
}
}
}
public enum ControlType
{
Image,
State,
RangeValueTalbe,
}
}