Allen's 데이터 맛집
C#과 Basler Pylon을 사용한 비전 코딩: EnumerationComboBoxUserControl 클래스 설명 본문
Programming/Pylon .Net API
C#과 Basler Pylon을 사용한 비전 코딩: EnumerationComboBoxUserControl 클래스 설명
Allen93 2024. 8. 17. 16:38C#과 Basler Pylon .NET API를 사용하여 카메라 파라미터를 설정하고 제어할 수 있는 EnumerationComboBoxUserControl 클래스에 대해 설명하겠습니다. 이 클래스는 Pylon 카메라의 열거형 파라미터를 쉽게 선택하고 설정할 수 있는 사용자 컨트롤입니다.
클래스 개요
EnumerationComboBoxUserControl 클래스는 Basler Pylon 카메라의 열거형 파라미터를 사용자 인터페이스에서 쉽게 선택하고 변경할 수 있도록 합니다. 이를 통해 사용자는 카메라의 다양한 설정을 직관적으로 조작할 수 있습니다.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Basler.Pylon;
namespace PylonLiveViewControl
{
public partial class EnumerationComboBoxUserControl : UserControl
{
// Sets up the initial state.
public EnumerationComboBoxUserControl()
{
InitializeComponent();
}
private IEnumParameter parameter = null; // The interface of the enum parameter.
private string defaultName = "N/A";
// Sets the parameter displayed by the user control.
public IEnumParameter Parameter
{
set
{
// Remove the old parameter.
if (parameter != null)
{
parameter.ParameterChanged -= ParameterChanged;
}
// Set the new parameter.
parameter = value;
if (parameter != null)
{
parameter.ParameterChanged += ParameterChanged;
labelName.Text = parameter.Advanced.GetPropertyOrDefault(AdvancedParameterAccessKey.DisplayName, parameter.Name);
UpdateValues();
}
else
{
labelName.Text = defaultName;
}
}
}
// Sets the default name of the control.
public string DefaultName
{
set
{
defaultName = value;
if (parameter == null)
{
labelName.Text = defaultName;
}
}
get
{
return defaultName;
}
}
// Provides the display name and the name of an enum value.
private class EnumValue
{
public EnumValue(IEnumParameter parameter)
{
ValueName = parameter.GetValue();
ValueDisplayName = parameter.GetAdvancedValueProperties(ValueName).GetPropertyOrDefault(AdvancedParameterAccessKey.DisplayName, ValueName);
}
public EnumValue(IEnumParameter parameter, string valueName)
{
ValueName = valueName;
ValueDisplayName = parameter.GetAdvancedValueProperties(valueName).GetPropertyOrDefault(AdvancedParameterAccessKey.DisplayName, valueName);
}
public override string ToString()
{
return ValueDisplayName;
}
public string ValueName;
public string ValueDisplayName;
}
// Occurs when the parameter state has changed. Updates the control.
private void ParameterChanged(Object sender, EventArgs e)
{
if (InvokeRequired)
{
// If called from a different thread, we must use the Invoke method to marshal the call to the proper thread.
BeginInvoke(new EventHandler<EventArgs>(ParameterChanged), sender, e);
return;
}
try
{
// Update the control values.
UpdateValues();
}
catch
{
// If errors occurred, disable the control.
SetErrorState();
}
}
private void SetErrorState()
{
// If errors occurred, disable the control.
comboBox.Items.Clear();
comboBox.Items.Add("<error>");
comboBox.Enabled = false;
labelName.Enabled = false;
}
// Gets the current values from the node and displays them.
private void UpdateValues()
{
try
{
if (parameter != null)
{
// Reset the Combobox.
comboBox.Items.Clear();
// Set the items for the combobox and enable the combobox.
if (parameter.IsWritable && parameter.IsReadable)
{
string selected = parameter.GetValue();
foreach (string valueName in parameter)
{
EnumValue item = new EnumValue(parameter, valueName);
comboBox.Items.Add(item);
if (selected == valueName)
{
comboBox.SelectedIndex = comboBox.Items.Count - 1;
}
}
labelName.Enabled = true;
comboBox.Enabled = true;
}
// Disable the combobox, e.g. if camera is grabbing.
else if (parameter.IsReadable)
{
EnumValue item = new EnumValue(parameter);
comboBox.Items.Add(item);
comboBox.SelectedIndex = comboBox.Items.Count - 1;
labelName.Enabled = true;
comboBox.Enabled = false;
}
// If the parameter is not readable, disable the combobox without setting any items.
else
{
labelName.Enabled = false;
comboBox.Enabled = false;
}
}
}
catch
{
// If errors occurred, disable the control.
SetErrorState();
}
}
// Handles selection changes.
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (parameter != null)
{
try
{
// If the parameter is readable and the combo box selection is ok ...
if (parameter.IsReadable && comboBox.SelectedIndex >= 0)
{
// ... get the displayed selected enumeration value.
EnumValue selectedValue = comboBox.SelectedItem as EnumValue;
if (parameter.CanSetValue(selectedValue.ValueName) && selectedValue.ValueName != parameter.GetValue())
{
parameter.TrySetValue(selectedValue.ValueName);
}
}
}
catch
{
// Ignore any errors here.
}
}
}
}
}
2. 주요 구성 요소 및 기능
a. 초기화
public EnumerationComboBoxUserControl()
{
InitializeComponent();
}
- 초기화: 클래스 생성자에서 컨트롤을 초기화합니다.
b. 파라미터 설정 및 갱신
private IEnumParameter parameter = null; // The interface of the enum parameter.
private string defaultName = "N/A";
// Sets the parameter displayed by the user control.
public IEnumParameter Parameter
{
set
{
if (parameter != null)
{
parameter.ParameterChanged -= ParameterChanged;
}
parameter = value;
if (parameter != null)
{
parameter.ParameterChanged += ParameterChanged;
labelName.Text = parameter.Advanced.GetPropertyOrDefault(AdvancedParameterAccessKey.DisplayName, parameter.Name);
UpdateValues();
}
else
{
labelName.Text = defaultName;
}
}
}
- 파라미터 설정: 파라미터가 변경될 때마다 이벤트 핸들러를 등록/제거하고, 파라미터 값에 따라 컨트롤을 업데이트합니다.
c. 기본 이름 설정
public string DefaultName
{
set
{
defaultName = value;
if (parameter == null)
{
labelName.Text = defaultName;
}
}
get
{
return defaultName;
}
}
- 기본 이름 설정: 파라미터가 없는 경우 표시할 기본 이름을 설정합니다.
d. 열거형 값 클래스
private class EnumValue
{
public EnumValue(IEnumParameter parameter)
{
ValueName = parameter.GetValue();
ValueDisplayName = parameter.GetAdvancedValueProperties(ValueName).GetPropertyOrDefault(AdvancedParameterAccessKey.DisplayName, ValueName);
}
public EnumValue(IEnumParameter parameter, string valueName)
{
ValueName = valueName;
ValueDisplayName = parameter.GetAdvancedValueProperties(valueName).GetPropertyOrDefault(AdvancedParameterAccessKey.DisplayName, valueName);
}
public override string ToString()
{
return ValueDisplayName;
}
public string ValueName;
public string ValueDisplayName;
}
- EnumValue 클래스: 열거형 값의 이름과 표시 이름을 저장합니다. 이 클래스는 콤보 박스에 표시될 항목을 나타냅니다.
e. 파라미터 변경 처리
private void ParameterChanged(Object sender, EventArgs e)
{
if (InvokeRequired)
{
BeginInvoke(new EventHandler<EventArgs>(ParameterChanged), sender, e);
return;
}
try
{
UpdateValues();
}
catch
{
SetErrorState();
}
}
- 파라미터 변경 처리: 파라미터 상태가 변경되었을 때 컨트롤 값을 업데이트합니다. 다른 스레드에서 호출된 경우 Invoke 메서드를 사용하여 호출을 적절한 스레드로 마샬링합니다.
f. 오류 상태 설정
private void SetErrorState()
{
comboBox.Items.Clear();
comboBox.Items.Add("<error>");
comboBox.Enabled = false;
labelName.Enabled = false;
}
- 오류 상태 설정: 오류가 발생했을 때 컨트롤을 비활성화하고 오류 메시지를 표시합니다.
g. 값 업데이트
private void UpdateValues()
{
try
{
if (parameter != null)
{
comboBox.Items.Clear();
if (parameter.IsWritable && parameter.IsReadable)
{
string selected = parameter.GetValue();
foreach (string valueName in parameter)
{
EnumValue item = new EnumValue(parameter, valueName);
comboBox.Items.Add(item);
if (selected == valueName)
{
comboBox.SelectedIndex = comboBox.Items.Count - 1;
}
}
labelName.Enabled = true;
comboBox.Enabled = true;
}
else if (parameter.IsReadable)
{
EnumValue item = new EnumValue(parameter);
comboBox.Items.Add(item);
comboBox.SelectedIndex = comboBox.Items.Count - 1;
labelName.Enabled = true;
comboBox.Enabled = false;
}
else
{
labelName.Enabled = false;
comboBox.Enabled = false;
}
}
}
catch
{
SetErrorState();
}
}
- 값 업데이트: 파라미터 값을 읽어 콤보 박스를 업데이트합니다. 파라미터가 쓰기 가능하고 읽기 가능한 경우 콤보 박스를 활성화하고 값을 설정합니다.
h. 선택 변경 처리
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (parameter != null)
{
try
{
if (parameter.IsReadable && comboBox.SelectedIndex >= 0)
{
EnumValue selectedValue = comboBox.SelectedItem as EnumValue;
if (parameter.CanSetValue(selectedValue.ValueName) && selectedValue.ValueName != parameter.GetValue())
{
parameter.TrySetValue(selectedValue.ValueName);
}
}
}
catch
{
// Ignore any errors here.
}
}
}
- 선택 변경 처리: 콤보 박스에서 선택이 변경될 때 파라미터 값을 업데이트합니다.
EnumerationComboBoxUserControl 클래스는 Basler Pylon 카메라의 열거형 파라미터를 사용자 인터페이스에서 쉽게 선택하고 설정할 수 있는 강력한 도구입니다. 이를 통해 사용자는 카메라의 다양한 설정을 직관적으로 조작할 수 있습니다.
728x90