界面显示转换后的值

This commit is contained in:
tfarcraw 2026-07-24 15:17:48 +08:00
parent 939984681b
commit be39d460f2
4 changed files with 45 additions and 14 deletions

View File

@ -488,11 +488,11 @@ public partial class VisionConfigViewModel
break;
case VisionFlowType.点到点距离:
newFlow.StringParams["ResultVar"] = "点到点距离";
newFlow.StringParams["PtPt_TransOutVar"] = "点到点距离";
newFlow.StringParams["PtPt_TransOutVar"] = "点到点距离_转换";
break;
case VisionFlowType.点到线距离:
newFlow.StringParams["ResultVar"] = "点到线距离";
newFlow.StringParams["PtLine_TransOutVar"] = "点到线距离";
newFlow.StringParams["PtLine_TransOutVar"] = "点到线距离_转换";
break;
case VisionFlowType.两线交点:
newFlow.StringParams["IntersectResultVar"] = "两线交点";

View File

@ -20,6 +20,7 @@
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
@ -54,8 +55,8 @@
SelectedValuePath="Name" DisplayMemberPath="Name" />
<CheckBox Grid.Row="5" Grid.Column="2" Content="启用" VerticalAlignment="Center"
IsChecked="{Binding SelectVisionFlow.BoolParams[PtLine_EnableTrans], Mode=TwoWay}" />
<TextBlock Grid.Row="6" Text="转换变量" VerticalAlignment="Center" />
<TextBox Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="2"
<TextBlock Grid.Row="7" Text="转换变量" VerticalAlignment="Center" />
<TextBox Grid.Row="7" Grid.Column="1" Grid.ColumnSpan="2"
Text="{Binding SelectVisionFlow.StringParams[PtLine_TransOutVar], Mode=TwoWay}" />
</Grid>
</GroupBox>

View File

@ -49,6 +49,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
// 坐标转换
bool transformApplied = false;
double tDist = 0;
if (item.BoolParams.TryGetValue("PtLine_EnableTrans", out bool doXf) && doXf)
{
if (item.StringParams.TryGetValue("PtLine_TransMat", out var xfName) && !string.IsNullOrEmpty(xfName))
@ -82,17 +83,19 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
double nDenom = Math.Sqrt(nA * nA + nB * nB);
if (nDenom >= 1e-10)
{
double tDist = Math.Abs(nA * nx0 + nB * ny0 + nC) / nDenom;
double _tDist = Math.Abs(nA * nx0 + nB * ny0 + nC) / nDenom;
double nDot = (nx0 - nx1) * (nx2 - nx1) + (ny0 - ny1) * (ny2 - ny1);
double nT = nDot / (nDenom * nDenom);
double nFootX = Math.Round(nx1 + nT * (nx2 - nx1), 3);
double nFootY = Math.Round(ny1 + nT * (ny2 - ny1), 3);
// 覆盖结果
func.Params.ResultDoubles["PointLineDist_Distance"] = Math.Round(tDist, 3);
// UI 显示转换后值
func.Params.ResultDoubles["PointLineDist_Distance"] = Math.Round(_tDist, 3);
func.Params.ResultDoubles["PointLineDist_FootX"] = nFootX;
func.Params.ResultDoubles["PointLineDist_FootY"] = nFootY;
transformApplied = true;
tDist = Math.Round(_tDist, 3);
}
}
}
@ -100,8 +103,21 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
}
if (transformApplied)
dist = func.Params.ResultDoubles["PointLineDist_Distance"];
{
// 转换后距离写入 TransOutVar
var xfVarName = item.StringParams.TryGetValue("PtLine_TransOutVar", out var xov) && !string.IsNullOrEmpty(xov) ? xov : null;
if (xfVarName != null)
{
var xfVar = func.Params.Variables.FirstOrDefault(v => v.Name == xfVarName);
if (xfVar == null)
{
xfVar = new LocalVariableItem(xfVarName, "Double", 0.0);
System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(xfVar));
}
xfVar.RawValue = tDist;
}
}
var resultVarName = item.StringParams.TryGetValue("ResultVar", out var rv) && !string.IsNullOrEmpty(rv) ? rv : "点到线距离";
var resultVar = func.Params.Variables.FirstOrDefault(v => v.Name == resultVarName);
if (resultVar == null)
@ -118,7 +134,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
func.DrawCommands.Add(DrawCommand.FilledCircle(footX, footY, 3, Colors.Cyan));
func.DrawCommands.Add(DrawCommand.TextBlock(
(point.X + footX) / 2, (point.Y + footY) / 2 - 10,
$"{dist:F3}", Colors.Lime, 16));
$"{func.Params.ResultDoubles["PointLineDist_Distance"]:F3}", Colors.Lime, 16));
_ = func.RenderDrawAsync();
item.Flow.Done = true;
return true;

