219 lines
7.9 KiB
C#
219 lines
7.9 KiB
C#
/*
|
|
* Copyright (c) 2025 LG Electronics Inc.
|
|
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
|
|
*/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using Aurora.Api;
|
|
using Aurora.Api.DataType;
|
|
using Aurora.View;
|
|
using VisionLight;
|
|
using MilCommon;
|
|
using VisionCommon;
|
|
using Hermes;
|
|
using MilPattern;
|
|
using VisionCommonUI;
|
|
using VisionCommonUI.Resources;
|
|
|
|
namespace MilUI {
|
|
public class EdgeSettingViewModel : NotifyPropertyChangedBase {
|
|
private Logger _logger = new Logger("EdgeSettingVM");
|
|
|
|
public ICommand CMD_VisibleChanged { get; }
|
|
public ICommand CMD_Create { get; }
|
|
public ICommand CMD_Save { get; }
|
|
public ICommand CMD_Cancel { get; }
|
|
public ICommand CMD_Delete { get; }
|
|
public ICommand CMD_SetLight { get; }
|
|
public ICommand CMD_SetFindROI { get; }
|
|
public ICommand CMD_FindPattern { get; }
|
|
|
|
public bool IsSubPattern { get; set; }
|
|
|
|
private bool _isPatternRegistered;
|
|
public bool IsPatternRegistered { get => _isPatternRegistered; set => SetProperty(ref _isPatternRegistered, value); }
|
|
|
|
private PatternInfo _patternInfo;
|
|
public PatternInfo PatternInfo { get => _patternInfo; set => SetProperty(ref _patternInfo, value); }
|
|
|
|
private EdgeFinderRunParam _runParam;
|
|
public EdgeFinderRunParam RunParam { get => _runParam; set => SetProperty(ref _runParam, value); }
|
|
|
|
private IEnumerable<int> _fixturePatterns;
|
|
public IEnumerable<int> FixturePatterns { get => _fixturePatterns; set => SetProperty(ref _fixturePatterns, value); }
|
|
|
|
public IEnumerable<eMilEdgeThresholdMode> ThresholdModeItems { get; } = Enum.GetValues(typeof(eMilEdgeThresholdMode)).Cast<eMilEdgeThresholdMode>();
|
|
|
|
private PatternAgentBase _patternAgent;
|
|
private CameraDisplay _cameraDisplay;
|
|
private FovInfo _fovInfo;
|
|
private Window _window = null;
|
|
|
|
public EdgeSettingViewModel(
|
|
PatternAgentBase patternAgent, PatternInfo patternInfo, ICameraDisplay cameraDisplay, FovInfo fovInfo) {
|
|
_patternAgent = patternAgent;
|
|
_patternInfo = patternInfo;
|
|
_cameraDisplay = cameraDisplay as CameraDisplay;
|
|
_fovInfo = fovInfo;
|
|
IsSubPattern = PatternConstants.IsSubPattern(_patternInfo.GetPatternIndex());
|
|
|
|
CMD_VisibleChanged = new DelegateCommand(VisibleChanged);
|
|
CMD_Create = new DelegateCommand(Create);
|
|
CMD_Save = new DelegateCommand(Save);
|
|
CMD_Cancel = new DelegateCommand(Cancel);
|
|
CMD_Delete = new DelegateCommand(Delete);
|
|
|
|
CMD_SetLight = new DelegateCommand(SetLight);
|
|
CMD_SetFindROI = new DelegateCommand(SetFindROI);
|
|
CMD_FindPattern = new DelegateCommand(FindPattern);
|
|
|
|
InitRegistered();
|
|
InitRunParam();
|
|
InitFixturePatterns();
|
|
}
|
|
|
|
public void InitRegistered() {
|
|
IsPatternRegistered = _patternAgent.IsPatternExist(_patternInfo.GetPatternIndex());
|
|
}
|
|
|
|
public void InitRunParam() {
|
|
if (_patternAgent.IsPatternExist(_patternInfo.GetPatternIndex())) {
|
|
RunParam = _patternAgent.GetRunParam(_patternInfo) as EdgeFinderRunParam;
|
|
}
|
|
else {
|
|
RunParam = new EdgeFinderRunParam();
|
|
}
|
|
}
|
|
|
|
public void InitFixturePatterns() {
|
|
FixturePatterns = _patternAgent.GetFixturePatternIndexListForFov(_patternInfo);
|
|
}
|
|
|
|
private void VisibleChanged(object obj) {
|
|
bool visible = (bool)obj;
|
|
if (visible == false) {
|
|
ClosingWindow();
|
|
}
|
|
}
|
|
|
|
private void Create(object obj) {
|
|
if (!_patternAgent.IsPatternExist(_patternInfo.GetPatternIndex())) {
|
|
if (E1.OK == _patternAgent.Create(_patternInfo) &&
|
|
E1.OK == _patternAgent.Save(_patternInfo)) {
|
|
IsPatternRegistered = true;
|
|
MessageBoxUtil.ShowMessageBoxOk(StrTokens.MSG_O_RegisterCompleted);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Save(object obj) {
|
|
if (_cameraDisplay.HasFindRoi()) {
|
|
RunParam.SetFindROI(_cameraDisplay.GetFindRoi().Clone() as ROI);
|
|
}
|
|
_patternAgent.SetRunParam(_patternInfo, RunParam);
|
|
_patternAgent.Save(_patternInfo);
|
|
MessageBoxUtil.ShowMessageBoxOk(StrTokens.MSG_O_SaveCompleted);
|
|
}
|
|
|
|
private void Cancel(object obj) {
|
|
ClosingWindow();
|
|
CloseWindow(obj);
|
|
}
|
|
|
|
private void Delete(object obj) {
|
|
var result = MessageBoxUtil.ShowMessageBoxQuestion(StrTokens.MSG_Q_Delete);
|
|
if (result == MessageBoxResult.Yes) {
|
|
Application.Current.Dispatcher.Invoke(() => {
|
|
if (_patternAgent.IsPatternExist(_patternInfo.GetPatternIndex())) {
|
|
if (E1.OK == _patternAgent.Remove(_patternInfo)) {
|
|
IsPatternRegistered = false;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
private void ClosingWindow() {
|
|
_cameraDisplay.ClearFindGraphics();
|
|
}
|
|
|
|
private void CloseWindow(object obj) {
|
|
var window = obj as Window;
|
|
if (window != null) {
|
|
window.Close();
|
|
}
|
|
}
|
|
|
|
private void SetLight(object obj) {
|
|
PatternLightWindowManager.ShowLightSettingWindow(_fovInfo, _patternInfo);
|
|
}
|
|
|
|
private void ResetWindow() {
|
|
if (_window.WindowState == WindowState.Minimized) {
|
|
_window.WindowState = WindowState.Normal;
|
|
}
|
|
|
|
(_window as IPatternSettingWindow).ResetPosition();
|
|
}
|
|
|
|
private void OnWindowClosed(object sender, EventArgs e) {
|
|
_window = null;
|
|
}
|
|
|
|
private void SetFindROI(object obj) {
|
|
var milImage = _cameraDisplay.GetImage();
|
|
if (milImage != 0) {
|
|
var imageSize = MilEnvironment.GetImageSize(milImage);
|
|
var findRoi = GetFindROIFromRunParam(imageSize, RunParam);
|
|
_cameraDisplay.AddFindRoi(findRoi);
|
|
}
|
|
}
|
|
|
|
private ROI GetFindROIFromRunParam(ImageSize imageSize, EdgeFinderRunParam runParam) {
|
|
if (runParam.IsValidFindRoi(imageSize)) {
|
|
return runParam.GetFindRoi();
|
|
}
|
|
|
|
return new ROI(
|
|
new ImagePoint(imageSize.Width * 0.5, imageSize.Height * 0.5),
|
|
500, 500,
|
|
Angle.CreateWithDegree(0));
|
|
}
|
|
|
|
private void FindPattern(object obj) {
|
|
if (!_patternInfo.IsActivated) {
|
|
MessageBoxUtil.ShowMessageBoxError(StrTokens.MSG_E_PatternNotActivated);
|
|
return;
|
|
}
|
|
|
|
if (_cameraDisplay != null) {
|
|
var image = _cameraDisplay.GetImage();
|
|
if (image == null) {
|
|
MessageBoxUtil.ShowMessageBoxError(StrTokens.MSG_E_ImageNotReady);
|
|
return;
|
|
}
|
|
if (_cameraDisplay.HasFindRoi()) {
|
|
RunParam.SetFindROI(_cameraDisplay.GetFindRoi().Clone() as ROI);
|
|
}
|
|
|
|
_cameraDisplay.ClearTrainGraphics();
|
|
_cameraDisplay.ClearFindGraphics();
|
|
_cameraDisplay.ClearMeasureGraphics();
|
|
|
|
_patternAgent.SetRunParam(_patternInfo, RunParam);
|
|
var patternResult = _patternAgent.Find(_patternInfo, image);
|
|
_patternAgent.Draw(_patternInfo, patternResult, _cameraDisplay);
|
|
|
|
if (patternResult.Status == eFoundStatus.NotFound) {
|
|
MessageBoxUtil.ShowMessageBoxError(StrTokens.MSG_E_PatternNotFound);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|