Explorar o código

"""
feat: 点位参数界面支持相机采集与图像显示

新增相机采集与图像显示功能:ViewModel中引入ICameraService,支持相机选择、采集控制及图像回调;界面左侧抽屉集成CogRecordDisplay控件,支持实时显示相机图像,并提供采集控制按钮和状态信息,提升调试与参数设置效率。
"""

孝锋 徐 hai 8 meses
pai
achega
03f2030315

+ 154 - 2
TeamAAS-VM/ViewModels/Product/PlcPointParamsViewModel.cs

@@ -1,4 +1,5 @@
-using MaterialDesignThemes.Wpf;
+using Cognex.VisionPro;
+using MaterialDesignThemes.Wpf;
 using MathNet.Numerics.LinearAlgebra;
 using NPOI.SS.Formula.Functions;
 using Prism.Commands;
@@ -43,6 +44,7 @@ namespace TeamAAS_VP.ViewModels.Product
         ISystemDatabaseService _systemDatabaseService;
         IRemoteCommandService _remoteCommandService;
         ICalibrationService _calibrationService;
+        ICameraService _cameraService;
         CoordinateTransformer local=null;
 
         #region 属性
@@ -272,6 +274,78 @@ namespace TeamAAS_VP.ViewModels.Product
             set { SetProperty(ref _LockScrewProgramNumber, value); }
         }
 
+        private bool _IsGrap = true;
+        public bool IsGrap
+        {
+            get { return _IsGrap; }
+            set { SetProperty(ref _IsGrap, value); }
+        }
+
+        private ObservableCollection<CameraInfo> _CameraList;
+        public ObservableCollection<CameraInfo> CameraList
+        {
+            get { return _CameraList; }
+            set { SetProperty(ref _CameraList, value); }
+        }
+
+        private CameraInfo _SelectedCamera;
+        public CameraInfo SelectedCamera
+        {
+            get { return _SelectedCamera; }
+            set { SetProperty(ref _SelectedCamera, value);
+                if (value!=null)
+                {
+                    if(Camera!=null)
+                    {
+                        if (Camera.IsGrabbing)
+                        {
+                            Camera.StopGrabbing();
+                        }
+                        Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
+                    }
+                    Camera = _cameraService.GetCamera(value.Id);
+                    Camera.ImageCallbackEvent += Camera_ImageCallbackEvent;
+                }
+            }
+        }
+
+        private ICamera _Camera;
+        public ICamera Camera
+        {
+            get { return _Camera; }
+            set { SetProperty(ref _Camera, value); }
+        }
+
+        private ICogImage _Image;
+
+        public ICogImage Image
+        {
+            get { return _Image; }
+            set { SetProperty(ref _Image, value); }
+        }
+
+        private string _Message;
+        public string Message
+        {
+            get { return _Message; }
+            set { SetProperty(ref _Message, value); }
+        }
+
+        private bool _IsLeftDrawerOpen;
+        public bool IsLeftDrawerOpen
+        {
+            get { return _IsLeftDrawerOpen; }
+            set { SetProperty(ref _IsLeftDrawerOpen, value);
+                if (!value)
+                {
+                    if (IsGrap)
+                    {
+                        ExecuteStopGrabbingCommand();
+                    }
+                }
+            }
+        }
+
         #endregion
 
         #region 命令
@@ -362,6 +436,14 @@ namespace TeamAAS_VP.ViewModels.Product
         public DelegateCommand AutoCorrectLockPointCommand =>
             _AutoCorrectLockPointCommand ?? (_AutoCorrectLockPointCommand = new DelegateCommand(ExecuteAutoCorrectLockPoint));
 
+        private DelegateCommand _StartGrabbingCommand;
+        public DelegateCommand StartGrabbingCommand =>
+            _StartGrabbingCommand ?? (_StartGrabbingCommand = new DelegateCommand(ExecuteStartGrabbingCommand, CanExecuteStartGrabbingCommand).ObservesProperty(() => IsGrap));
+
+        private DelegateCommand _StopGrabbingCommand;
+        public DelegateCommand StopGrabbingCommand =>
+            _StopGrabbingCommand ?? (_StopGrabbingCommand = new DelegateCommand(ExecuteStopGrabbingCommand, CanExecuteStopGrabbingCommand).ObservesProperty(() => IsGrap));
+        
         #endregion
 
         #region 事件
@@ -370,7 +452,7 @@ namespace TeamAAS_VP.ViewModels.Product
 
         public PlcPointParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
             IConfigService configService, IRobotService robotService, ISystemDatabaseService systemDatabaseService, IRemoteCommandService remoteCommandService,
