| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172 |
- using Cognex.VisionPro;
- using Cognex.VisionPro.ImageFile;
- using Cognex.VisionPro3D;
- using log4net.Core;
- using MahApps.Metro.Controls;
- using Prism.Events;
- using Prism.Ioc;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Forms.Integration;
- using System.Windows.Interop;
- using TeamAAS_VP;
- using TeamAAS_VP.Core;
- using TeamAAS_VP.Enums;
- using TeamAAS_VP.Events;
- using TeamAAS_VP.Interfaces;
- using TeamAAS_VP.Models;
- using TeamAAS_VP.Resources.Languages;
- using TeamAAS_VP.ViewModels;
- using TeamAAS_VP.ViewModels.Home;
- //using static MaterialDesignThemes.Wtf.ToolBar;
- namespace TeamAAS_VP.Views.Home
- {
- /// <summary>
- /// Interaction logic for ShowVisionRender
- /// </summary>
- public partial class ShowVisionRender : UserControl
- {
- private const string Render2DKeySuffix = ":2D";
- private const string Render3DKeySuffix = ":3D";
- private static string Get2DKey(Guid id) => id.ToString() + Render2DKeySuffix;
- private static string Get3DKey(Guid id) => id.ToString() + Render3DKeySuffix;
- ShowVisionRenderViewModel viewModel;
- Dictionary<string, WindowsFormsHost> vmRenders;
- Dictionary<Guid, TextBlock> Titles;
- IEventAggregator _eventAggregator;
- private readonly IProductService _productService;
- private readonly Prism.Services.Dialogs.IDialogService _dialogService;
- private CancellationTokenSource _saveImageCts = new CancellationTokenSource();
- private readonly SemaphoreSlim _saveSemaphore = new SemaphoreSlim(5, 5);
- private List<Models.DelaySaveImage> delaySaveImages = new List<Models.DelaySaveImage>();
- public ShowVisionRender()
- {
- InitializeComponent();
- vmRenders = new Dictionary<string, WindowsFormsHost>();
- Titles = new Dictionary<Guid, TextBlock>();
- viewModel = DataContext as ShowVisionRenderViewModel;
- _eventAggregator = viewModel._eventAggregator;
- _productService = ((Prism.PrismApplicationBase)App.Current).Container.Resolve<IProductService>();
- // 新增:DialogService
- _dialogService = ((Prism.PrismApplicationBase)App.Current).Container.Resolve<Prism.Services.Dialogs.IDialogService>();
- viewModel.UpdateLayout += ViewModel_UpdateLayout;
- // ThreadOption.BackgroundThread:让订阅者在后台线程执行,Publish() 立即返回,
- // 图像处理流程不再等待 UI 更新和保存完成才回复完成信号
- _eventAggregator.GetEvent<RenderUpdateNotification>().Subscribe(
- UpdateRenderModuleSource,
- ThreadOption.BackgroundThread);
- _eventAggregator.GetEvent<ProductChangedNotification>().Subscribe(ProductChanged);
- _eventAggregator.GetEvent<DelaySaveImageNotification>().Subscribe(UpdateDelaySaveImage);
- }
- private bool GetView3DUse()
- {
- try
- {
- var configService = ((Prism.PrismApplicationBase)App.Current).Container.Resolve<IConfigService>();
- if(configService.GetSystemConfiguration().PickPointNum==0)
- {
- return false;
- }
- else
- {
- return true;
- }
- //return true;
- }
- catch
- {
- return false;
- }
- }
- private void UpdateRenderModuleSource(ShowRender render)
- {
- if (render == null)
- return;
- // 【关键】在后台线程深拷贝 ICogImage,防止下一次视觉流程运行覆盖像素数据
- ICogImage imageCopy = null;
- try
- {
- imageCopy = render.Image?.CopyBase(CogImageCopyModeConstants.CopyPixels);
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("深拷贝 ICogImage 失败", ex);
- }
- // ICogRecord 和 Graphic 通常每次运行新创建,引用不会被覆盖,无需深拷贝
- var graphic = render.Graphic;
- var record = render.Record;
- var rangeImage = render.RangeImage;
- var greyImage = render.GreyImage;
- // 派发 UI 更新到 WPF UI 线程(fire-and-forget,不等待完成)
- this.gridRender.Dispatcher.BeginInvoke(new Action(() =>
- {
- if (render.Is3DModel == true && GetView3DUse() == true)
- {
- if (!vmRenders.TryGetValue(Get3DKey(render.Id), out var host3D))
- return;
- var display3D = host3D.Child as Cognex.VisionPro3D.Cog3DDisplayV2WF;
- if (display3D == null)
- return;
- try
- {
- display3D.Clear();
- if (rangeImage == null || greyImage == null)
- return;
- var rImgG = new Cog3DRangeImageGraphic(rangeImage, greyImage);
- display3D.Add(rImgG, "__default");
- display3D.FitView();
- }
- catch (System.Windows.Forms.AxHost.InvalidActiveXStateException)
- {
- }
- }
- if (!vmRenders.TryGetValue(Get2DKey(render.Id), out var host2D))
- return;
- var recordDisplay = host2D.Child as Cognex.VisionPro.CogRecordDisplay;
- if (recordDisplay == null)
- return;
- try
- {
- recordDisplay.Record = null;
- recordDisplay.Image = imageCopy; // ✅ 深拷贝,安全
- recordDisplay.StaticGraphics.Clear();
- if (graphic != null)
- recordDisplay.StaticGraphics.AddList(graphic, "");
- if (record != null)
- recordDisplay.Record = record;
- if (render.IsSaveImage)
- {
- // 传递深拷贝后的图像引用给保存逻辑
- var renderForSave = new ShowRender
- {
- Id = render.Id,
- Image = imageCopy,
- Record = record,
- Graphic = graphic,
- Result = render.Result,
- IsSaveImage = render.IsSaveImage,
- SaveImageModel = render.SaveImageModel,
- SaveImagePathModel = render.SaveImagePathModel,
- SavePath = render.SavePath,
- IsCompress = render.IsCompress,
- IsDelaySaveImage = render.IsDelaySaveImage,
- ImageFileName = render.ImageFileName,
- ProceductName = render.ProceductName,
- Is3DModel = render.Is3DModel,
- };
- RequestSaveImage(renderForSave, recordDisplay);
- }
- }
- catch (System.Windows.Forms.AxHost.InvalidActiveXStateException)
- {
- }
- }));
- }
- private void AddRenderPair(Guid id, string displayName)
- {
- // 2D
- var display2D = new Cognex.VisionPro.CogRecordDisplay() { Tag = displayName };
- display2D.HandleCreated += (s, e) =>
- {
- try
- {
- display2D.HorizontalScrollBar = false;
- display2D.VerticalScrollBar = false;
- display2D.AutoFit = true;
- display2D.BackColor = System.Drawing.SystemColors.ActiveCaption;
- }
- catch (System.Windows.Forms.AxHost.InvalidActiveXStateException)
- {
- }
- };
- // 绑定双击:弹出预览窗口
- display2D.DoubleClick += (s, e) => OnDisplayDoubleClick(display2D);
- vmRenders.Add(Get2DKey(id), new WindowsFormsHost() { Tag = displayName, Child = display2D });
- // 3D:按配置创建
- if (!GetView3DUse())
- return;
- // 3D
- var display3D = new Cognex.VisionPro3D.Cog3DDisplayV2WF() { Tag = displayName };
- display3D.HandleCreated += (s, e) =>
- {
- try
- {
- display3D.BackColor = System.Drawing.SystemColors.ActiveCaption;
- }
- catch (System.Windows.Forms.AxHost.InvalidActiveXStateException)
- {
- }
- };
- vmRenders.Add(Get3DKey(id), new WindowsFormsHost() { Tag = displayName, Child = display3D });
- }
- private Grid CreateCellGrid(Guid id, string displayName, System.Windows.Media.FontFamily font)
- {
- var is3DEnabled = GetView3DUse();
- var cell = new Grid();
- cell.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
- cell.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- var title = new TextBlock()
- {
- Text = displayName,
- HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
- FontWeight = FontWeights.Bold,
- FontFamily = font,
- FontSize = 12,
- Foreground = System.Windows.Media.Brushes.Black
- };
- Titles[id] = title;
- var content = new Grid();
- if(is3DEnabled)
- {
- content.ColumnDefinitions.Add(new ColumnDefinition()); // 2D
- content.ColumnDefinitions.Add(new ColumnDefinition()); // 3D
- content.Children.Add(vmRenders[Get2DKey(id)]);
- content.Children.Add(vmRenders[Get3DKey(id)]);
- Grid.SetColumn(vmRenders[Get2DKey(id)], 0);
- Grid.SetColumn(vmRenders[Get3DKey(id)], 1);
- }
- else
- {
- content.ColumnDefinitions.Add(new ColumnDefinition()); // 仅 2D
- content.Children.Add(vmRenders[Get2DKey(id)]);
- Grid.SetColumn(vmRenders[Get2DKey(id)], 0);
- }
- cell.Children.Add(title);
- cell.Children.Add(content);
- Grid.SetRow(title, 0);
- Grid.SetRow(content, 1);
- return cell;
- }
- Grid grid1 = new Grid();
- Grid grid2 = new Grid();
- Grid grid3 = new Grid();
- Grid grid4 = new Grid();
- Grid grid5 = new Grid();
- Grid grid6 = new Grid();
- Grid grid7 = new Grid();
- Grid grid8 = new Grid();
- Grid grid9 = new Grid();
- Grid grid10 = new Grid();
- Grid grid11 = new Grid();
- Grid grid12 = new Grid();
- Grid grid13 = new Grid();
- Grid grid14 = new Grid();
- Grid grid15 = new Grid();
- Grid grid16 = new Grid();
- Grid grid17 = new Grid();
- Grid grid18 = new Grid();
- Grid grid19 = new Grid();
- Grid grid20 = new Grid();
- private void ViewModel_UpdateLayout(System.Collections.ObjectModel.ObservableCollection<Models.ShowRender> obj)
- {
- System.Windows.Media.FontFamily font = Application.Current.Resources["DefaultFont"] as System.Windows.Media.FontFamily;
-
- if (vmRenders.Count > 0)
- {
- foreach (var render in vmRenders)
- {
- render.Value.Dispose();
- }
- }
- vmRenders.Clear();
- gridRender.Children.Clear();
- gridRender.RowDefinitions.Clear();
- gridRender.ColumnDefinitions.Clear();
- Titles.Clear();
- if (obj == null || obj.Count == 0)
- return;
- // 先创建所有相机的 2D/3D 对
- for (int i = 0; i < obj.Count; i++)
- AddRenderPair(obj[i].Id, obj[i].DisplayName);
- switch (obj.Count)
- {
- case 1:
- {
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- var cell = CreateCellGrid(obj[0].Id, obj[0].DisplayName, font);
- gridRender.Children.Add(cell);
- Grid.SetRow(cell, 0);
- Grid.SetColumn(cell, 0);
- gridRender.ShowGridLines = false;
- break;
- }
- case 2:
- {
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ShowGridLines = true;
- var cell0 = CreateCellGrid(obj[0].Id, obj[0].DisplayName, font);
- var cell1 = CreateCellGrid(obj[1].Id, obj[1].DisplayName, font);
- gridRender.Children.Add(cell0);
- gridRender.Children.Add(cell1);
- Grid.SetRow(cell0, 0);
- Grid.SetColumn(cell0, 0);
- Grid.SetRow(cell1, 0);
- Grid.SetColumn(cell1, 1);
- break;
- }
- case 3:
- {
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ShowGridLines = true;
- var cell0 = CreateCellGrid(obj[0].Id, obj[0].DisplayName, font);
- var cell1 = CreateCellGrid(obj[1].Id, obj[1].DisplayName, font);
- var cell2 = CreateCellGrid(obj[2].Id, obj[2].DisplayName, font);
- gridRender.Children.Add(cell0);
- gridRender.Children.Add(cell1);
- gridRender.Children.Add(cell2);
- Grid.SetRow(cell0, 0);
- Grid.SetColumn(cell0, 0);
- Grid.SetRow(cell1, 0);
- Grid.SetColumn(cell1, 1);
- Grid.SetRow(cell2, 1);
- Grid.SetColumn(cell2, 0);
- Grid.SetColumnSpan(cell2, 2);
- break;
- }
- case 4:
- {
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ShowGridLines = true;
- for (int i = 0; i < 4; i++)
- {
- var cell = CreateCellGrid(obj[i].Id, obj[i].DisplayName, font);
- gridRender.Children.Add(cell);
- Grid.SetRow(cell, i / 2);
- Grid.SetColumn(cell, i % 2);
- }
- break;
- }
- case 5:
- {
- // 你的原布局:上面2个,下面3个(通过嵌套grid6)
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ShowGridLines = true;
- var top0 = CreateCellGrid(obj[0].Id, obj[0].DisplayName, font);
- var top1 = CreateCellGrid(obj[1].Id, obj[1].DisplayName, font);
- var bottom = new Grid();
- bottom.ColumnDefinitions.Add(new ColumnDefinition());
- bottom.ColumnDefinitions.Add(new ColumnDefinition());
- bottom.ColumnDefinitions.Add(new ColumnDefinition());
- var b0 = CreateCellGrid(obj[2].Id, obj[2].DisplayName, font);
- var b1 = CreateCellGrid(obj[3].Id, obj[3].DisplayName, font);
- var b2 = CreateCellGrid(obj[4].Id, obj[4].DisplayName, font);
- bottom.Children.Add(b0);
- bottom.Children.Add(b1);
- bottom.Children.Add(b2);
- Grid.SetColumn(b0, 0);
- Grid.SetColumn(b1, 1);
- Grid.SetColumn(b2, 2);
- gridRender.Children.Add(top0);
- gridRender.Children.Add(top1);
- gridRender.Children.Add(bottom);
- Grid.SetRow(top0, 0);
- Grid.SetColumn(top0, 0);
- Grid.SetRow(top1, 0);
- Grid.SetColumn(top1, 1);
- Grid.SetRow(bottom, 1);
- Grid.SetColumn(bottom, 0);
- Grid.SetColumnSpan(bottom, 2);
- break;
- }
- case 6:
- {
- // 你的原布局:2行3列
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ShowGridLines = true;
- for (int i = 0; i < 6; i++)
- {
- var cell = CreateCellGrid(obj[i].Id, obj[i].DisplayName, font);
- gridRender.Children.Add(cell);
- Grid.SetRow(cell, i / 3);
- Grid.SetColumn(cell, i % 3);
- }
- break;
- }
- case 7:
- {
- // 你的原布局:第一行4个,第二行3个(但原来只放到 col 0/1/2)
- // 这里保持原样:第一行4列,第二行3列(第4列留空)
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ShowGridLines = true;
- for (int i = 0; i < 7; i++)
- {
- var cell = CreateCellGrid(obj[i].Id, obj[i].DisplayName, font);
- gridRender.Children.Add(cell);
- if (i < 4)
- {
- Grid.SetRow(cell, 0);
- Grid.SetColumn(cell, i);
- }
- else
- {
- Grid.SetRow(cell, 1);
- Grid.SetColumn(cell, i - 4);
- }
- }
- break;
- }
- case 8:
- {
- // 2行4列
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- for (int i = 0; i < 4; i++)
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ShowGridLines = true;
- for (int i = 0; i < 8; i++)
- {
- var cell = CreateCellGrid(obj[i].Id, obj[i].DisplayName, font);
- gridRender.Children.Add(cell);
- Grid.SetRow(cell, i / 4);
- Grid.SetColumn(cell, i % 4);
- }
- break;
- }
- case 9:
- {
- // 3行3列
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ShowGridLines = true;
- for (int i = 0; i < 9; i++)
- {
- var cell = CreateCellGrid(obj[i].Id, obj[i].DisplayName, font);
- gridRender.Children.Add(cell);
- Grid.SetRow(cell, i / 3);
- Grid.SetColumn(cell, i % 3);
- }
- break;
- }
- case 12:
- {
- // 3行4列
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- for (int i = 0; i < 4; i++)
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ShowGridLines = true;
- for (int i = 0; i < 12; i++)
- {
- var cell = CreateCellGrid(obj[i].Id, obj[i].DisplayName, font);
- gridRender.Children.Add(cell);
- Grid.SetRow(cell, i / 4);
- Grid.SetColumn(cell, i % 4);
- }
- break;
- }
- case 16:
- {
- // 4行4列
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- for (int i = 0; i < 4; i++)
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ShowGridLines = true;
- for (int i = 0; i < 16; i++)
- {
- var cell = CreateCellGrid(obj[i].Id, obj[i].DisplayName, font);
- gridRender.Children.Add(cell);
- Grid.SetRow(cell, i / 4);
- Grid.SetColumn(cell, i % 4);
- }
- break;
- }
- case 20:
- {
- // 5行4列
- for (int r = 0; r < 5; r++)
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- for (int c = 0; c < 4; c++)
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ShowGridLines = true;
- for (int i = 0; i < 20; i++)
- {
- var cell = CreateCellGrid(obj[i].Id, obj[i].DisplayName, font);
- gridRender.Children.Add(cell);
- Grid.SetRow(cell, i / 4);
- Grid.SetColumn(cell, i % 4);
- }
- break;
- }
- default:
- {
- // 未覆盖的数量:先用简单的 1列多行兜底(避免空白)
- gridRender.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- gridRender.ColumnDefinitions.Add(new ColumnDefinition());
- gridRender.ShowGridLines = false;
- var cell = CreateCellGrid(obj[0].Id, obj[0].DisplayName, font);
- gridRender.Children.Add(cell);
- Grid.SetRow(cell, 0);
- Grid.SetColumn(cell, 0);
- break;
- }
- }
- }
- /// <summary>
- /// 请求保存图像(在 WinForms 消息循环线程截图,然后派发到后台线程保存)
- /// 使用 SemaphoreSlim 控制并发,支持 CancellationToken 取消
- /// </summary>
- private void RequestSaveImage(ShowRender render, Cognex.VisionPro.CogRecordDisplay disp)
- {
- if (render == null) return;
- if (disp == null) return;
- if (!render.IsSaveImage) return;
- bool needRecordedBitmap =
- render.SaveImageModel == ProcedureSaveImageModel.Recorded ||
- render.SaveImageModel == ProcedureSaveImageModel.OriginalAndRecorded;
- var token = _saveImageCts.Token;
- disp.BeginInvoke(new Action(() =>
- {
- Bitmap clonedBitmap = null;
- try
- {
- if (token.IsCancellationRequested) return;
- if (needRecordedBitmap)
- {
- if (disp.IsDisposed || !disp.IsHandleCreated) return;
- Bitmap uiBitmap = (Bitmap)disp.CreateContentBitmap(
- Cognex.VisionPro.Display.CogDisplayContentBitmapConstants.Custom);
- clonedBitmap = (Bitmap)uiBitmap.Clone();
- uiBitmap.Dispose();
- }
- var capturedBitmap = clonedBitmap;
- var capturedRender = render;
- Task.Run(async () =>
- {
- await _saveSemaphore.WaitAsync(token);
- try
- {
- if (token.IsCancellationRequested) return;
- if (capturedRender.IsDelaySaveImage)
- {
- // 延迟保存:存入列表,等 DelaySaveImageNotification 时统一写入
- ICogImage imageDeepCopy = null;
- try
- {
- imageDeepCopy = capturedRender.Image?
- .CopyBase(CogImageCopyModeConstants.CopyPixels);
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("延迟保存深拷贝 ICogImage 失败", ex);
- }
- var delayItem = new Models.DelaySaveImage()
- {
- SaveImageModel = capturedRender.SaveImageModel,
- SaveImagePathModel = capturedRender.SaveImagePathModel,
- SavePath = capturedRender.SavePath,
- Image = imageDeepCopy,
- clonedBitmap = capturedBitmap != null
- ? (Bitmap)capturedBitmap.Clone() : null,
- Result = capturedRender.Result,
- IsCompress = capturedRender.IsCompress,
- ImageFileName = capturedRender.ImageFileName,
- Is3DModel = capturedRender.Is3DModel,
- };
- lock (delaySaveImages)
- {
- delaySaveImages.Add(delayItem);
- }
- }
- else
- {
- if (string.IsNullOrEmpty(_productService.CurrentProductCode))
- {
- _eventAggregator.GetEvent<TaskMessageNotification>()
- .Publish(new MessageStruct()
- {
- Message = "当前产品SN不存在,无法保存图片!",
- level = MessageLevel.Alarm
- });
- return;
- }
- LogHelper.WriteLogInfo("开始异步保存图像");
- await SaveImageAsync(
- capturedRender.SaveImageModel,
- capturedRender.SaveImagePathModel,
- capturedRender.SavePath,
- capturedRender.Image,
- capturedBitmap,
- capturedRender.Result,
- capturedRender.IsCompress,
- capturedRender.ImageFileName,
- capturedRender.Is3DModel,
- token
- );
- }
- }
- catch (OperationCanceledException) { }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("后台保存图像任务出错!", ex);
- }
- finally
- {
- try { capturedBitmap?.Dispose(); } catch { }
- _saveSemaphore.Release();
- }
- }, token);
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("RequestSaveImage(UI截图阶段) 出错!", ex);
- try { clonedBitmap?.Dispose(); } catch { }
- }
- }));
- }
- /// <summary>
- /// 异步保存图像(调用者已在后台线程,不再嵌套 Task.Run;通过 CancellationToken 支持取消)
- /// </summary>
- private async Task SaveImageAsync(
- ProcedureSaveImageModel saveImageModel,
- ProcedureSaveImagePathModel saveImagePathModel,
- string path, ICogImage image, Bitmap reimage,
- bool result, bool isCompress, string imageFileName,
- bool is3Dmodel, CancellationToken cancellationToken)
- {
- if (string.IsNullOrEmpty(path) || string.IsNullOrWhiteSpace(path) || !Path.IsPathRooted(path))
- {
- _eventAggregator.GetEvent<TaskMessageNotification>()
- .Publish(new Models.MessageStruct() { Message = "保存图像路径为无效的路径!", level = MessageLevel.Alarm });
- return;
- }
- try
- {
- await Task.CompletedTask; // 消除 CS1998:调用者已在后台线程,下方所有 I/O 为同步执行
- cancellationToken.ThrowIfCancellationRequested();
- DateTime now = DateTime.Now;
- //图片名
- string imgName = now.ToString("yyyy-MM-dd HH_mm_ss");
- if (!string.IsNullOrEmpty(imageFileName))
- {
- imgName = imageFileName;
- }
- if (saveImageModel == ProcedureSaveImageModel.Original)
- {
- string Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\{imgName}.bmp";
- if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_OkNG_Time)
- {
- Originalpath = result
- ? $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\OK\\{imgName}.bmp"
- : $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\NG\\{imgName}.bmp";
- }
- else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Ok_Time)
- {
- if (result)
- Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\OK\\{imgName}.bmp";
- else
- return;
- }
- else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Ng_Time)
- {
- if (!result)
- Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\NG\\{imgName}.bmp";
- else
- return;
- }
- else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Formula_ProductSN_Time)
- {
- // 当前产品配方名(过滤非法字符)
- string formulaName = _productService.GetCurrentProduct().Name;
- foreach (char c in Path.GetInvalidFileNameChars())
- formulaName = formulaName.Replace(c, '_');
- // 产品SN(过滤非法字符)
- string productSN = _productService.CurrentProductCode;
- foreach (char c in Path.GetInvalidFileNameChars())
- productSN = productSN.Replace(c, '_');
- string resultStr = result ? "OK" : "NG";
- Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\{formulaName}\\{productSN}\\{resultStr}\\{imgName}.bmp";
- }
- cancellationToken.ThrowIfCancellationRequested();
- if (!Directory.Exists(Path.GetDirectoryName(Originalpath)))
- Directory.CreateDirectory(Path.GetDirectoryName(Originalpath));
- using (CogImageFileBMP _cogImageFileTool = new CogImageFileBMP())
- {
- _cogImageFileTool.Open(Originalpath, CogImageFileModeConstants.Write);
- _cogImageFileTool.Append(image);
- _cogImageFileTool.Close();
- }
- }
- else if (saveImageModel == ProcedureSaveImageModel.Recorded)
- {
- string Recordedpath = $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\{imgName}.bmp";
- if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_OkNG_Time)
- {
- Recordedpath = result
- ? $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\OK\\{imgName}.bmp"
- : $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\NG\\{imgName}.bmp";
- }
- else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Ok_Time)
- {
- if (result)
- Recordedpath = $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\OK\\{imgName}.bmp";
- else
- return;
- }
- else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Ng_Time)
- {
- if (!result)
- Recordedpath = $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\NG\\{imgName}.bmp";
- else
- return;
- }
- else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Formula_ProductSN_Time)
- {
- string formulaName = _productService.GetCurrentProduct().Name;
- foreach (char c in Path.GetInvalidFileNameChars())
- formulaName = formulaName.Replace(c, '_');
- string productSN = _productService.CurrentProductCode;
- foreach (char c in Path.GetInvalidFileNameChars())
- productSN = productSN.Replace(c, '_');
- string resultStr = result ? "OK" : "NG";
- Recordedpath = $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\{formulaName}\\{productSN}\\{resultStr}\\{imgName}.bmp";
- }
- cancellationToken.ThrowIfCancellationRequested();
- if (!Directory.Exists(Path.GetDirectoryName(Recordedpath)))
- Directory.CreateDirectory(Path.GetDirectoryName(Recordedpath));
- if (isCompress)
- {
- ImageHelper.CompressImage(reimage, Recordedpath, 20);
- }
- else
- {
- reimage.Save(Recordedpath);
- }
- }
- else if (saveImageModel == ProcedureSaveImageModel.OriginalAndRecorded)
- {
- string Originalpath = "";
- if (is3Dmodel == true)
- {
- Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\{imgName}.idb";
- }
- else
- {
- Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\{imgName}.bmp";
- }
- string Recordedpath = $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\{imgName}.bmp";
- if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_OkNG_Time)
- {
- if (result)
- {
- Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\OK\\{imgName}.bmp";
- Recordedpath = $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\OK\\{imgName}.bmp";
- }
- else
- {
- Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\NG\\{imgName}.bmp";
- Recordedpath = $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\NG\\{imgName}.bmp";
- }
- }
- else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Ok_Time)
- {
- if (result)
- {
- Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\OK\\{imgName}.bmp";
- Recordedpath = $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\OK\\{imgName}.bmp";
- }
- else
- {
- return;
- }
- }
- else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Ng_Time)
- {
- if (!result)
- {
- Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\NG\\{imgName}.bmp";
- Recordedpath = $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\NG\\{imgName}.bmp";
- }
- else
- {
- return;
- }
- }
- else if (saveImagePathModel == ProcedureSaveImagePathModel.Month_Day_Formula_ProductSN_Time)
- {
- string formulaName = _productService.GetCurrentProduct().Name;
- foreach (char c in Path.GetInvalidFileNameChars())
- formulaName = formulaName.Replace(c, '_');
- string productSN = _productService.CurrentProductCode;
- if (productSN != null)
- {
- foreach (char c in Path.GetInvalidFileNameChars())
- productSN = productSN.Replace(c, '_');
- }
- string resultStr = result ? "OK" : "NG";
- if (is3Dmodel == true)
- {
- Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\{formulaName}\\{productSN}\\{resultStr}\\{imgName}.idb";
- }
- else
- {
- Originalpath = $"{path}\\Original\\{now:yyyy-MM}\\{now:MM-dd}\\{formulaName}\\{productSN}\\{resultStr}\\{imgName}.bmp";
- }
- Recordedpath = $"{path}\\Recored\\{now:yyyy-MM}\\{now:MM-dd}\\{formulaName}\\{productSN}\\{resultStr}\\{imgName}.bmp";
- }
- cancellationToken.ThrowIfCancellationRequested();
- if (!Directory.Exists(Path.GetDirectoryName(Originalpath)))
- Directory.CreateDirectory(Path.GetDirectoryName(Originalpath));
- using (CogImageFile _cogImageFileTool = new CogImageFile())
- {
- _cogImageFileTool.Open(Originalpath, CogImageFileModeConstants.Write);
- _cogImageFileTool.Append(image);
- _cogImageFileTool.Close();
- }
- cancellationToken.ThrowIfCancellationRequested();
- if (!Directory.Exists(Path.GetDirectoryName(Recordedpath)))
- Directory.CreateDirectory(Path.GetDirectoryName(Recordedpath));
- if (isCompress)
- {
- ImageHelper.CompressImage(reimage, Recordedpath, 20);
- }
- else
- {
- reimage.Save(Recordedpath);
- }
- }
- }
- catch (OperationCanceledException) { }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("保存流程运行结果的图片时出错!", ex);
- }
- finally
- {
- try { reimage?.Dispose(); } catch { }
- }
- }
- /// <summary>
- /// 延迟保存图像处理:isSave=true 时批量保存所有待保存项;false 时丢弃并清理
- /// </summary>
- private async void UpdateDelaySaveImage(bool isSave)
- {
- if (!isSave)
- {
- lock (delaySaveImages)
- {
- foreach (var item in delaySaveImages)
- {
- try { item.clonedBitmap?.Dispose(); } catch { }
- try { item.Image = null; } catch { }
- }
- delaySaveImages.Clear();
- }
- return;
- }
- List<Models.DelaySaveImage> itemsToSave;
- lock (delaySaveImages)
- {
- itemsToSave = new List<Models.DelaySaveImage>(delaySaveImages);
- delaySaveImages.Clear();
- }
- var token = _saveImageCts.Token;
- foreach (var item in itemsToSave)
- {
- try
- {
- if (token.IsCancellationRequested) break;
- if (string.IsNullOrEmpty(_productService.CurrentProductCode))
- {
- _eventAggregator.GetEvent<TaskMessageNotification>()
- .Publish(new MessageStruct()
- {
- Message = "当前产品SN不存在,无法保存图片!",
- level = MessageLevel.Alarm
- });
- break;
- }
- LogHelper.WriteLogInfo("开始异步保存延迟图像");
- await SaveImageAsync(
- item.SaveImageModel,
- item.SaveImagePathModel,
- item.SavePath,
- item.Image,
- item.clonedBitmap,
- item.Result,
- item.IsCompress,
- item.ImageFileName,
- item.Is3DModel,
- token
- );
- }
- catch (OperationCanceledException) { break; }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("延迟保存图像任务出错!", ex);
- }
- finally
- {
- try { item.clonedBitmap?.Dispose(); } catch { }
- try { item.Image = null; } catch { }
- }
- }
- }
- private void ShowVisionRender_Unloaded(object sender, RoutedEventArgs e)
- {
- try { _saveImageCts.Cancel(); _saveImageCts.Dispose(); } catch { }
- // 释放延迟保存列表中的 Bitmap 资源
- lock (delaySaveImages)
- {
- foreach (var item in delaySaveImages)
- {
- try { item.clonedBitmap?.Dispose(); } catch { }
- try { item.Image = null; } catch { }
- }
- delaySaveImages.Clear();
- }
- }
- private void ProductChanged(ProductModel product)
- {
- this.gridRender.Dispatcher.BeginInvoke(new Action(() =>
- {
- var is3DEnabled = GetView3DUse();
- foreach (var item in vmRenders)
- {
- try
- {
- // 2D:永远清理
- var display2D = item.Value.Child as Cognex.VisionPro.CogRecordDisplay;
- if (display2D != null)
- {
- display2D.Record = null;
- display2D.Image = null;
- display2D.StaticGraphics.Clear();
- continue;
- }
- // 3D:仅在启用 3D 时清理
- if (!is3DEnabled)
- continue;
- var display3D = item.Value.Child as Cognex.VisionPro3D.Cog3DDisplayV2WF;
- if (display3D != null)
- {
- display3D.Clear();
- }
- }
- catch (System.Windows.Forms.AxHost.InvalidActiveXStateException)
- {
- // 控件句柄/ActiveX 未就绪时忽略
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError(Lang.切换产品清除主页图像时出错, ex);
- }
- }
- }));
- }
- private void OnDisplayDoubleClick(CogRecordDisplay disp)
- {
- if (disp == null)
- return;
- try
- {
- // 直接把当前 CogRecordDisplay 传给对话框 VM(VM 内部读取 Image/Record)
- var parameters = new Prism.Services.Dialogs.DialogParameters
- {
- { "Cog", disp }
- };
- // Prism Dialog 必须在 WPF UI 线程调用
- Dispatcher.BeginInvoke(new Action(() =>
- {
- try
- {
- _dialogService.ShowDialog("ImagePreviewWindow", parameters, r => { });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("打开 ImagePreviewWindow 失败", ex);
- }
- }));
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("双击打开预览窗口失败", ex);
- }
- }
- }
- }
|