/* * 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.Controls; using Aurora.Api; using VisionCommon; using System.Reactive.Linq; using System.Reactive.Disposables; namespace VisionUI.Setup { /// /// Interaction logic for VisionRepeatView.xaml /// public partial class VisionRepeatView : UserControl, IUpdateRecipe, IDisposable { private static Logger _Logger; private FovInfo _FovInfo; private FovRecipe _FovRecipe; //Patern private int _PatternId; private bool _IsFovPattern; private const int PATTERN_NUM_1 = 1; private const int PATTERN_MAX_COUNT = 3; private const string PATTERN_UNDEFINED = "Undefined"; private PatternAgentBase _PatternAgent = null; private List _PatternInfos = null; private Dictionary _CbVisionRepeatPatterns; private List _PatternIndexes = new List(); //end Pattern private bool _isShapeModelFinder; public bool IsShapeModelFinder { get { return _isShapeModelFinder; } set { _isShapeModelFinder = value; } } /// /// 생성자 /// /// Fov 정보 public VisionRepeatView(FovInfo fovInfo) { _FovInfo = fovInfo; _FovRecipe = VisionRecipeManager.GetFovRecipe(_FovInfo); _Logger = new Logger($"VisionRepeatView[{_FovInfo.SubSysName}][{_FovRecipe.FovName}]"); _PatternAgent = PatternAgentManager.GetAgent(_FovInfo); InitializeComponent(); } private void Init() { CreateCameraView(); CreateComboBoxes(); SetObservable(); } private void CreateCameraView() { var cameraView = IpViewManager.CreateCameraView(_FovInfo, eDisplayType.VisionRepeat); gridCameraView.Children.Add(cameraView); } private void CreateComboBoxes() { _CbVisionRepeatPatterns = new Dictionary(PATTERN_MAX_COUNT); _CbVisionRepeatPatterns.Add(PATTERN_NUM_1, cbVisionRepeatPattern); } private void SetObservable() { var disposable = VisionEnv.AuroraEnv.EventObservable.AsObservable(VisionEventId.NOTIFY_VISION_REPEAT_SAVE_PARAMS). Where(x => x.From == _FovRecipe.FovName).Subscribe(msg => UpdatePatternId(msg)); DisposableManager.AddDisposable(eDpsGroupKey.SubMenu_VisionRepeat, disposable); } private void UpdatePatternId(EventMessage msg) { var visionRepeatRecipe = VisionRecipeManager.GetVisionRepeatRecipe(_FovInfo); //visionRepeatRecipe.Enabled = (msg.Param as VisionRepeatRecipe).Enabled; visionRepeatRecipe.RepeatCount = (msg.Param as VisionRepeatRecipe).RepeatCount; visionRepeatRecipe.PatternId = _PatternId; visionRepeatRecipe.IsFovPattern = _IsFovPattern; RecipeFileManager.SaveVisionRecipe(); } private void cbVisionRepeatPattern_SelectionChanged(object sender, SelectionChangedEventArgs e) { if ((sender as ComboBox).SelectedIndex != -1) { _PatternId = _PatternIndexes[_CbVisionRepeatPatterns[PATTERN_NUM_1].SelectedIndex].PatternId; var patternInfo = _PatternAgent.GetPatternInfo(_PatternIndexes[_CbVisionRepeatPatterns[PATTERN_NUM_1].SelectedIndex]); (DataContext as VisionRepeatViewModel).SelectedPatternInfo = patternInfo; if (patternInfo.PatternType == ePatternType.ShapeModelFinder) { (DataContext as VisionRepeatViewModel).IsShapeModelFinder = true; } else { (DataContext as VisionRepeatViewModel).IsShapeModelFinder = false; } } } private void InitPatternList() { try { _PatternInfos = _PatternAgent.GetMainPatterns(_FovInfo); ClearComboBoxItems(_CbVisionRepeatPatterns); AddComboBoxeItems(_CbVisionRepeatPatterns); SelectComboBoxItem(_CbVisionRepeatPatterns); } catch (Exception e) { _Logger.FuncE(e.Message); } } private void AddComboBoxeItems(Dictionary cbPatterns) { try { foreach (var cbPatternPair in cbPatterns) { ComboBox cb = cbPatternPair.Value; for (int i = 0; i < _PatternInfos.Count(); i++) { _PatternIndexes.Add(new PatternIndex(_FovInfo, _FovRecipe.FovName, _PatternInfos[i].GetPatternId())); cb.Items.Add(PatternUtil.GetPatternDescription(_PatternInfos[i])); } } } catch (Exception e) { _Logger.FuncE(e.Message); } } private void SelectComboBoxItem(Dictionary cbPatterns) { try { if (VisionSettingManager.VisionSetting.UseCamPattern) { var startIndex = 0; var endIndex = _PatternInfos.Count(); if (!VisionRecipeManager.GetVisionRepeatRecipe(_FovInfo).IsFovPattern) { startIndex = _PatternInfos.Count(); endIndex = _PatternIndexes.Count(); } for (int i = startIndex; i < endIndex; i++) { if (_PatternIndexes[i].PatternId == VisionRecipeManager.GetVisionRepeatRecipe(_FovInfo).PatternId) { cbPatterns[PATTERN_NUM_1].SelectedIndex = i; } } } else { for (int i = 0; i < _PatternIndexes.Count(); i++) { if (_PatternIndexes[i].PatternId == VisionRecipeManager.GetVisionRepeatRecipe(_FovInfo).PatternId) { cbPatterns[PATTERN_NUM_1].SelectedIndex = i; } } } } 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 UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { bool isVisible = (bool)e.NewValue; if (isVisible == true) { RefreshVisionRecipes(); Init(); UpdateRecipeFromFile(); (DataContext as VisionRepeatViewModel).LoadCsvNow(); } else { ISequence seq = VisionEnv.AuroraEnv.SequenceEntry[VisionKeys.VisionRepeatSeqKey]; if (seq != null) { if (seq.RunState != SequenceRunState.Idle) { seq.EmergencyStop(); } } seq = VisionEnv.AuroraEnv.SequenceEntry[VisionKeys.VisionRepeatDynamicSeqKey]; if (seq != null) { if (seq.RunState != SequenceRunState.Idle) { seq.EmergencyStop(); } } Dispose(); } } public void RefreshVisionRecipes() { _FovRecipe = VisionRecipeManager.GetFovRecipe(_FovInfo); (DataContext as VisionRepeatViewModel).RefreshVisionRecipes(); } public void UpdateRecipeFromFile() { InitPatternList(); } public void UpdateRecipeFromUI() { throw new NotImplementedException(); } #region Dispose public void Dispose() { RemoveComboBoxes(); RemoveCameraView(); } private void RemoveComboBoxes() { _CbVisionRepeatPatterns.Clear(); _CbVisionRepeatPatterns = null; } private void RemoveCameraView() { foreach (var child in gridCameraView.Children) { if (child is UserControl view) { IpViewManager.DisposeCameraView(view); } } gridCameraView.Children.Clear(); } #endregion } }