305 lines
10 KiB
C#
305 lines
10 KiB
C#
/*
|
|
* Copyright (c) 2025 LG Electronics Inc.
|
|
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
|
|
*/
|
|
using Aurora.Api;
|
|
using VisionCommon;
|
|
using Hermes;
|
|
using Cognex.VisionPro;
|
|
using System;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.ComponentModel;
|
|
using System.Runtime.CompilerServices;
|
|
using CogCommon;
|
|
using Cognex.VisionPro.Display;
|
|
|
|
namespace CogUI {
|
|
/// <summary>
|
|
/// Interaction logic for ForceOkView.xaml
|
|
/// </summary>
|
|
public partial class ForceOkView : UserControl, INotifyPropertyChanged, IDisposable {
|
|
protected Window _PatternSettingWindow = null;
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
protected bool SetProperty<S>(
|
|
ref S storage,
|
|
S value,
|
|
[CallerMemberName] string propertyName = null) {
|
|
if (Equals(storage, value))
|
|
return false;
|
|
storage = value;
|
|
OnPropertyChanged(propertyName);
|
|
return true;
|
|
}
|
|
|
|
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
|
=> PropertyChanged?.Invoke(
|
|
this,
|
|
new PropertyChangedEventArgs(propertyName));
|
|
|
|
public Window ParentWindow { get; set; }
|
|
public PatternInfo PatternInfo { get; set; }
|
|
|
|
private bool _isForceOkEnabled = true;
|
|
public bool IsForceOkEnabled { get => _isForceOkEnabled; set => SetProperty(ref _isForceOkEnabled, value); }
|
|
|
|
private ImagePoint _forceOkPoint = new ImagePoint();
|
|
public ImagePoint ForceOkPoint { get => _forceOkPoint; set => SetProperty(ref _forceOkPoint, value); }
|
|
|
|
public PatternResult NewFindResult { get; set; }
|
|
|
|
private bool _isApplyButtonEnabled = false;
|
|
public bool IsApplyButtonEnabled { get => _isApplyButtonEnabled; set => SetProperty(ref _isApplyButtonEnabled, value); }
|
|
|
|
public bool IsApply { get; set; } = false;
|
|
|
|
private Logger _logger = new Logger("ForceOkView");
|
|
private FovInfo _fovInfo;
|
|
private PatternAgentBase _patternAgent;
|
|
private CameraAgentBase _CameraAgent;
|
|
private CameraView _cameraView;
|
|
private CameraDisplay _cameraDisplay;
|
|
private CogDisplay _cogDisplay;
|
|
private ICogImage _findImage = null;
|
|
private CogColorConstants COLOR_ORANGE = CogColorConstants.Orange;
|
|
private int _crossLength = 200;
|
|
private const double ZOOM_STEP = 0.1;
|
|
|
|
public ForceOkView(FovInfo fovInfo, PatternInfo patternInfo, object findImage) {
|
|
InitializeComponent();
|
|
|
|
_fovInfo = fovInfo;
|
|
PatternInfo = patternInfo;
|
|
|
|
SetForceOkEnabled();
|
|
SetCameraView();
|
|
SetDisplay();
|
|
SetCameraAgent();
|
|
SetPatternAgent();
|
|
LoadFindlImageToDisplay((ICogImage)findImage);
|
|
|
|
if (_isForceOkEnabled) {
|
|
LoadTrainOrigin();
|
|
IsApplyButtonEnabled = true;
|
|
}
|
|
|
|
DataContext = this;
|
|
}
|
|
|
|
private void SetForceOkEnabled() {
|
|
_isForceOkEnabled = VisionRecipeManager.IsForceOkEnaled(_fovInfo.ToSubSysInfo(), eAlignId.Align1);
|
|
}
|
|
|
|
private void SetCameraView() {
|
|
_cameraView = new CameraView(_fovInfo, eDisplayType.ForceOk);
|
|
gridCameraView.Children.Add(_cameraView);
|
|
_cameraDisplay = _cameraView.GetDisplay() as CameraDisplay;
|
|
}
|
|
|
|
private void SetDisplay() {
|
|
_cogDisplay = _cameraDisplay.GetDisplay();
|
|
}
|
|
|
|
public ICameraDisplay GetDisplay() {
|
|
return _cameraDisplay;
|
|
}
|
|
|
|
private void SetCameraAgent() {
|
|
_CameraAgent = CameraAgentManager.GetCameraAgent(_fovInfo);
|
|
}
|
|
|
|
private void SetPatternAgent() {
|
|
_patternAgent = PatternAgentManager.GetAgent(_fovInfo);
|
|
}
|
|
|
|
public void Dispose() {
|
|
_PatternSettingWindow?.Close();
|
|
CogEnvironment.FreeImage(_findImage);
|
|
_cameraView.Dispose();
|
|
}
|
|
|
|
private void LoadFindlImageToDisplay(ICogImage findImage) {
|
|
_findImage = findImage;
|
|
_cameraDisplay.SetImage(findImage);
|
|
}
|
|
|
|
private void LoadTrainOrigin() {
|
|
if (_isForceOkEnabled && _patternAgent.IsTrainPattern(PatternInfo)) {
|
|
var trainParam = _patternAgent.GetTrainParam(PatternInfo) as ImageTrainParam;
|
|
_crossLength = (int)(trainParam.TrainRoi.Width * 0.5);
|
|
ForceOkPoint = trainParam.TrainOrigin.Origin.Clone() as ImagePoint;
|
|
AddCross(trainParam);
|
|
}
|
|
else {
|
|
ForceOkPoint = new ImagePoint(0, 0);
|
|
}
|
|
}
|
|
|
|
private void ZoomMinus_Button_Click(object sender, RoutedEventArgs e) {
|
|
RelativeZoomImage(ZOOM_STEP * (-1));
|
|
}
|
|
|
|
private void FitImage_Button_Click(object sender, RoutedEventArgs e) {
|
|
_cogDisplay.Fit(true);
|
|
}
|
|
|
|
private void ZoomPlus_Button_Click(object sender, RoutedEventArgs e) {
|
|
RelativeZoomImage(ZOOM_STEP);
|
|
}
|
|
|
|
public int RelativeZoomImage(double zoom) {
|
|
try {
|
|
double curZoom = _cogDisplay.Zoom;
|
|
return AbsZoomImage(curZoom + zoom);
|
|
}
|
|
catch (Exception e) {
|
|
_logger.FuncE(e.Message);
|
|
}
|
|
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
public int AbsZoomImage(double zoom) {
|
|
try {
|
|
if (zoom > 0) {
|
|
_cogDisplay.Zoom = zoom;
|
|
}
|
|
return E1.OK;
|
|
}
|
|
catch (Exception e) {
|
|
_logger.FuncE(e.Message);
|
|
}
|
|
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
public void Clear() {
|
|
_cameraDisplay.ClearMeasureGraphics();
|
|
_PatternSettingWindow = null;
|
|
}
|
|
|
|
private void Edit_Button_Click(object sender, RoutedEventArgs e) {
|
|
_cameraDisplay.ClearAllGraphics();
|
|
|
|
var param = new Tuple<PatternAgentBase, PatternInfo, ICameraDisplay, FovInfo, Window, ForceOkView>(
|
|
_patternAgent, PatternInfo, _cameraDisplay, _fovInfo, _PatternSettingWindow, this);
|
|
VisionEnv.AuroraEnv.EventPublisher.Publish(
|
|
new EventMessage(string.Empty, VisionEventId.SHOW_PATTERN_SETTING_WINDOW_FORCE_OK, param));
|
|
}
|
|
|
|
private void Find_Button_Click(object sender, RoutedEventArgs e) {
|
|
var patternResult = _patternAgent.Find(PatternInfo, _cameraDisplay.GetImage(), _cameraDisplay);
|
|
NewFindResult = patternResult;
|
|
|
|
if (patternResult.Status == eFoundStatus.FoundOK) {
|
|
if (patternResult.Results != null) {
|
|
foreach (var result in patternResult.Results) {
|
|
ForceOkPoint = result.ImagePosition;
|
|
break;
|
|
}
|
|
}
|
|
IsApplyButtonEnabled = true;
|
|
}
|
|
else {
|
|
if (_isForceOkEnabled) {
|
|
Move(new ImagePoint(0, 0));
|
|
}
|
|
else {
|
|
IsApplyButtonEnabled = false;
|
|
}
|
|
}
|
|
_cameraDisplay.ClearMeasureGraphics();
|
|
_cameraDisplay.ClearFindGraphics();
|
|
_patternAgent.Draw(PatternInfo, patternResult, _cameraDisplay);
|
|
}
|
|
|
|
private void Apply_Button_Click(object sender, RoutedEventArgs e) {
|
|
IsApply = true;
|
|
ParentWindow?.Close();
|
|
}
|
|
|
|
private void Cancel_Button_Click(object sender, RoutedEventArgs e) {
|
|
IsApply = false;
|
|
ParentWindow?.Close();
|
|
}
|
|
|
|
private void Move_Left_Up_Button_Click(object sender, RoutedEventArgs e) {
|
|
Move(new ImagePoint(
|
|
GetMoveStep() * (-1),
|
|
GetMoveStep() * (-1)));
|
|
}
|
|
|
|
private void Move_Right_Up_Button_Click(object sender, RoutedEventArgs e) {
|
|
Move(new ImagePoint(
|
|
GetMoveStep(),
|
|
GetMoveStep() * (-1)));
|
|
}
|
|
|
|
private void Move_Up_Button_Click(object sender, RoutedEventArgs e) {
|
|
Move(new ImagePoint(
|
|
0,
|
|
GetMoveStep() * (-1)));
|
|
}
|
|
|
|
private void Move_Left_Button_Click(object sender, RoutedEventArgs e) {
|
|
Move(new ImagePoint(
|
|
GetMoveStep() * (-1),
|
|
0));
|
|
}
|
|
|
|
private void Move_Right_Button_Click(object sender, RoutedEventArgs e) {
|
|
Move(new ImagePoint(
|
|
GetMoveStep(),
|
|
0));
|
|
}
|
|
|
|
private void Move_Left_Down_Button_Click(object sender, RoutedEventArgs e) {
|
|
Move(new ImagePoint(
|
|
GetMoveStep() * (-1),
|
|
GetMoveStep()));
|
|
}
|
|
|
|
private void Move_Down_Button_Click(object sender, RoutedEventArgs e) {
|
|
Move(new ImagePoint(0, GetMoveStep()));
|
|
}
|
|
|
|
private void Move_Right_Down_Button_Click(object sender, RoutedEventArgs e) {
|
|
Move(new ImagePoint(
|
|
GetMoveStep(),
|
|
GetMoveStep()));
|
|
}
|
|
|
|
private double GetMoveStep() {
|
|
return Convert.ToDouble(trainOriginMoveStep.Value);
|
|
}
|
|
|
|
private void Move(ImagePoint moveOffset) {
|
|
ForceOkPoint = (ForceOkPoint + moveOffset) as ImagePoint;
|
|
DisplayDrawer.RemoveInteractiveGraphic(_cogDisplay, CogConstants.KEY_CROSS);
|
|
AddCross();
|
|
}
|
|
|
|
private void Center_Button_Click(object sender, RoutedEventArgs e) {
|
|
ForceOkPoint = CogEnvironment.GetImageCenter(_findImage);
|
|
DisplayDrawer.RemoveInteractiveGraphic(_cogDisplay, CogConstants.KEY_CROSS);
|
|
AddCross();
|
|
}
|
|
|
|
private void AddCross(ImageTrainParam trainParam = null) {
|
|
if (trainParam != null) {
|
|
DisplayDrawer.AddCross(
|
|
_cogDisplay, COLOR_ORANGE,
|
|
new Cross(ForceOkPoint, trainParam.TrainRoi.Angle, _crossLength),
|
|
CogConstants.KEY_CROSS);
|
|
}
|
|
else {
|
|
DisplayDrawer.AddCross(
|
|
_cogDisplay, COLOR_ORANGE,
|
|
new Cross(ForceOkPoint, _crossLength),
|
|
CogConstants.KEY_CROSS);
|
|
}
|
|
}
|
|
}
|
|
}
|