using System;
using System.Collections.Generic;
using System.IO;
using HalconDotNet;
public static class HalconCameraConfig
{
/// <summary>
/// Set camera parameters from a 2-row CSV file (header + values)
/// </summary>
public static void ApplyCameraParamsFromCsv2Row(HTuple acqHandle, string csvFilePath)
{
if (!File.Exists(csvFilePath))
throw new FileNotFoundException("CSV file not found", csvFilePath);
var lines = File.ReadAllLines(csvFilePath);
if (lines.Length < 2)
throw new FormatException("CSV file must contain two rows: header and values");
var headers = lines[0].Split(',');
var values = lines[1].Split(',');
if (headers.Length != values.Length)
throw new FormatException("The number of headers and values do not match");
for (int i = 0; i < headers.Length; i++)
{
string key = headers[i].Trim();
string valStr = values[i].Trim();
if (string.IsNullOrEmpty(key))
continue;
HTuple val;
if (double.TryParse(valStr, out double d))
val = d;
else if (int.TryParse(valStr, out int n))
val = n;
else
val = valStr;
try
{
HOperatorSet.SetFramegrabberParam(acqHandle, key, val);
Console.WriteLine($"Set: {key} = {val}");
}
catch (HalconException ex)
{
Console.WriteLine($"Failed to set parameter: {key} = {val} → {ex.Message}");
}
}
}
/// <summary>
/// Save parameters as 2-row CSV (header + values) from a dictionary
/// </summary>
public static void SaveCameraParamsToCsv2Row(string csvFilePath, Dictionary<string, string> paramDict)
{
if (paramDict == null || paramDict.Count == 0)
throw new ArgumentException("Parameter dictionary is empty", nameof(paramDict));
var headers = string.Join(",", paramDict.Keys);
var values = string.Join(",", paramDict.Values);
File.WriteAllLines(csvFilePath, new[] { headers, values });
}
}