248 lines
10 KiB
C#
248 lines
10 KiB
C#
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;
|
|
|
|
namespace VisionUI.Setup
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for VisionRepeatView.xaml
|
|
/// </summary>
|
|
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 _FovPatternAgent = null;
|
|
private PatternAgentBase _CamPatternAgent = null;
|
|
private List<PatternInfo> _FovPatternInfos = null;
|
|
private List<PatternInfo> _CamPatternInfos = new List<PatternInfo>();
|
|
private Dictionary<int, ComboBox> _CbVisionRepeatPatterns;
|
|
private List<PatternIndex> _patternIndexes = new List<PatternIndex>();
|
|
//end Pattern
|
|
|
|
private bool _isShapeModelFinder;
|
|
public bool IsShapeModelFinder {
|
|
get { return _isShapeModelFinder; }
|
|
set { _isShapeModelFinder = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 생성자
|
|
/// </summary>
|
|
/// <param name="fovInfo">Fov 정보</param>
|
|
public VisionRepeatView(FovInfo fovInfo)
|
|
{
|
|
_FovInfo = fovInfo;
|
|
_FovRecipe = VisionRecipeManager.GetFovRecipe(_FovInfo);
|
|
_Logger = new Logger($"VisionRepeatView[{_FovInfo.SubSysName}][{_FovRecipe.FovName}]");
|
|
_FovPatternAgent = FovPatternAgentManager.GetAgent(_FovInfo.ToSubSysInfo());
|
|
_CamPatternAgent = CamPatternAgentManager.GetAgent(_FovRecipe.CamRecipe.CamId);
|
|
|
|
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<int, ComboBox>(PATTERN_MAX_COUNT);
|
|
_CbVisionRepeatPatterns.Add(PATTERN_NUM_1, cbVisionRepeatPattern);
|
|
}
|
|
|
|
private void SetObservable() {
|
|
VisionEnv.AuroraEnv.EventObservable.AsObservable(VisionEventId.VISION_REPEAT_SAVE_PARAMS).
|
|
Where(x => x.From == _FovRecipe.FovName).Subscribe(msg => UpdatePatternId(msg));
|
|
}
|
|
|
|
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;
|
|
_isFovPattern = _patternIndexes[_CbVisionRepeatPatterns[PATTERN_NUM_1].SelectedIndex] is FovPatternIndex;
|
|
|
|
if (_isFovPattern) {
|
|
var patternInfo = _FovPatternAgent.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;
|
|
}
|
|
}
|
|
else {
|
|
var patternInfo = _CamPatternAgent.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 {
|
|
_FovPatternInfos = _FovPatternAgent.GetMainPatterns(_FovInfo);
|
|
if (VisionSettingManager.VisionSetting.UseCamPattern && _CamPatternAgent != null) {
|
|
_CamPatternInfos = _CamPatternAgent.GetMainPatterns(_FovInfo);
|
|
}
|
|
ClearComboBoxItems(_CbVisionRepeatPatterns);
|
|
AddComboBoxeItems(_CbVisionRepeatPatterns);
|
|
SelectComboBoxItem(_CbVisionRepeatPatterns);
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
}
|
|
|
|
private void AddComboBoxeItems(Dictionary<int, ComboBox> 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) {
|
|
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<int, ComboBox> cbPatterns) {
|
|
try {
|
|
if (VisionSettingManager.VisionSetting.UseCamPattern) {
|
|
var startIndex = 0;
|
|
var endIndex = _FovPatternInfos.Count();
|
|
if (!VisionRecipeManager.GetVisionRepeatRecipe(_FovInfo).IsFovPattern) {
|
|
startIndex = _FovPatternInfos.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<int, ComboBox> 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
|
|
}
|
|
} |