-            ICalibrationService calibrationService)
+            ICalibrationService calibrationService, ICameraService cameraService)
         {
             _regionManager = regionManager;
             _eventAggregator = ea;
@@ -384,6 +466,7 @@ namespace TeamAAS_VP.ViewModels.Product
             _robotService = robotService;
             _remoteCommandService = remoteCommandService;
             _calibrationService = calibrationService;
+            _cameraService = cameraService;
         }
 
         #region 方法
@@ -1405,6 +1488,52 @@ namespace TeamAAS_VP.ViewModels.Product
             }
         }
 
+        /// <summary>
+        /// 停止采集
+        /// </summary>
+        void ExecuteStopGrabbingCommand()
+        {
+            
+            if (Camera != null)
+                Camera.StopGrabbing();
+            IsGrap = false;
+        }
+
+        bool CanExecuteStopGrabbingCommand()
+        {
+            return IsGrap;
+        }
+
+        /// <summary>
+        /// 开始采集
+        /// </summary>
+        void ExecuteStartGrabbingCommand()
+        {
+            if (Camera != null)
+                Camera.StartGrabbing();
+            IsGrap = true;
+        }
+        bool CanExecuteStartGrabbingCommand()
+        {
+            return SelectedCamera != null && !IsGrap;
+        }
+
+        private void Camera_ImageCallbackEvent(ICogImage image, TimeSpan totaltime, string errormessage)
+        {
+            Image = image;
+            App.Current.Dispatcher.Invoke(new
+                 Action(() =>
+                 {
+                     if (errormessage != null && !string.IsNullOrEmpty(errormessage))
+                     {
+                         Message = $"{Lang.耗时}:{totaltime.TotalMilliseconds.ToString("F1")} ms Error:{errormessage}";
+                     }
+                     else
+                     {
+                         Message = $"{Lang.耗时}:{totaltime.TotalMilliseconds.ToString("F1")} ms";
+                     }
+                 }));
+        }
         #endregion
 
         #region 继承
@@ -1466,6 +1595,9 @@ namespace TeamAAS_VP.ViewModels.Product
                 LockZDescendSpeed = SelectProduct.ScrewPoints.FirstOrDefault()?.Z_Velocity_Stop ?? 100;
                 LockFeederNumber = SelectProduct.ScrewPoints.FirstOrDefault()?.Feeder ?? 1;
                 LockScrewProgramNumber = SelectProduct.ScrewPoints.FirstOrDefault()?.ScrewProNum ?? 1;
+
+                CameraList = new ObservableCollection<CameraInfo>(_configService.GetAllCameras());
+               
             }
             catch (Exception ex)
             {
@@ -1493,6 +1625,26 @@ namespace TeamAAS_VP.ViewModels.Product
             //{
             //    Robot.EnterDebugMode = false;
             //}
+            try
+            {
+                IsGrap = false;
+                if (Camera != null)
+                {
+                    if (Camera.IsGrabbing)
+                    {
+                        Camera.StopGrabbing();
+                    }
+                    Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
+                    
+                    Camera = null;
+                }
+                SelectedCamera = null;
+                IsLeftDrawerOpen = false;
+            }
+            catch (Exception)
+            {
+
+            }
         }
         #endregion
 

+ 71 - 4
TeamAAS-VM/Views/Product/PlcPointParams.xaml

@@ -2,6 +2,8 @@
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:prism="http://prismlibrary.com/"
+             xmlns:wf="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
+             xmlns:vp="clr-namespace:Cognex.VisionPro;assembly=Cognex.VisionPro.Controls"
              xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
              xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
              xmlns:sys="clr-namespace:System;assembly=mscorlib"
@@ -95,12 +97,66 @@
         </Border>
 
         <materialDesign:DrawerHost Grid.Row="1"
-                                   OpenMode="Modal"
+                                   OpenMode="Standard"
                                    x:Name="DrawerHost"
+                                   IsLeftDrawerOpen="{Binding IsLeftDrawerOpen,Mode=TwoWay}"
+                                   d:IsLeftDrawerOpen="true"
                                    IsEnabled="{Binding IsAllowEdit}">
