修复CNC运行 与算子编辑

This commit is contained in:
zhengxuan.zhang
2026-04-28 14:13:12 +08:00
parent 89759cf511
commit 514eace979
2 changed files with 72 additions and 2 deletions
@@ -8,11 +8,17 @@ using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using XplorePlane.Events;
using XplorePlane.Models;
using XplorePlane.Services;
using XplorePlane.Services.MainViewport;
using XP.Common.Logging.Interfaces;
using Prism.Events;
namespace XplorePlane.ViewModels.Cnc
{
@@ -21,6 +27,9 @@ namespace XplorePlane.ViewModels.Cnc
private readonly CncEditorViewModel _editorViewModel;
private readonly IImageProcessingService _imageProcessingService;
private readonly IPipelinePersistenceService _persistenceService;
private readonly IPipelineExecutionService _executionService;
private readonly IMainViewportService _mainViewportService;
private readonly IEventAggregator _eventAggregator;
private readonly ILoggerService _logger;
private CncNodeViewModel _activeModuleNode;
@@ -29,17 +38,26 @@ namespace XplorePlane.ViewModels.Cnc
private string _pipelineFileDisplayName = "未命名模块.xpm";
private string _currentFilePath;
private bool _isSynchronizing;
private CancellationTokenSource _debounceCts;
private const int DebounceDelayMs = 300;
public CncInspectionModulePipelineViewModel(
CncEditorViewModel editorViewModel,
IImageProcessingService imageProcessingService,
IPipelinePersistenceService persistenceService,
ILoggerService logger)
ILoggerService logger,
IPipelineExecutionService executionService = null,
IMainViewportService mainViewportService = null,
IEventAggregator eventAggregator = null)
{
_editorViewModel = editorViewModel ?? throw new ArgumentNullException(nameof(editorViewModel));
_imageProcessingService = imageProcessingService ?? throw new ArgumentNullException(nameof(imageProcessingService));
_persistenceService = persistenceService ?? throw new ArgumentNullException(nameof(persistenceService));
_logger = (logger ?? throw new ArgumentNullException(nameof(logger))).ForModule<CncInspectionModulePipelineViewModel>();
_executionService = executionService;
_mainViewportService = mainViewportService;
_eventAggregator = eventAggregator;
PipelineNodes = new ObservableCollection<PipelineNodeViewModel>();
@@ -373,6 +391,52 @@ namespace XplorePlane.ViewModels.Cnc
_activeModuleNode.Pipeline = BuildPipelineModel();
StatusMessage = statusMessage;
TriggerDebouncedPreview();
}
private void TriggerDebouncedPreview()
{
if (_executionService == null || _mainViewportService == null)
return;
var sourceImage = _mainViewportService.CurrentDisplayImage as BitmapSource
?? _mainViewportService.LatestManualImage as BitmapSource;
if (sourceImage == null)
{
_logger.Debug("[图像链路][CNC] TriggerDebouncedPreview:无可用源图像,跳过");
return;
}
_debounceCts?.Cancel();
_debounceCts = new CancellationTokenSource();
var token = _debounceCts.Token;
Task.Delay(DebounceDelayMs, token).ContinueWith(t =>
{
if (!t.IsCanceled)
_ = ExecutePreviewAsync(sourceImage, token);
}, TaskScheduler.FromCurrentSynchronizationContext());
}
private async Task ExecutePreviewAsync(BitmapSource sourceImage, CancellationToken token)
{
try
{
_logger.Info("[图像链路][CNC] ExecutePreviewAsync:开始执行,节点数={Count}", PipelineNodes.Count);
var result = await _executionService.ExecutePipelineAsync(PipelineNodes, sourceImage, null, token);
_logger.Info("[图像链路][CNC] ExecutePreviewAsync:执行完成,推送结果图像");
_mainViewportService.SetManualImage(result, string.Empty);
_eventAggregator?.GetEvent<PipelinePreviewUpdatedEvent>()
.Publish(new PipelinePreviewUpdatedPayload(result, StatusMessage));
}
catch (OperationCanceledException)
{
_logger.Debug("[图像链路][CNC] ExecutePreviewAsync:已取消");
}
catch (Exception ex)
{
_logger.Error(ex, "[图像链路][CNC] ExecutePreviewAsync:执行失败");
}
}
private PipelineModel BuildPipelineModel()
+7 -1
View File
@@ -61,12 +61,18 @@ namespace XplorePlane.Views.Cnc
var imageProcessingService = ContainerLocator.Current.Resolve<IImageProcessingService>();
var persistenceService = ContainerLocator.Current.Resolve<IPipelinePersistenceService>();
var logger = ContainerLocator.Current.Resolve<ILoggerService>();
var executionService = ContainerLocator.Current.Resolve<IPipelineExecutionService>();
var mainViewportService = ContainerLocator.Current.Resolve<XplorePlane.Services.MainViewport.IMainViewportService>();
var eventAggregator = ContainerLocator.Current.Resolve<Prism.Events.IEventAggregator>();
_inspectionModulePipelineViewModel = new CncInspectionModulePipelineViewModel(
editorViewModel,
imageProcessingService,
persistenceService,
logger);
logger,
executionService,
mainViewportService,
eventAggregator);
InspectionModulePipelineEditor.DataContext = _inspectionModulePipelineViewModel;
}