using Aurora.Api; using System.Windows; using System.Windows.Controls; using VisionCommon; using System.Collections.Generic; using System; using System.Linq; using System.Reactive.Linq; using VisionCommonUI.Resources; using System.Text.RegularExpressions; using System.Windows.Input; namespace VisionUI.Setup { /// /// 캘리브레이션 파라미터 뷰 /// CalParamView.xaml behind code /// public partial class CalParamView : UserControl, IUpdateRecipe, IDisposable { private const int PATTERN_NUM_1 = 1; private const int PATTERN_MAX_COUNT = 3; private const string PATTERN_UNDEFINED = "Undefined"; private Logger _Logger; private FovInfo _FovInfo; private FovRecipe _FovRecipe; private IDisposable _EventDisposable; private PatternAgentBase _FovPatternAgent = null; private PatternAgentBase _CamPatternAgent = null; private List _FovPatternInfos = null; private List _CamPatternInfos = new List(); private Dictionary _CbCalPatterns; private List _patternIndexes = new List(); /// /// 생성자 /// /// Fov 정보 public CalParamView(FovInfo fovInfo) { _FovInfo = fovInfo; _FovRecipe = VisionRecipeManager.GetFovRecipe(fovInfo); _Logger = new Logger($"CalParamView[{_FovRecipe.FovName}]"); _FovPatternAgent = FovPatternAgentManager.GetAgent(fovInfo.ToSubSysInfo()); _CamPatternAgent = CamPatternAgentManager.GetAgent(_FovRecipe.CamRecipe.CamId); InitializeComponent(); CreateComboBoxes(); } private void InitPatternList() { try { _FovPatternInfos = _FovPatternAgent.GetMainPatterns(_FovInfo); if (VisionSettingManager.VisionSetting.UseCamPattern && _CamPatternAgent != null) { _CamPatternInfos = _CamPatternAgent.GetMainPatterns(_FovInfo); } ClearComboBoxItems(_CbCalPatterns); AddComboBoxeItems(_CbCalPatterns); SelectComboBoxItem(_CbCalPatterns); } catch (Exception e) { _Logger.FuncE(e.Message); } } private void ClearComboBoxItems(Dictionary cbPatterns) { foreach (var cbPatternPair in cbPatterns) { ComboBox cb = cbPatternPair.Value; cb.Items.Clear(); } } private void CreateComboBoxes() { _CbCalPatterns = new Dictionary(PATTERN_MAX_COUNT); _CbCalPatterns.Add(PATTERN_NUM_1, cbCalPattern); } private void AddComboBoxeItems(Dictionary cbPatterns) { try { foreach (var cbPatternPair in cbPatterns) { ComboBox cb = cbPatternPair.Value; for (int i=0; i< _FovPatternInfos.Count(); i++) { _patternIndexes.Add(new FovPatternIndex(_FovInfo, _FovRecipe.FovName, _FovPatternInfos[i].GetPatternId())); cb.Items.Add(PatternUtil.GetPatternDescription(_FovPatternInfos[i])); } if (VisionSettingManager.VisionSetting.UseCamPattern && _CamPatternAgent != null) { for (int i = 0; i < _CamPatternInfos.Count(); i++) { _patternIndexes.Add(new CamPatternIndex(_FovRecipe.CamRecipe.CamId, _CamPatternInfos[i].GetPatternId())); cb.Items.Add(PatternUtil.GetPatternDescription(_CamPatternInfos[i])); } } } } catch (Exception e) { _Logger.FuncE(e.Message); } } private void SelectComboBoxItem(Dictionary cbPatterns) { try { if (VisionSettingManager.VisionSetting.UseCamPattern) { var startIndex = 0; var endIndex = _FovPatternInfos.Count(); if (!_FovRecipe.CalFovRecipe.IsFovPattern) { startIndex = _FovPatternInfos.Count(); endIndex = _patternIndexes.Count(); } for (int i = startIndex; i < endIndex; i++) { if (_patternIndexes[i].PatternId == _FovRecipe.CalFovRecipe.PatternId) { cbPatterns[PATTERN_NUM_1].SelectedIndex = i; } } } else { for (int i = 0; i < _patternIndexes.Count(); i++) { if (_patternIndexes[i].PatternId == _FovRecipe.CalFovRecipe.PatternId) { cbPatterns[PATTERN_NUM_1].SelectedIndex = i; } } } } catch (Exception e) { _Logger.FuncE(e.Message); } } private void Button_Save_Click(object sender, RoutedEventArgs e) { UpdateRecipeFromUI(); RecipeFileManager.SaveVisionRecipe(); VisionCommonUI.MessageBoxUtil.ShowMessageBoxOk(StrTokens.MSG_O_SaveCompleted); } private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { bool isVisible = (bool)e.NewValue; if (isVisible == true) { RefreshVisionRecipes(); UpdateRecipeFromFile(); } } public void RefreshVisionRecipes() { _FovRecipe = VisionRecipeManager.GetFovRecipe(_FovInfo); (DataContext as CalParamViewModel).RefreshVisionRecipes(); } public void UpdateRecipeFromFile() { (DataContext as CalParamViewModel).UpdateRecipeFromFile(); InitPatternList(); } public void UpdateRecipeFromUI() { (DataContext as CalParamViewModel).CalFovRecipe.PatternId = _patternIndexes[_CbCalPatterns[PATTERN_NUM_1].SelectedIndex].PatternId; if (_patternIndexes[_CbCalPatterns[PATTERN_NUM_1].SelectedIndex] is FovPatternIndex) { (DataContext as CalParamViewModel).CalFovRecipe.IsFovPattern = true; } else { (DataContext as CalParamViewModel).CalFovRecipe.IsFovPattern = false; } (DataContext as CalParamViewModel).UpdateRecipeFromUI(); } private void IsDecimalNumber(object sender, TextCompositionEventArgs e) { decimal d; if (!decimal.TryParse(e.Text, out d)) { e.Handled = true; } } public void Dispose() { _EventDisposable?.Dispose(); _EventDisposable = null; } } }