diff --git a/PLCSharpWpf/Core/Prism/ObservableDictionary.cs b/PLCSharpWpf/Core/Prism/ObservableDictionary.cs index b10e772..d377bcd 100644 --- a/PLCSharpWpf/Core/Prism/ObservableDictionary.cs +++ b/PLCSharpWpf/Core/Prism/ObservableDictionary.cs @@ -1,4 +1,4 @@ -#nullable enable +#nullable enable using System.Collections.Specialized; using System.ComponentModel; @@ -170,7 +170,7 @@ namespace PLCSharp.Core.Prism if (dispatcher != null && !dispatcher.CheckAccess()) { #pragma warning disable VSTHRD001 // 同步事件通知无法 async,与本项目其他 Dispatcher.Invoke 保持一致 - dispatcher.Invoke((Action)Raise); + dispatcher.Invoke(Raise); #pragma warning restore VSTHRD001 } else diff --git a/PLCSharpWpf/Core/Tools/WpfTool.cs b/PLCSharpWpf/Core/Tools/WpfTool.cs index f4d5138..83ea10f 100644 --- a/PLCSharpWpf/Core/Tools/WpfTool.cs +++ b/PLCSharpWpf/Core/Tools/WpfTool.cs @@ -1,4 +1,4 @@ -using System.Collections.Concurrent; +using System.Collections.Concurrent; using System.Drawing; using System.Windows; using System.Windows.Media; @@ -30,53 +30,53 @@ namespace PLCSharp.Core.Tools PointsQueue.TryDequeue(out PointF point); Points.Add(point); } - Application.Current?.Dispatcher.Invoke(delegate - { - WriteableBitmap writeableBitmap = _imgSource; - int width = (int)writeableBitmap.Width; - int height = (int)writeableBitmap.Height; - writeableBitmap.Lock(); - using var bitmap = new Bitmap(width, height, writeableBitmap.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, writeableBitmap.BackBuffer); - using Graphics graphics = Graphics.FromImage(bitmap); - graphics.Clear(System.Drawing.Color.Black); - //System.Drawing.FontFamily family = new("微软雅黑"); + await Application.Current?.Dispatcher.BeginInvoke(() => + { + WriteableBitmap writeableBitmap = _imgSource; + int width = (int)writeableBitmap.Width; + int height = (int)writeableBitmap.Height; + writeableBitmap.Lock(); + using var bitmap = new Bitmap(width, height, writeableBitmap.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, writeableBitmap.BackBuffer); + using Graphics graphics = Graphics.FromImage(bitmap); + graphics.Clear(System.Drawing.Color.Black); + //System.Drawing.FontFamily family = new("微软雅黑"); - //求最小点,看是否为负;为负则整体偏移 - var min_x = Points.Min(p => p.X); - var min_y = Points.Min(p => p.Y); + //求最小点,看是否为负;为负则整体偏移 + var min_x = Points.Min(p => p.X); + var min_y = Points.Min(p => p.Y); - var points = Points.ToArray(); - if (min_x < 0) - for (int i = 0; i < Points.Count; i++) - { - points[i] = new PointF(Points[i].X - min_x, Points[i].Y); - } - if (min_y < 0) - for (int i = 0; i < Points.Count; i++) - { - points[i] = new(Points[i].X, Points[i].Y - min_y); - } - for (int i = 0; i < Points.Count; i++) - { - points[i] = new(points[i].X + 100, points[i].Y + 100); - } - var max_x = points.Max(p => p.X); - var max_y = points.Max(p => p.Y); - var scalex = max_x / width; - var scaley = max_y / height; - var scale = Math.Max(scaley, scalex); - foreach (var point in points) - { + var points = Points.ToArray(); + if (min_x < 0) + for (int i = 0; i < Points.Count; i++) + { + points[i] = new PointF(Points[i].X - min_x, Points[i].Y); + } + if (min_y < 0) + for (int i = 0; i < Points.Count; i++) + { + points[i] = new(Points[i].X, Points[i].Y - min_y); + } + for (int i = 0; i < Points.Count; i++) + { + points[i] = new(points[i].X + 100, points[i].Y + 100); + } + var max_x = points.Max(p => p.X); + var max_y = points.Max(p => p.Y); + var scalex = max_x / width; + var scaley = max_y / height; + var scale = Math.Max(scaley, scalex); + foreach (var point in points) + { - graphics.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.White), new Rectangle((int)(point.X / scale), (int)(point.Y / scale), 1, 1)); + graphics.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.White), new Rectangle((int)(point.X / scale), (int)(point.Y / scale), 1, 1)); - } + } - graphics.Flush(); - writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height)); - writeableBitmap.Unlock(); + graphics.Flush(); + writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height)); + writeableBitmap.Unlock(); - }); + }); } } diff --git a/PLCSharpWpf/PLCSharp.csproj b/PLCSharpWpf/PLCSharp.csproj index df79201..839e15a 100644 --- a/PLCSharpWpf/PLCSharp.csproj +++ b/PLCSharpWpf/PLCSharp.csproj @@ -95,7 +95,7 @@ Code - + Code diff --git a/PLCSharpWpf/VVMs/Connects/ModbusRtu/ModbusRtuConfigViewModel.cs b/PLCSharpWpf/VVMs/Connects/ModbusRtu/ModbusRtuConfigViewModel.cs index a35d8b9..47f24f9 100644 --- a/PLCSharpWpf/VVMs/Connects/ModbusRtu/ModbusRtuConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Connects/ModbusRtu/ModbusRtuConfigViewModel.cs @@ -1,4 +1,4 @@ -using PLCSharp.Core.Common; +using PLCSharp.Core.Common; using PLCSharp.Core.Prism; using PLCSharp.VVMs.Connects.ModbusTcp; using Prism.Commands; @@ -180,7 +180,7 @@ namespace PLCSharp.VVMs.Connects.ModbusRtu { if (Client.LogQueue.TryDequeue(out string log)) { - System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => + _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => { Logs.Add(new ErrorLog(log)); })); diff --git a/PLCSharpWpf/VVMs/Connects/ModbusTcp/ModbusClientConfigViewModel.cs b/PLCSharpWpf/VVMs/Connects/ModbusTcp/ModbusClientConfigViewModel.cs index 85a87b7..a2d940c 100644 --- a/PLCSharpWpf/VVMs/Connects/ModbusTcp/ModbusClientConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Connects/ModbusTcp/ModbusClientConfigViewModel.cs @@ -1,4 +1,4 @@ -using DryIoc; +using DryIoc; using PLCSharp.Core.Common; using PLCSharp.Core.Prism; using Prism.Commands; @@ -89,7 +89,7 @@ namespace PLCSharp.VVMs.Connects.ModbusTcp while (!worker.CancellationPending) { - Thread.Sleep(10); + Thread.Sleep(100); if (Client == null) continue; // 日志轮询 @@ -97,7 +97,7 @@ namespace PLCSharp.VVMs.Connects.ModbusTcp { if (Client.LogQueue.TryDequeue(out string log)) { - System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => + _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => { Logs.Add(new ErrorLog(log)); })); diff --git a/PLCSharpWpf/VVMs/Connects/ModbusTcp/ModbusServerConfigViewModel.cs b/PLCSharpWpf/VVMs/Connects/ModbusTcp/ModbusServerConfigViewModel.cs index 5ef0a44..1ff0a86 100644 --- a/PLCSharpWpf/VVMs/Connects/ModbusTcp/ModbusServerConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Connects/ModbusTcp/ModbusServerConfigViewModel.cs @@ -1,4 +1,4 @@ -using DryIoc; +using DryIoc; using PLCSharp.Core.Common; using PLCSharp.Core.Prism; using Prism.Commands; @@ -193,17 +193,17 @@ namespace PLCSharp.VVMs.Connects.ModbusTcp while (!worker.CancellationPending) { - Thread.Sleep(10); + Thread.Sleep(100); if (Server != null && Server.LogSwitch) { if (!Server.LogQueue.IsEmpty) { if (Server.LogQueue.TryDequeue(out string log)) { - System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => - { - Logs.Add(new ErrorLog(log)); - })); + _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => + { + Logs.Add(new ErrorLog(log)); + })); } } } diff --git a/PLCSharpWpf/VVMs/Connects/SerialPort/FreeSerialProtocolConfigViewModel.cs b/PLCSharpWpf/VVMs/Connects/SerialPort/FreeSerialProtocolConfigViewModel.cs index f1ede22..18f024e 100644 --- a/PLCSharpWpf/VVMs/Connects/SerialPort/FreeSerialProtocolConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Connects/SerialPort/FreeSerialProtocolConfigViewModel.cs @@ -1,4 +1,4 @@ -using PLCSharp.Core.Common; +using PLCSharp.Core.Common; using PLCSharp.Core.Prism; using Prism.Commands; using Prism.Dialogs; @@ -94,7 +94,7 @@ namespace PLCSharp.VVMs.Connects.SerialPort while (!worker.CancellationPending) { - Thread.Sleep(10); + Thread.Sleep(100); if (Client == null) continue; // 日志轮询 @@ -102,7 +102,7 @@ namespace PLCSharp.VVMs.Connects.SerialPort { if (Client.LogQueue.TryDequeue(out string log)) { - System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => + _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => { Logs.Add(new ErrorLog(log)); })); diff --git a/PLCSharpWpf/VVMs/Connects/Socket/SocketClientConfigViewModel.cs b/PLCSharpWpf/VVMs/Connects/Socket/SocketClientConfigViewModel.cs index dc6359a..924e6a7 100644 --- a/PLCSharpWpf/VVMs/Connects/Socket/SocketClientConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Connects/Socket/SocketClientConfigViewModel.cs @@ -92,7 +92,7 @@ namespace PLCSharp.VVMs.Connects.Socket var worker = (BackgroundWorker)sender; while (!worker.CancellationPending) { - Thread.Sleep(10); + Thread.Sleep(100); if (Client != null) { @@ -102,9 +102,9 @@ namespace PLCSharp.VVMs.Connects.Socket { if ((Client as SocketClient).LogQueue.TryDequeue(out string log)) - System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => + _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => { - Logs.Add(new(log)); + Logs.Add(new ErrorLog(log)); })); } } diff --git a/PLCSharpWpf/VVMs/Connects/Socket/SocketServerConfigViewModel.cs b/PLCSharpWpf/VVMs/Connects/Socket/SocketServerConfigViewModel.cs index 3940def..f996fac 100644 --- a/PLCSharpWpf/VVMs/Connects/Socket/SocketServerConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Connects/Socket/SocketServerConfigViewModel.cs @@ -95,24 +95,21 @@ namespace PLCSharp.VVMs.Connects.Socket while (!worker.CancellationPending) { - Thread.Sleep(10); + Thread.Sleep(100); if (Server != null) { - for (int i = 0; i < Server.Clients.Count; i++) { Server.Clients[i].Online = NetTool.PingIP(Server.Clients[i].IP_SerialPort); - } if (Server.LogSwitch) { if (!(Server as SocketServer).LogQueue.IsEmpty) { if ((Server as SocketServer).LogQueue.TryDequeue(out string log)) - - System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => + _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => { - Logs.Add(new(log)); + Logs.Add(new ErrorLog(log)); })); } } diff --git a/PLCSharpWpf/VVMs/Homepage/CustomControl.cs b/PLCSharpWpf/VVMs/Homepage/CustomControl.cs index 057a646..85eafa7 100644 --- a/PLCSharpWpf/VVMs/Homepage/CustomControl.cs +++ b/PLCSharpWpf/VVMs/Homepage/CustomControl.cs @@ -212,9 +212,9 @@ namespace PLCSharp.VVMs.Homepage get { return _ImgSrc; } set { SetProperty(ref _ImgSrc, value); } } - private void Dispatch(Action action) + private static void Dispatch(Action action) { - Application.Current.Dispatcher.Invoke(action); + _= Application.Current.Dispatcher.BeginInvoke(action); } /// diff --git a/PLCSharpWpf/VVMs/Vision/Camera/CameraConfigViewModel.cs b/PLCSharpWpf/VVMs/Vision/Camera/CameraConfigViewModel.cs index a6e59ed..a725312 100644 --- a/PLCSharpWpf/VVMs/Vision/Camera/CameraConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Vision/Camera/CameraConfigViewModel.cs @@ -1,4 +1,4 @@ -using OpenCvSharp; +using OpenCvSharp; using OpenCvSharp.WpfExtensions; using PLCSharp.Core.Common; using PLCSharp.Core.Prism; @@ -46,7 +46,7 @@ namespace PLCSharp.VVMs.Vision.Camera { SetProperty(ref _ShowMat, value); if (value == null) return; - System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => + _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => { if (_ImgSrc != null && _ShowMat.Width == _ImgSrc.PixelWidth diff --git a/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs b/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs index 88c0ba5..4f5e9f2 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs @@ -21,7 +21,6 @@ using System.Reflection; using System.Threading; using System.Windows; using System.Windows.Controls; -using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Media; using System.Windows.Media.Imaging; @@ -53,7 +52,7 @@ namespace PLCSharp.VVMs.Vision ImageEdit.SetBinding(ImageEdit.ImageSourceProperty, binding); } - + private ImageEdit _ImageEdit; /// @@ -78,7 +77,7 @@ namespace PLCSharp.VVMs.Vision /// public override void OnDialogClosed() { - SelectedVisionFunction.EditImageEdit = null; + SelectedVisionFunction.EditImageEdit = null; } #region 视觉流程 @@ -121,8 +120,8 @@ namespace PLCSharp.VVMs.Vision ShowMat = SelectedVisionFunction.Src; - - + + } private DelegateCommand _Save; /// @@ -182,7 +181,7 @@ namespace PLCSharp.VVMs.Vision ShowMat = SelectedVisionFunction.Src; - + } private DelegateCommand _Manage; /// @@ -459,7 +458,7 @@ namespace PLCSharp.VVMs.Vision SelectVisionFlow.StringParams["Variable"] = value.Name; } } - + private ImageData _SelectImageData; /// @@ -619,7 +618,7 @@ namespace PLCSharp.VVMs.Vision #endregion - + #region 局部图像管理 private DelegateCommand _MatManage; @@ -834,31 +833,31 @@ namespace PLCSharp.VVMs.Vision { SetProperty(ref _ShowMat, value); if (value == null) return; - System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => - { - try - { - if (_ImgSrc != null - && _ShowMat.Width == _ImgSrc.PixelWidth - && _ShowMat.Height == _ImgSrc.PixelHeight - && _ShowMat.Channels() == _ImgSrc.Format.BitsPerPixel / 8 - ) - { - WriteableBitmapConverter.ToWriteableBitmap(_ShowMat, _ImgSrc); + _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => + { + try + { + 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); - } - } - catch (Exception ex) - { - SendErr($"图像转换失败: {ex.Message}"); + } + else + { + ImgSrc = WriteableBitmapConverter.ToWriteableBitmap(_ShowMat); + } + } + catch (Exception ex) + { + SendErr($"图像转换失败: {ex.Message}"); - } + } - })); + })); } @@ -945,7 +944,7 @@ namespace PLCSharp.VVMs.Vision } - + @@ -967,7 +966,7 @@ namespace PLCSharp.VVMs.Vision { SendInfoDialog("请先获取图片!"); return; - } + } try { @@ -1322,9 +1321,9 @@ namespace PLCSharp.VVMs.Vision SendInfoDialog("标定成功"); } catch (Exception ex) { SendInfoDialog(ex.Message); } - } - - + } + + private AsyncDelegateCommand _ComputeTwoLineTemplate; public AsyncDelegateCommand ComputeTwoLineTemplate => @@ -1906,7 +1905,7 @@ namespace PLCSharp.VVMs.Vision imageEdit.Draw(crossV); } -#endregion + #endregion diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Access/ImageToFile .xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Access/ImageToFile.xaml similarity index 100% rename from PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Access/ImageToFile .xaml rename to PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Access/ImageToFile.xaml diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Access/ImageToFile .xaml.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Access/ImageToFile.xaml.cs similarity index 100% rename from PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Access/ImageToFile .xaml.cs rename to PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Access/ImageToFile.xaml.cs diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindCircleHandler.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindCircleHandler.cs index 1f07101..9df5565 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindCircleHandler.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindCircleHandler.cs @@ -222,7 +222,11 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm var offVarName = item.StringParams.TryGetValue("CircleOffsetVar", out var ov) && !string.IsNullOrEmpty(ov) ? ov : "找圆_Offset"; var offVar = func.Params.Variables.FirstOrDefault(v => v.Name == offVarName); - if (offVar == null) { offVar = new LocalVariableItem(offVarName, "Pos", new Pos()); System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(offVar)); } + if (offVar == null) + { + offVar = new LocalVariableItem(offVarName, "Pos", new Pos()); + System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(offVar)); + } offVar.RawValue = new Pos(offX, offY, offR, 0); } diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindEdgeHandler.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindEdgeHandler.cs index a709f18..64511c5 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindEdgeHandler.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindEdgeHandler.cs @@ -93,7 +93,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm var inls = RansacInliers(edgePoints, inlierTh); // 可视化 — 写入 DrawCommands - + var cyan = Color.FromArgb(128, 0, 255, 255); var lime = Color.FromArgb(128, 0, 255, 0); var magenta = Color.FromArgb(128, 255, 0, 255); @@ -198,7 +198,11 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm var offVarName = item.StringParams.TryGetValue("LineOffsetVar", out var ov) && !string.IsNullOrEmpty(ov) ? ov : "找线_Offset"; var offVar = func.Params.Variables.FirstOrDefault(v => v.Name == offVarName); - if (offVar == null) { offVar = new LocalVariableItem(offVarName, "Pos", new Pos()); System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(offVar)); } + if (offVar == null) + { + offVar = new LocalVariableItem(offVarName, "Pos", new Pos()); + System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(offVar)); + } offVar.RawValue = new Pos(offsetX, offsetY, 0, offsetAngle); } _ = func.RenderDrawAsync(); @@ -231,8 +235,10 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm vx /= len; vy /= len; int cnt = 0; for (int i = 0; i < n; i++) - { double dx = points[i].X - points[i1].X, dy = points[i].Y - points[i1].Y; - if (Math.Abs(dx * vy - dy * vx) <= threshold) cnt++; } + { + double dx = points[i].X - points[i1].X, dy = points[i].Y - points[i1].Y; + if (Math.Abs(dx * vy - dy * vx) <= threshold) cnt++; + } if (cnt > bestCnt) { bestCnt = cnt; bvx = vx; bvy = vy; bx = points[i1].X; by = points[i1].Y; } } var result = points.Where(p => Math.Abs((p.X - bx) * bvy - (p.Y - by) * bvx) <= threshold).ToList(); diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindRectHandler.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindRectHandler.cs index 617d8c5..901bb26 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindRectHandler.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindRectHandler.cs @@ -1,6 +1,4 @@ using OpenCvSharp; -using System.Linq; -using System.Windows.Media; namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm { @@ -227,7 +225,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm var rr = new RotatedRect(new Point2f((float)fitCx, (float)fitCy), new Size2f((float)fitW, (float)fitH), (float)fitAngle); var rrPts = rr.Points(); for (int i = 0; i < 4; i++) - func.DrawCommands.Add(DrawCommand.Line(rrPts[i].X, rrPts[i].Y, rrPts[(i+1)%4].X, rrPts[(i+1)%4].Y, System.Windows.Media.Color.FromArgb(128, 255, 0, 255), 3)); + func.DrawCommands.Add(DrawCommand.Line(rrPts[i].X, rrPts[i].Y, rrPts[(i + 1) % 4].X, rrPts[(i + 1) % 4].Y, System.Windows.Media.Color.FromArgb(128, 255, 0, 255), 3)); func.DrawCommands.Add(DrawCommand.FilledCircle(fitCx, fitCy, 4, System.Windows.Media.Color.FromArgb(128, 255, 0, 255))); // 保存结果 func.Params.ResultDoubles["InlierCount"] = inliers; @@ -272,7 +270,11 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm { var offsetVarName = item.StringParams.TryGetValue("RectOffsetVar", out var ov) && !string.IsNullOrEmpty(ov) ? ov : "找矩形_Offset"; var offsetVar = func.Params.Variables.FirstOrDefault(v => v.Name == offsetVarName); - if (offsetVar == null) { offsetVar = new LocalVariableItem(offsetVarName, "Pos", new Pos()); System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(offsetVar)); } + if (offsetVar == null) + { + offsetVar = new LocalVariableItem(offsetVarName, "Pos", new Pos()); + System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(offsetVar)); + } offsetVar.RawValue = new Pos(offsetX, offsetY, 0, offsetAngle); } diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ColorArea.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ColorArea.xaml index b9053b6..2e3383b 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ColorArea.xaml +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ColorArea.xaml @@ -31,7 +31,10 @@ - + @@ -89,7 +92,7 @@ - + @@ -107,13 +110,15 @@ - + + FontWeight="Bold" + IsReadOnly="True" Background="#FFF0F0F0" /> diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/GrayArea.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/GrayArea.xaml index eb64986..21d7f1d 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/GrayArea.xaml +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/GrayArea.xaml @@ -31,7 +31,10 @@ - + @@ -77,7 +80,7 @@ - + @@ -95,13 +98,15 @@ - + + FontWeight="Bold" + IsReadOnly="True" Background="#FFF0F0F0" /> diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/TwoLineIntersectHandler.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/TwoLineIntersectHandler.cs index e1bd453..787eab8 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/TwoLineIntersectHandler.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/TwoLineIntersectHandler.cs @@ -64,7 +64,8 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm var offVarName = item.StringParams.TryGetValue("IntersectOffsetVar", out var ov) && !string.IsNullOrEmpty(ov) ? ov : "两线交点_Offset"; var offVar = func.Params.Variables.FirstOrDefault(v => v.Name == offVarName); - if (offVar == null) { offVar = new LocalVariableItem(offVarName, "Pos", new Pos()); System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(offVar)); } + if (offVar == null) { offVar = new LocalVariableItem(offVarName, "Pos", new Pos()); + System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(offVar)); } offVar.RawValue = new Pos(offX, offY, 0, offAngle); } diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/WeChatDecodeHandler.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/WeChatDecodeHandler.cs index e4b6fd8..df31291 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/WeChatDecodeHandler.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/WeChatDecodeHandler.cs @@ -1,6 +1,5 @@ -#nullable enable +#nullable enable using OpenCvSharp; -using PLCSharp.Core.Common; using System.Windows.Media; namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm @@ -122,8 +121,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm if (existingVar == null) { existingVar = new LocalVariableItem(varName, "Barcode", new Barcode(new Pos(), "")); - System.Windows.Application.Current.Dispatcher.Invoke( - () => func.Params.Variables.Add(existingVar)); + System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(existingVar)); } existingVar.RawValue = new Barcode(new Pos(boxCX, boxCY, 0, 0), text); } diff --git a/PLCSharpWpf/VVMs/Vision/VisionsViewModel.cs b/PLCSharpWpf/VVMs/Vision/VisionsViewModel.cs index b6e7c37..c342d19 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionsViewModel.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionsViewModel.cs @@ -43,7 +43,7 @@ namespace PLCSharp.VVMs.Vision { SetProperty(ref _ShowMat, value); if (value == null) return; - System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => + _ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => { if (_ImgSrc != null && _ShowMat.Width == _ImgSrc.PixelWidth