View File

@ -30,6 +30,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
func.Params.ResultDoubles["PointDist_DeltaY"] = Math.Round(pos2.Y - pos1.Y, 3);
// 坐标转换
double? tDist = null;
if (item.BoolParams.TryGetValue("PtPt_EnableTrans", out bool doXf) && doXf)
{
if (item.StringParams.TryGetValue("PtPt_TransMat", out var xfName) && !string.IsNullOrEmpty(xfName))
@ -55,11 +56,12 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
var tp1 = XfPt(pos1.X, pos1.Y);
var tp2 = XfPt(pos2.X, pos2.Y);
double tDist = Math.Sqrt((tp2.X - tp1.X) * (tp2.X - tp1.X) + (tp2.Y - tp1.Y) * (tp2.Y - tp1.Y));
func.Params.ResultDoubles["PointDist_Distance"] = Math.Round(tDist, 3);
double tDist_ = Math.Sqrt((tp2.X - tp1.X) * (tp2.X - tp1.X) + (tp2.Y - tp1.Y) * (tp2.Y - tp1.Y));
// UI 显示转换后值
func.Params.ResultDoubles["PointDist_Distance"] = Math.Round(tDist_, 3);
func.Params.ResultDoubles["PointDist_DeltaX"] = Math.Round(tp2.X - tp1.X, 3);
func.Params.ResultDoubles["PointDist_DeltaY"] = Math.Round(tp2.Y - tp1.Y, 3);
dist = Math.Round(tDist, 3);
tDist = tDist_;
}
}
}
@ -74,13 +76,25 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
}
resultVar.RawValue = dist;
// 转换后距离写入 TransOutVar(如果启用)
if (!string.IsNullOrEmpty(item.StringParams.TryGetValue("PtPt_TransOutVar", out var xov) ? xov : null))
{
var xfVar = func.Params.Variables.FirstOrDefault(v => v.Name == xov);
if (xfVar == null)
{
xfVar = new LocalVariableItem(xov, "Double", 0.0);
System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(xfVar));
}
xfVar.RawValue = tDist.HasValue ? Math.Round(tDist.Value, 3) : double.NaN;
}
// 可视化:画连接线和标注
func.DrawCommands.Add(DrawCommand.Line(pos1.X, pos1.Y, pos2.X, pos2.Y, Colors.Lime, 2));
func.DrawCommands.Add(DrawCommand.FilledCircle(pos1.X, pos1.Y, 4, Colors.Cyan));
func.DrawCommands.Add(DrawCommand.FilledCircle(pos2.X, pos2.Y, 4, Colors.Cyan));
func.DrawCommands.Add(DrawCommand.TextBlock(
(pos1.X + pos2.X) / 2 + 5, (pos1.Y + pos2.Y) / 2 - 5,
$"{dist:F3}", Colors.Lime, 16));
$"{func.Params.ResultDoubles["PointDist_Distance"]:F3}", Colors.Lime, 16));
_ = func.RenderDrawAsync();
item.Flow.Done = true;
return true;