-            <!--<materialDesign:DrawerHost.RightDrawerContent>
-            <local:TestProductPage Visibility="{Binding IsHaveRobot,Converter={StaticResource BooleanToVisibilityConverter}}" />
-        </materialDesign:DrawerHost.RightDrawerContent>-->
+            <materialDesign:DrawerHost.LeftDrawerContent>
+                <Grid Width="600">
+                    <Grid.RowDefinitions>
+                        <RowDefinition Height="*" />
+                        <RowDefinition Height=" auto" />
+                    </Grid.RowDefinitions>
+                    <wf:WindowsFormsHost Grid.Row="0"
+                                         Margin="0,40">
+                        <vp:CogRecordDisplay x:Name="display" />
+                    </wf:WindowsFormsHost>
+                    <StackPanel Grid.Row="1"
+                                HorizontalAlignment="Left"
+                                VerticalAlignment="Bottom"
+                                Orientation="Horizontal">
+                        <TextBlock Text="{lex:Loc 相机,Converter={StaticResource StringFormatConverter},ConverterParameter='{}{0}: '}"
+                                   FontWeight="Bold"
+                                   VerticalAlignment="Center"
+                                   />
+                        <ComboBox Margin="5,0,10,0"
+                                  IsEnabled="{Binding IsGrap,Converter={StaticResource InvertBooleanConverter}}"
+                                  ItemsSource="{Binding CameraList}"
+                                  SelectedItem="{Binding SelectedCamera}"
+                                  materialDesign:HintAssist.Hint="{lex:Loc 选择相机}">
+                            <ComboBox.ItemTemplate>
+                                <DataTemplate>
+                                    <StackPanel Orientation="Horizontal">
+                                        <materialDesign:PackIcon Kind="Camera" />
+                                        <TextBlock Text="{Binding CameraName}"
+                                                   Margin="6,0" />
+                                    </StackPanel>
+                                </DataTemplate>
+                            </ComboBox.ItemTemplate>
+                        </ComboBox>
+                        <Button Grid.Column="0"
+                                Content="{lex:Loc 开始采集}"
+                                MinWidth="100"
+                                materialDesign:ButtonAssist.CornerRadius="10"
+                                Style="{StaticResource MaterialDesignRaisedButton}"
+                                Command="{Binding StartGrabbingCommand}" />
+                        <Button Grid.Column="0"
+                                Content="{lex:Loc 停止采集}"
+                                IsEnabled="{Binding IsAllowEdit}"
+                                MinWidth="100"
+                                Margin="10,0"
+                                materialDesign:ButtonAssist.CornerRadius="10"
+                                Style="{StaticResource MaterialDesignRaisedButton}"
+                                Command="{Binding StopGrabbingCommand}" />
+
+                        <TextBlock Text="{Binding Message}"
+                                   VerticalAlignment="Center"
+                                   Foreground="Gray" />
+                    </StackPanel>
+                </Grid>
+                
+            </materialDesign:DrawerHost.LeftDrawerContent>
             <Grid>
                 <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="auto" />
@@ -1220,6 +1276,15 @@
                                 <ColumnDefinition Width="auto" />
                                 <ColumnDefinition Width="auto" />
                             </Grid.ColumnDefinitions>
+                            <ToggleButton Grid.ColumnSpan="2"
+                                          HorizontalAlignment="Left"
+                                          VerticalAlignment="Top"
+                                         materialDesign:ToggleButtonAssist.OnContent="{materialDesign:PackIcon Kind=ArrowLeft}"
+                                          Content="{materialDesign:PackIcon Kind=Camera}"
+                                          Style="{StaticResource MaterialDesignActionToggleButton}"
+                                          ToolTip="相机图像"
+                                          Margin="5,0"
+                                          IsChecked="{Binding IsLeftDrawerOpen,ElementName=DrawerHost}" />
                             <Grid Grid.Column="0"
                                   VerticalAlignment="Center">
                                 <Grid.RowDefinitions>
@@ -1229,6 +1294,8 @@
                                     <RowDefinition Height="auto" />
                                     <RowDefinition Height="auto" />
                                 </Grid.RowDefinitions>
+                                
+                                
                                 <Button Content="{lex:Loc 上使能}"
                                         Grid.Row="0"
                                         materialDesign:ButtonAssist.CornerRadius="10"

+ 17 - 0
TeamAAS-VM/Views/Product/PlcPointParams.xaml.cs

@@ -1,4 +1,5 @@
 using System.Windows.Controls;
+using TeamAAS_VP.ViewModels.Product;
 
 namespace TeamAAS_VP.Views.Product
 {
@@ -7,9 +8,25 @@ namespace TeamAAS_VP.Views.Product
     /// </summary>
     public partial class PlcPointParams : UserControl
     {
+
+        PlcPointParamsViewModel VM;
         public PlcPointParams()
         {
             InitializeComponent();
+            VM = DataContext as PlcPointParamsViewModel;
+            VM.PropertyChanged += VM_PropertyChanged;
+            this.display.HorizontalScrollBar = false;
+            this.display.VerticalScrollBar = false;
+            this.display.AutoFit = true;
+            this.display.BackColor = System.Drawing.SystemColors.ActiveCaption;
+        }
+
+        private void VM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
+        {
+            if (e.PropertyName == "Image")
+            {
+                this.display.Image = VM.Image;
+            }
         }
     }
 }