using Aurora.Api; using Aurora; using Aurora.View; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using VisionCommon; using System.Windows.Input; using System.Windows; namespace VisionUI.RecipeMenu { /// /// 비전 레시피 메뉴 뷰 모델 베이스 (선언) /// public partial class UCVisionRecipeMenuViewModelBase : NotifyPropertyChangedBase { #region Properties // Command Parameter public ICommand CMD_OpenPropertyGrid { get; } public ICommand CMD_AddUnit { get; } public ICommand CMD_DeleteUnit { get; } public ICommand CMD_AddSubSysRecipe { get; } public ICommand CMD_DeleteSubSysRecipe { get; } public ICommand CMD_AddRecipe { get; } internal IAuroraEnvironment _Aurora; // Vision Common Parameters public List UnitIdList { get; } public List SubSysIdList { get; } public List SubSysTypeList { get; } public List FovIdList { get; } public List FovUiPosList { get; } public List CamIdList { get; } public List LightIdList { get; } public List DirectionList { get; } public List DisplayTypeList { get; } public List SubsystemPropertyNameList { get; } private VisionDef _SelectedVisionDef; public VisionDef SelectedVisionDef { get => _SelectedVisionDef; set => SetProperty(ref _SelectedVisionDef, value); } Logger Log = new Logger("VisionDefEditor"); #endregion #region Method public virtual bool CanRename(string oldName, string newName, out string errMsg) { errMsg = ""; return true; } // Vision Def PropertyGrid private void OpenPropertyGrid(object obj) { var popup = CreatePopUP("VisionPropertyGrid", new UCDevPropertyGrid() { DataContext = SelectedVisionDef }, new Size(1200, 800)); popup.Show(); } private void DeleteSubSysRecipe(object obj) { if (SelectedVisionDef == null) return; if (SelectedVisionDef.UnitRecipes == null) return; if (obj is SubSysRecipe subSysRecipe) { var unit = SelectedVisionDef.UnitRecipes.FirstOrDefault(x => x.SubSysRecipes.Contains(subSysRecipe)); if (unit != null) { unit.SubSysRecipes.Remove(subSysRecipe); NotifyPropertyChanged(nameof(SelectedVisionDef)); } } } private void AddSubSysRecipe(object obj) { if (obj is UnitRecipe unitRecipe) { int ID = ReturnLeastEigenNumber(unitRecipe.SubSysRecipes.ToList(), typeof(SubSysRecipe), "SubSysId"); if (unitRecipe.SubSysRecipes.Count < (int)eSubSysId.Max) { var subSysRecipe = new SubSysRecipe(); AddSubSysRecipeInside(subSysRecipe, ID); unitRecipe.SubSysRecipes.Add(subSysRecipe); NotifyPropertyChanged(nameof(SelectedVisionDef)); } else { MessageUtil.ShowErrorToast("Exceed Maximum SubSysRecipe Count : " + (int)eSubSysId.Max); } } } private void DeleteUnit(object obj) { if (SelectedVisionDef == null) return; if (SelectedVisionDef.UnitRecipes == null) return; if (obj is UnitRecipe unitRecipe) { if (SelectedVisionDef.UnitRecipes.Contains(unitRecipe)) { SelectedVisionDef.UnitRecipes.Remove(unitRecipe); NotifyPropertyChanged(nameof(SelectedVisionDef)); } } } private void AddUnit(object obj) { if (SelectedVisionDef == null) return; if (SelectedVisionDef.UnitRecipes == null) return; if (SelectedVisionDef.UnitRecipes.Count < (int)eUnitId.Max) { int ID = ReturnLeastEigenNumber(SelectedVisionDef.UnitRecipes.ToList(), typeof(UnitRecipe), "UnitId"); var unitRecipe = new UnitRecipe(); unitRecipe.UnitId = (eUnitId)ID; unitRecipe.UnitName = ((eUnitId)ID).ToString(); var subSysRecipe = new SubSysRecipe(); AddSubSysRecipeInside(subSysRecipe, 0); unitRecipe.SubSysRecipes.Add(subSysRecipe); SelectedVisionDef.UnitRecipes.Add(unitRecipe); NotifyPropertyChanged(nameof(SelectedVisionDef)); } else { MessageUtil.ShowErrorToast("Exceed Maximum UnitRecipe Count : " + (int)eUnitId.Max); } } /// /// 레시피 추가 /// /// SubsysRecipe public void AddRecipe(object obj) { if (SelectedVisionDef == null) return; if (SelectedVisionDef.UnitRecipes == null) return; if (obj is SubSysRecipe subSysRecipe) { if (SelectedSubsystemPropertyName == "FovRecipes") { AddFovRecipe(subSysRecipe); } else if (SelectedSubsystemPropertyName == "AlignRecipes") { AddAlignRecipe(subSysRecipe); } } } private void AddAlignRecipe(SubSysRecipe subSysRecipe) { int ID = ReturnLeastEigenNumber(subSysRecipe.AlignRecipes.ToList(), typeof(AlignRecipe), "AlignId"); if (subSysRecipe.AlignRecipes.Count < (int)eAlignId.Max) { subSysRecipe.AlignRecipes.Add(new AlignRecipe() { AlignId = (eAlignId)ID, AlignName = ((eAlignId)ID).ToString(), }); NotifyPropertyChanged(nameof(SelectedVisionDef)); } else { MessageUtil.ShowErrorToast("Exceed Maximum AlignRecipe Count : " + (int)eAlignId.Max); } } private void AddFovRecipe(SubSysRecipe subSysRecipe) { int ID = ReturnLeastEigenNumber(subSysRecipe.FovRecipes.ToList(), typeof(FovRecipe), "FovId"); if (subSysRecipe.FovRecipes.Count < (int)eFovId.Max) { subSysRecipe.FovRecipes.Add(new FovRecipe() { FovId = (eFovId)ID, FovName = ((eFovId)ID).ToString(), }); NotifyPropertyChanged(nameof(SelectedVisionDef)); } else { MessageUtil.ShowErrorToast("Exceed Maximum FovRecipe Count : " + (int)eFovId.Max); } } private void AddSubSysRecipeInside(SubSysRecipe subSysRecipe, int ID) { subSysRecipe.SubSysId = (eSubSysId)ID; subSysRecipe.SubSysName = ((eSubSysId)ID).ToString(); subSysRecipe.SubSysType = eSubSysType.Align; subSysRecipe.FovRecipes.Add(new FovRecipe() { FovId = eFovId.Fov1, FovName = eFovId.Fov1.ToString() }); subSysRecipe.AlignRecipes.Add(new AlignRecipe() { AlignId = eAlignId.Align1, AlignName = eAlignId.Align1.ToString() }); } /// /// 팝업창 생성 /// /// 타이틀 /// 내용 /// 사이즈 /// 끝난 후 동작할 이벤트 핸들러 /// 최상단 고정여부 /// public static Window CreatePopUP(string title, object Content, Size size, EventHandler closedEvent = null, bool isTopMost = true) { Window targetWindow = Application.Current.Windows.OfType().FirstOrDefault(w => w.Title == title); if (targetWindow is null) { Window wnd = new Window(); wnd.Content = Content; wnd.Topmost = isTopMost; wnd.Title = title; wnd.Owner = Application.Current.MainWindow; wnd.WindowStartupLocation = WindowStartupLocation.CenterOwner; wnd.Closed += closedEvent; if (size == Size.Empty) { wnd.SizeToContent = SizeToContent.WidthAndHeight; } else { wnd.Width = size.Width; wnd.Height = size.Height; } return wnd; } else { targetWindow.Topmost = isTopMost; targetWindow.WindowState = WindowState.Normal; targetWindow.Focus(); } return targetWindow; } private int ReturnLeastEigenNumber(List list, Type propertyType, string propertyName) { if (list.Count() > 0) { int before = -1; List sortedList = (from id in list select Convert.ToInt32(propertyType.GetProperty(propertyName).GetValue(id))).ToList(); sortedList.Sort(); foreach (var current in sortedList) { if (current - before > 1) { break; } else { before = current; } } return before + 1; } return 0; } /// /// 좌표시스템 계산 /// /// 서브시스템 레시피 public void CalculateCoordSystem(SubSysRecipe subSysRecipe) { CoordSystem TeachingDirector = new CoordSystem(); CoordSystem CoordConverter = new CoordSystem(); CoordSystem AlignDirector = new CoordSystem(); if (subSysRecipe.LayoutRecipe.MoveObjectTypeX == eMoveObject.CamMove) { TeachingDirector = (CoordSystem)subSysRecipe.LayoutRecipe.MotorDirection.XFlip(); CoordConverter = subSysRecipe.LayoutRecipe.MotorDirection; } else { TeachingDirector = subSysRecipe.LayoutRecipe.MotorDirection; CoordConverter = (CoordSystem)subSysRecipe.LayoutRecipe.MotorDirection.XFlip(); } if (subSysRecipe.LayoutRecipe.MoveObjectTypeY == eMoveObject.CamMove) { TeachingDirector = (CoordSystem)TeachingDirector.YFlip(); } else { CoordConverter = (CoordSystem)CoordConverter.YFlip(); } if (subSysRecipe.LayoutRecipe.IsAlignMotor) { if (subSysRecipe.LayoutRecipe.AlignMoveObjectTypeX == eMoveObject.CamMove) { AlignDirector = (CoordSystem)subSysRecipe.LayoutRecipe.AlignMotorDirection.XFlip(); } else { AlignDirector = subSysRecipe.LayoutRecipe.AlignMotorDirection; } if (subSysRecipe.LayoutRecipe.AlignMoveObjectTypeY == eMoveObject.CamMove) { AlignDirector = (CoordSystem)AlignDirector.YFlip(); } } else { AlignDirector = TeachingDirector; } subSysRecipe.TeachingDirectorRecipe.CoordSystem = TeachingDirector; subSysRecipe.CoordConvRecipe.CoordSystem = CoordConverter; subSysRecipe.AlignDirectorRecipe.CoordSystem = AlignDirector; } #endregion } }