308 lines
12 KiB
C#
308 lines
12 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.Windows;
|
||
using System.Windows.Input;
|
||
using Aurora.Api;
|
||
using Aurora.Api.DataType;
|
||
using Aurora.View;
|
||
using MilCommon;
|
||
using VisionCommon;
|
||
using MilPattern;
|
||
using VisionCommonUI;
|
||
using VisionCommonUI.Resources;
|
||
using System.IO;
|
||
using Hermes;
|
||
|
||
namespace MilUI {
|
||
public class OcrSettingViewModel : NotifyPropertyChangedBase {
|
||
private Logger _logger = new Logger(typeof(OcrSettingViewModel).Name);
|
||
|
||
public ICommand CMD_VisibleChanged { get; }
|
||
public ICommand CMD_Load { get; }
|
||
public ICommand CMD_Edit_StrRead { get; }
|
||
public ICommand CMD_Edit_OcrRead { get; }
|
||
public ICommand CMD_Copy { 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_SetPreProc { get; }
|
||
public ICommand CMD_SetFindROI { get; }
|
||
public ICommand CMD_FindPattern { get; }
|
||
|
||
private PatternInfo _patternInfo;
|
||
public PatternInfo PatternInfo { get => _patternInfo; set => SetProperty(ref _patternInfo, value); }
|
||
|
||
private OcrRunParam _runParam;
|
||
public OcrRunParam RunParam { get => _runParam; set => SetProperty(ref _runParam, value); }
|
||
|
||
public bool IsSubPattern { get; set; }
|
||
|
||
private bool _isPatternRegistered;
|
||
public bool IsPatternRegistered { get => _isPatternRegistered; set => SetProperty(ref _isPatternRegistered, value); }
|
||
|
||
private bool _isSubPatternRegistered;
|
||
public bool IsSubPatternRegistered { get => _isSubPatternRegistered; set => SetProperty(ref _isSubPatternRegistered, value); }
|
||
|
||
private bool _isSubPatternNotRegistered;
|
||
public bool IsSubPatternNotRegistered { get => _isSubPatternNotRegistered; set => SetProperty(ref _isSubPatternNotRegistered, value); }
|
||
|
||
private IEnumerable<int> _fixturePatterns;
|
||
public IEnumerable<int> FixturePatterns { get => _fixturePatterns; set => SetProperty(ref _fixturePatterns, value); }
|
||
|
||
private PatternAgentBase _patternAgent;
|
||
private CameraDisplay _milCameraDisplay;
|
||
private FovInfo _fovInfo;
|
||
private Window _childWindow = null;
|
||
|
||
/// <summary>
|
||
/// <20>ܺ<EFBFBD> <20><><EFBFBD>μ<EFBFBD><CEBC><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>Ǿ<EFBFBD><C7BE><EFBFBD> <20><> ȣ<><C8A3><EFBFBD><EFBFBD> <20>ݹ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ
|
||
/// </summary>
|
||
public static Dictionary<PatternInfo, Action> _ExeExiteds = new Dictionary<PatternInfo, Action>();
|
||
|
||
public OcrSettingViewModel(
|
||
PatternAgentBase patternAgent, PatternInfo patternInfo, ICameraDisplay cameraDisplay, FovInfo fovInfo) {
|
||
_patternAgent = patternAgent;
|
||
_patternInfo = patternInfo;
|
||
_milCameraDisplay = (CameraDisplay)cameraDisplay;
|
||
_fovInfo = fovInfo;
|
||
IsSubPattern = PatternConstants.IsSubPattern(_patternInfo.GetPatternIndex());
|
||
|
||
CMD_VisibleChanged = new DelegateCommand(VisibleChanged);
|
||
CMD_Load = new DelegateCommand(Load);
|
||
CMD_Edit_StrRead = new DelegateCommand(Edit_StrRead);
|
||
CMD_Edit_OcrRead = new DelegateCommand(Edit_OcrRead);
|
||
CMD_Copy = new DelegateCommand(Copy);
|
||
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_SetPreProc = new DelegateCommand(SetPreProc);
|
||
CMD_SetFindROI = new DelegateCommand(SetFindROI);
|
||
CMD_FindPattern = new DelegateCommand(FindPattern);
|
||
|
||
Update();
|
||
}
|
||
|
||
private void Update() {
|
||
InitRegistered();
|
||
InitRunParam();
|
||
InitFixturePatterns();
|
||
UpdateControlButtons(IsPatternRegistered);
|
||
}
|
||
|
||
public void InitRegistered() {
|
||
IsPatternRegistered = _patternAgent.IsPatternExist(_patternInfo.GetPatternIndex());
|
||
}
|
||
|
||
public void InitRunParam() {
|
||
RunParam = _patternAgent.GetRunParam(_patternInfo) as OcrRunParam;
|
||
}
|
||
|
||
public void InitFixturePatterns() {
|
||
FixturePatterns = _patternAgent.GetFixturePatternIndexListForFov(_patternInfo);
|
||
}
|
||
|
||
private void UpdateControlButtons(bool isPatternRegistered) {
|
||
IsPatternRegistered = isPatternRegistered;
|
||
IsSubPatternRegistered = IsSubPattern && isPatternRegistered;
|
||
IsSubPatternNotRegistered = IsSubPattern && !isPatternRegistered;
|
||
}
|
||
|
||
private void VisibleChanged(object obj) {
|
||
bool visible = (bool)obj;
|
||
if (visible == false) {
|
||
ClosingWindow();
|
||
}
|
||
}
|
||
|
||
private void Load(object obj) {
|
||
var image = _milCameraDisplay.GetImage();
|
||
if (image == null) {
|
||
MessageBoxUtil.ShowMessageBoxError(StrTokens.MSG_E_ImageNotReady);
|
||
return;
|
||
}
|
||
|
||
var subPatternDirPath = _patternInfo.GetSubPatternDirectoryPath();
|
||
var contextDirPath = Path.Combine(_patternAgent.GetPatternInfoPath(), PatternConstants.DIR_NAME_PATTERN, subPatternDirPath);
|
||
var fileName = Path.Combine(contextDirPath, "OcrTeachingImage.png");
|
||
IpAdapter.Instance.IpEnvironment.SaveImage(fileName, image);
|
||
System.Diagnostics.Process.Start(contextDirPath);
|
||
}
|
||
|
||
private void Edit_StrRead(object obj) {
|
||
var exePath = @"C:\Program Files\Matrox Imaging\Tools\StringReader.exe"; // MIL <20><>ġ<EFBFBD><C4A1>, Default <20><><EFBFBD><EFBFBD>
|
||
StartExternalProcess(exePath, ".msr");
|
||
}
|
||
|
||
private void Edit_OcrRead(object obj) {
|
||
var exePath = @"C:\Program Files\Matrox Imaging\Tools\OcrReader.exe"; // MIL <20><>ġ<EFBFBD><C4A1>, Default <20><><EFBFBD><EFBFBD>
|
||
StartExternalProcess(exePath, ".mfo");
|
||
}
|
||
|
||
private void StartExternalProcess(string exePath, string fileExtension) {
|
||
var subPatternDirPath = _patternInfo.GetSubPatternDirectoryPath();
|
||
var contextDirPath = Path.Combine(_patternAgent.GetPatternInfoPath(), PatternConstants.DIR_NAME_PATTERN, subPatternDirPath);
|
||
|
||
// EX) "D:...\\_out\\x64-Debug\\_config_recipe\\recipe#1\\Pattern\\0\\0\\0\\7\\0\\Pattern.msr"
|
||
var argPath = Path.Combine(contextDirPath, PatternConstants.FILE_NAME_PATTERN + fileExtension);
|
||
|
||
if (!_ExeExiteds.ContainsKey(_patternInfo)) {
|
||
_ExeExiteds.Add(_patternInfo, null);
|
||
_ExeExiteds[_patternInfo] += () => { Application.Current.Dispatcher.Invoke(() => Reload()); };
|
||
}
|
||
ExternalProcessor.StartExternalProcess(exePath, argPath, _ExeExiteds[_patternInfo]);
|
||
}
|
||
|
||
private void Reload() {
|
||
var pattern = _patternAgent.GetPattern(_patternInfo.GetPatternIndex());
|
||
pattern.Load();
|
||
}
|
||
|
||
private void Copy(object obj) {
|
||
var result = MessageBoxUtil.ShowMessageBoxQuestion(StrTokens.MSG_Q_CopyPatternZero);
|
||
if (result == MessageBoxResult.Yes) {
|
||
var mainPatternInfo = _patternAgent.GetMainPatternInfo(_patternInfo.GetPatternIndex());
|
||
if (E1.OK == _patternAgent.CopyPattern(mainPatternInfo.GetPatternIndex(), _patternInfo.GetPatternIndex())) {
|
||
InitRunParam();
|
||
InitFixturePatterns();
|
||
UpdateControlButtons(true);
|
||
MessageBoxUtil.ShowMessageBoxOk(StrTokens.MSG_O_CopyCompleted);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void Create(object obj) {
|
||
if (!_patternAgent.IsPatternExist(_patternInfo.GetPatternIndex())) {
|
||
if (E1.OK == _patternAgent.Create(_patternInfo) &&
|
||
E1.OK == _patternAgent.Save(_patternInfo)) {
|
||
InitRunParam();
|
||
InitFixturePatterns();
|
||
UpdateControlButtons(true);
|
||
MessageBoxUtil.ShowMessageBoxOk(StrTokens.MSG_O_RegisterCompleted);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void Save(object obj) {
|
||
if (_milCameraDisplay.HasFindRoi()) {
|
||
RunParam.SetFindROI(_milCameraDisplay.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) {
|
||
if (_patternAgent.IsPatternExist(_patternInfo.GetPatternIndex())) {
|
||
if (E1.OK == _patternAgent.Remove(_patternInfo)) {
|
||
UpdateControlButtons(false);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void ClosingWindow() {
|
||
_milCameraDisplay.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 SetPreProc(object obj) {
|
||
if (_childWindow == null) {
|
||
_childWindow = new PreProcessingWindow(_patternInfo, _milCameraDisplay);
|
||
(_childWindow as IPatternSettingWindow).ResetPosition();
|
||
_childWindow.Closed += OnWindowClosed;
|
||
_childWindow.Owner = Application.Current.MainWindow;
|
||
_childWindow.Show();
|
||
}
|
||
else {
|
||
ResetWindow();
|
||
}
|
||
}
|
||
|
||
private void ResetWindow() {
|
||
if (_childWindow.WindowState == WindowState.Minimized) {
|
||
_childWindow.WindowState = WindowState.Normal;
|
||
}
|
||
|
||
(_childWindow as IPatternSettingWindow).ResetPosition();
|
||
}
|
||
|
||
private void OnWindowClosed(object sender, EventArgs e) {
|
||
_childWindow = null;
|
||
}
|
||
|
||
private void SetFindROI(object obj) {
|
||
var milImage = _milCameraDisplay.GetImage();
|
||
if (milImage != 0) {
|
||
var imageSize = MilEnvironment.GetImageSize(milImage);
|
||
var findRoi = GetFindROIFromRunParam(imageSize, RunParam);
|
||
_milCameraDisplay.AddFindRoi(findRoi);
|
||
}
|
||
}
|
||
|
||
private ROI GetFindROIFromRunParam(ImageSize imageSize, OcrRunParam runParam) {
|
||
if (runParam.IsValidFindRoi(imageSize)) {
|
||
return runParam.GetFindRoi();
|
||
}
|
||
|
||
return new ROI(
|
||
new ImagePoint(imageSize.Width * 0.5, imageSize.Height * 0.5),
|
||
100, 300,
|
||
Angle.CreateWithDegree(0));
|
||
}
|
||
|
||
private void FindPattern(object obj) {
|
||
if (!_patternInfo.IsActivated) {
|
||
MessageBoxUtil.ShowMessageBoxError(StrTokens.MSG_E_PatternNotActivated);
|
||
return;
|
||
}
|
||
|
||
if (_milCameraDisplay != null) {
|
||
var image = _milCameraDisplay.GetImage();
|
||
if (image == null) {
|
||
MessageBoxUtil.ShowMessageBoxError(StrTokens.MSG_E_ImageNotReady);
|
||
return;
|
||
}
|
||
if (_milCameraDisplay.HasFindRoi()) {
|
||
RunParam.SetFindROI(_milCameraDisplay.GetFindRoi().Clone() as ROI);
|
||
}
|
||
|
||
_milCameraDisplay.ClearFindGraphics();
|
||
_milCameraDisplay.ClearMeasureGraphics();
|
||
|
||
_patternAgent.SetRunParam(_patternInfo, RunParam);
|
||
var patternResult = _patternAgent.Find(_patternInfo, image);
|
||
_patternAgent.Draw(_patternInfo, patternResult, _milCameraDisplay);
|
||
|
||
if (patternResult.Status == eFoundStatus.NotFound) {
|
||
MessageBoxUtil.ShowMessageBoxError(StrTokens.MSG_E_PatternNotFound);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} |