306 lines
12 KiB
C#
306 lines
12 KiB
C#
using Aurora.Api;
|
|
using Aurora.View;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using VisionCommon;
|
|
using VisionCommonUI.Resources;
|
|
using VisionLight;
|
|
|
|
namespace VisionCommonUI {
|
|
public abstract class PatternListViewModelBase : NotifyPropertyChangedBase {
|
|
public ICommand CMD_VisibleChanged { get; }
|
|
public ICommand CMD_GrabFind { get; }
|
|
public ICommand CMD_Find { get; }
|
|
public ICommand CMD_Register { get; }
|
|
public ICommand CMD_Edit { get; }
|
|
public ICommand CMD_Delete { get; }
|
|
public ICommand CMD_Apply { get; }
|
|
public ICommand CMD_Tracking { get; }
|
|
|
|
public ObservableCollection<PatternInfo> Patterns { get; set; }
|
|
|
|
private int _selectedPatternIndex;
|
|
public int SelectedPatternIndex {
|
|
get => _selectedPatternIndex;
|
|
set {
|
|
SetProperty(ref _selectedPatternIndex, value);
|
|
if (SelectedPatternIndex != -1) {
|
|
IsSelectedPatternRegistered = Patterns[SelectedPatternIndex].IsRegistered();
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool _isViewEnabled = true;
|
|
public bool IsViewEnabled { get => _isViewEnabled; set => SetProperty(ref _isViewEnabled, value); }
|
|
|
|
private bool _isSelectedPatternRegistered;
|
|
public bool IsSelectedPatternRegistered { get => _isSelectedPatternRegistered; set => SetProperty(ref _isSelectedPatternRegistered, value); }
|
|
|
|
private bool _UseCamPattern = false;
|
|
public bool UseCamPattern { get => _UseCamPattern; set => SetProperty(ref _UseCamPattern, value); }
|
|
|
|
public bool _isRegistered;
|
|
public bool IsRegistered { get => _isRegistered; set => SetProperty(ref _isRegistered, value); }
|
|
|
|
private readonly Logger _logger = new Logger("PatternListVM");
|
|
|
|
private SubSysInfo _SubSysInfo;
|
|
private FovInfo _FovInfo;
|
|
private FovRecipe _FovRecipe;
|
|
protected Window _window = null;
|
|
protected PatternAgentBase _patternAgent = null;
|
|
protected readonly int _PatternCnt;
|
|
|
|
public PatternListViewModelBase(FovInfo fovInfo, int patternCnt) {
|
|
_SubSysInfo = fovInfo.ToSubSysInfo();
|
|
_FovInfo = fovInfo;
|
|
_FovRecipe = VisionRecipeManager.GetFovRecipe(fovInfo);
|
|
_PatternCnt = patternCnt;
|
|
UseCamPattern = VisionSettingManager.VisionSetting.UseCamPattern;
|
|
|
|
CMD_VisibleChanged = new DelegateCommand(VisibleChanged);
|
|
CMD_GrabFind = new DelegateCommand(GrabFind);
|
|
CMD_Find = new DelegateCommand(Find);
|
|
CMD_Register = new DelegateCommand(Register);
|
|
CMD_Edit = new DelegateCommand(Edit);
|
|
CMD_Delete = new DelegateCommand(Delete);
|
|
CMD_Apply = new DelegateCommand(Apply);
|
|
CMD_Tracking = new DelegateCommand(Tracking);
|
|
|
|
var unitInfo = VisionRecipeManager.GetUnitInfo(_FovInfo.UnitId);
|
|
}
|
|
|
|
private void ResetPatternAgent() {
|
|
if (VisionSettingManager.VisionSetting.UseCamPattern) {
|
|
_patternAgent = CamPatternAgentManager.GetAgent(_FovRecipe.CamRecipe.CamId);
|
|
}
|
|
else {
|
|
_patternAgent = FovPatternAgentManager.GetAgent(_FovInfo.ToSubSysInfo());
|
|
}
|
|
}
|
|
|
|
public abstract PatternInfo CreateDefaultPatternInfo(int PatternId);
|
|
|
|
public LightInfo GetDefaultLightInfo() {
|
|
var lightParams = LightHub.Instance.GetLightParams(_FovRecipe.CamRecipe.CamId);
|
|
return new LightInfo() { LightParams = lightParams };
|
|
}
|
|
|
|
public void InitPatternList(int initIndex) {
|
|
try {
|
|
Application.Current.Dispatcher.Invoke(
|
|
new Action(() => {
|
|
if (Patterns == null) {
|
|
Patterns = new ObservableCollection<PatternInfo>();
|
|
}
|
|
Patterns.Clear();
|
|
|
|
var existingPatterns = GetExistingPatterns();
|
|
var existingPatternKeys = existingPatterns.Select(ep => ep.GetPatternId()).ToList();
|
|
|
|
for (int i = 0; i < _PatternCnt; i++) {
|
|
if (existingPatternKeys.Contains(i)) {
|
|
Patterns.Add(existingPatterns.FirstOrDefault(ep => i == ep.GetPatternId()));
|
|
|
|
var curPatternInfo = Patterns[Patterns.Count - 1];
|
|
curPatternInfo.Thumbnail = _patternAgent.GetThumbnail(curPatternInfo);
|
|
}
|
|
else {
|
|
Patterns.Add(CreateDefaultPatternInfo(i));
|
|
}
|
|
}
|
|
|
|
SelectedPatternIndex = initIndex;
|
|
}));
|
|
}
|
|
catch (Exception e) {
|
|
_logger.FuncE(e.Message);
|
|
}
|
|
}
|
|
|
|
public List<PatternInfo> GetExistingPatterns() {
|
|
if (_patternAgent == null) {
|
|
ResetPatternAgent();
|
|
}
|
|
return _patternAgent.GetMainPatterns(CreateDefaultPatternInfo(0));
|
|
}
|
|
|
|
private void VisibleChanged(object obj) {
|
|
bool isVisible = (bool)obj;
|
|
|
|
if (isVisible) {
|
|
InitPatternList(0);
|
|
NotifyPropertyChanged("Patterns");
|
|
}
|
|
else {
|
|
_window?.Close();
|
|
}
|
|
}
|
|
|
|
public void GrabFind(object obj) {
|
|
var cameraAgent = CameraAgentManager.GetCameraAgent(_FovInfo);
|
|
if (cameraAgent.IsLiveRunning()) {
|
|
cameraAgent.StopLive();
|
|
}
|
|
|
|
_logger.FuncI($"SelectedPatternIndex : {SelectedPatternIndex}");
|
|
|
|
if (0 <= SelectedPatternIndex) {
|
|
var patternInfo = Patterns[SelectedPatternIndex];
|
|
if (IsFindSupported(patternInfo.PatternType)) {
|
|
StartGrabFindSeq(patternInfo);
|
|
}
|
|
else {
|
|
MessageBoxUtil.ShowMessageBoxError(
|
|
string.Format(StrTokens.MSG_E_PatternFindingNotSupported, patternInfo.PatternType));
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool IsFindSupported(ePatternType patternType) {
|
|
return patternType != ePatternType.EdgeFinder;
|
|
}
|
|
|
|
public abstract void StartGrabFindSeq(PatternInfo patternInfo);
|
|
|
|
public void Find(object obj) {
|
|
if (0 <= SelectedPatternIndex) {
|
|
var patternInfo = Patterns[SelectedPatternIndex];
|
|
if (IsFindSupported(patternInfo.PatternType)) {
|
|
StartFindSeq(patternInfo);
|
|
}
|
|
else {
|
|
MessageBoxUtil.ShowMessageBoxError(
|
|
string.Format(StrTokens.MSG_E_PatternFindingNotSupported, patternInfo.PatternType));
|
|
}
|
|
}
|
|
}
|
|
|
|
public abstract void StartFindSeq(PatternInfo patternInfo);
|
|
|
|
private void Register(object obj) {
|
|
_logger.FuncI($"SelectedPatternIndex : {SelectedPatternIndex}");
|
|
|
|
if (_window == null) {
|
|
IsViewEnabled = false;
|
|
RegisterPatternWindow();
|
|
(_window as IPatternSettingWindow).ResetPosition();
|
|
_window.Closed += OnWindowClosed;
|
|
_window.Owner = Application.Current.MainWindow;
|
|
_window.Show();
|
|
}
|
|
else {
|
|
ResetWindow();
|
|
}
|
|
}
|
|
|
|
public abstract void RegisterPatternWindow();
|
|
|
|
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;
|
|
IsViewEnabled = true;
|
|
InitPatternList(SelectedPatternIndex);
|
|
}
|
|
|
|
private void Edit(object obj) {
|
|
_logger.FuncI($"SelectedPatternIndex : {SelectedPatternIndex}");
|
|
|
|
if (_window == null) {
|
|
IsViewEnabled = false;
|
|
_patternAgent.ReloadPattern(Patterns[SelectedPatternIndex]);
|
|
CreatePatternSettingWindow();
|
|
if (_window == null) {
|
|
MessageBoxUtil.ShowMessageBoxError(StrTokens.MSG_E_PatternNotFound);
|
|
}
|
|
else {
|
|
(_window as IPatternSettingWindow).ResetPosition();
|
|
_window.Closed += OnWindowClosed;
|
|
_window.Show();
|
|
}
|
|
}
|
|
else {
|
|
ResetWindow();
|
|
}
|
|
}
|
|
|
|
public abstract void CreatePatternSettingWindow();
|
|
|
|
private void Delete(object obj) {
|
|
_logger.FuncI($"SelectedPatternIndex : {SelectedPatternIndex}");
|
|
|
|
var lastSelectedPatternIndex = SelectedPatternIndex;
|
|
var result = MessageBoxUtil.ShowMessageBoxQuestion(
|
|
string.Format(StrTokens.MSG_Q_DeletePatternFormat, SelectedPatternIndex));
|
|
if (result == MessageBoxResult.Yes) {
|
|
Application.Current.Dispatcher.Invoke(() => {
|
|
DeleteSubPatterns(Patterns[SelectedPatternIndex]);
|
|
DeleteMainPattern(Patterns[SelectedPatternIndex]);
|
|
Patterns[SelectedPatternIndex] = CreateDefaultPatternInfo(SelectedPatternIndex);
|
|
InitPatternList(lastSelectedPatternIndex);
|
|
});
|
|
}
|
|
}
|
|
|
|
private void DeleteSubPatterns(PatternInfo patternInfo) {
|
|
var subPatternList = _patternAgent.GetSubPatterns(patternInfo);
|
|
foreach (var subPatternInfo in subPatternList) {
|
|
_patternAgent.Remove(subPatternInfo);
|
|
}
|
|
}
|
|
private void DeleteMainPattern(PatternInfo patternInfo) {
|
|
_patternAgent.Remove(patternInfo);
|
|
}
|
|
|
|
private void Apply(object obj) {
|
|
_logger.FuncI($"SelectedPatternIndex : {SelectedPatternIndex}");
|
|
|
|
var patternRecipe = VisionRecipeManager.GetPatternRecipe(_SubSysInfo, _FovInfo.FovId);
|
|
var options = new string[(int)eProcPatternId.Max];
|
|
|
|
for (int i = 0; i < patternRecipe.ProcPatternInfos.Count; i++) {
|
|
options[i] = patternRecipe.ProcPatternInfos[i].ProcPatternId.ToString();
|
|
}
|
|
|
|
var msgResult = MessageBoxUtil.ShowMessageBoxQuestionOption(
|
|
string.Format(StrTokens.MSG_Q_ApplyPatternAsProcessPatternFormat, SelectedPatternIndex),
|
|
StrTokens.MSG_I_SelectProcessPatternId,
|
|
options);
|
|
|
|
if (msgResult.Result == MessageBoxResult.Yes) {
|
|
Application.Current.Dispatcher.Invoke(() => {
|
|
var patternInfo = Patterns[SelectedPatternIndex];
|
|
var procPatternId = (eProcPatternId)msgResult.SelIndex;
|
|
var procPatternInfo = patternRecipe.ProcPatternInfos.Where(p => p.ProcPatternId.Equals(procPatternId)).FirstOrDefault();
|
|
|
|
if (procPatternInfo == null) {
|
|
_logger.FuncE("procPatternInfo is null.");
|
|
}
|
|
|
|
procPatternInfo.PatternId = patternInfo.GetPatternId();
|
|
procPatternInfo.ProcPatternId = procPatternId;
|
|
RecipeFileManager.SaveVisionRecipe();
|
|
});
|
|
}
|
|
}
|
|
|
|
private void Tracking(object obj) {
|
|
var window = new PatternTrackingSettingWindow(_FovInfo);
|
|
window?.Show();
|
|
}
|
|
}
|
|
}
|