/* * Copyright (c) 2025 LG Electronics Inc. * SPDX-License-Identifier: LicenseRef-LGE-Proprietary */ using Aurora; using Aurora.Api; using Aurora.View; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Reactive.Linq; using System.Windows; using System.Windows.Data; using System.Windows.Input; using VisionCommon; using VisionCommonUI; using VisionCommonUI.Resources; using VisionSequence.Setup; namespace VisionUI.Setup { /// /// 캘리브레이션 세팅 뷰 모델 /// public class CalSettingViewModel : NotifyPropertyChangedBase, IUpdateRecipe { private Logger _Logger = new Logger(typeof(CalSettingViewModel).Name); private IEventObservable _EventObservable = VisionEnv.AuroraEnv.EventObservable; /// 캘리브레이션 플랜 아이템 컬렉션 public ObservableCollection _PlanItems; /// 캘리브레이션 플랜 생성 이벤트 핸들러 public event EventHandler CalPlanCreated; /// 캘리브레이션 시퀀스 취소 이벤트 핸들러 public event EventHandler CancelSequence; private SubSysInfo _SubSysInfo; private SubSysRecipe _SubSysRecipe; #region Binding Properties private CalSetupRecipe _CalSetupRecipe; /// 캘리브레이션 셋업 레시피 public CalSetupRecipe CalSetupRecipe { get => _CalSetupRecipe; set => SetProperty(ref _CalSetupRecipe, value); } #endregion #region ButtonCommand public ICommand ButtonCommandStart { get; set; } public ICommand ButtonCommandStop { get; set; } public ICommand ButtonCommandSave { get; set; } #endregion /// /// 생성자 /// /// 서브시스템 정보 public CalSettingViewModel(SubSysInfo subSysInfo) { _SubSysInfo = subSysInfo; _SubSysRecipe = VisionRecipeManager.GetSubSysRecipe(_SubSysInfo); _PlanItems = new ObservableCollection(); ButtonCommandStart = new DelegateCommand(new Action(DoStart)); ButtonCommandStop = new DelegateCommand(new Action(DoStop)); ButtonCommandSave = new DelegateCommand(new Action(DoSave)); } /// /// 시작 /// /// null public void DoStart(object o = null) { if (MessageBoxUtil.ShowMessageBoxQuestion( string.Format(StrTokens.MSG_Q_ConfirmRunCalibration)) == MessageBoxResult.Yes) { if (!IsCalSettingParamsValid()) { return; } StartCalSeq(); } } /// /// 시퀀스 정지 /// /// null public void DoStop(object o = null) { CancelSequence.Invoke(this, new EventArgs()); } /// /// 세팅레시피 저장 /// /// null public void DoSave(object o = null) { UpdateRecipeFromUI(); RecipeFileManager.SaveVisionRecipe(); MessageBoxUtil.ShowMessageBoxOk(StrTokens.MSG_O_SaveCompleted); } private void StartCalSeq() { try { var plan = new CalPlan((int)CalSetupRecipe.DistanceStep, CalSetupRecipe.DistanceUnit, (int)CalSetupRecipe.RotationStep, CalSetupRecipe.RotationUnit); MakePlanItems(plan.MakeCalPlan(true, CalSetupRecipe.isX(), CalSetupRecipe.isY(), CalSetupRecipe.isT())); // 기존 //CalPlanCreated.Invoke(this, new SetupSeqParams_Cal() { // FovInfo = _FovInfo, // PlanItems = _PlanItems.ToList(), // DisplayType = eDisplayType.Calibration //}); // 변경 (0124) CalPlanCreated.Invoke(this, new CalSeqParams() { SubSysInfo = _SubSysInfo, DisplayType = eDisplayType.Calibration, PlanItems = _PlanItems.ToList(), CalSetupRecipe = CalSetupRecipe }); } catch (Exception e) { _Logger.FuncE(e.Message); } } private bool IsCalSettingParamsValid() { var distanceUnitMax = 3; // 한 스텝당 이동은 3mm를 넘을 수 없음. var rotationUnitMax = 90; // 한 스텝당 회전은 90도를 넘을 수 없음. if (CalSetupRecipe.DistanceUnit <= 0 || CalSetupRecipe.DistanceUnit > distanceUnitMax) { MessageBoxUtil.ShowMessageBoxError( string.Format(StrTokens.MSG_E_NumberInvalid, "DistanceUnit", 0, distanceUnitMax)); return false; } if (CalSetupRecipe.RotationUnit <= 0 || CalSetupRecipe.RotationUnit > rotationUnitMax) { MessageBoxUtil.ShowMessageBoxError( string.Format(StrTokens.MSG_E_NumberInvalid, "RotationUnit", 0, rotationUnitMax)); return false; } return true; } private void MakePlanItems(Dictionary plan) { _PlanItems.Clear(); string planStep, planPurpose, planTarget, planAction, planX, planY, planT; planStep = planPurpose = planTarget = planAction = planX = planY = planT = ""; foreach (var step in plan) { planStep = step.Key; foreach (var purpose in (Dictionary)step.Value) { planPurpose = purpose.Key; foreach (var target in (Dictionary)purpose.Value) { planTarget = target.Key; foreach (var action in (Dictionary)target.Value) { planAction = action.Key; foreach (var position in (Dictionary)action.Value) { if (position.Key == "x") { planX = position.Value.ToString(); } else if (position.Key == "y") { planY = position.Value.ToString(); } else if (position.Key == "t") { planT = position.Value.ToString(); } else { } } } } } PlanItem planItem = new PlanItem(planStep, planPurpose, planTarget, planAction, planX, planY, planT); _PlanItems.Add(planItem); } } public void RefreshVisionRecipes() { _SubSysRecipe = VisionRecipeManager.GetSubSysRecipe(_SubSysInfo); } public void UpdateRecipeFromFile() { var calSetupRecipe = VisionRecipeManager.GetCalSetupRecipe(_SubSysInfo); CalSetupRecipe = calSetupRecipe.Clone(); } public void UpdateRecipeFromUI() { var calSetupRecipe = VisionRecipeManager.GetCalSetupRecipe(_SubSysInfo); CalSetupRecipe.CopyTo(calSetupRecipe); } } }