247 lines
8.2 KiB
C#
247 lines
8.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using WindowsFormsApp5;
|
|
using Excel = Microsoft.Office.Interop.Excel;
|
|
|
|
namespace daq_testing
|
|
{
|
|
public partial class UserControlMeasureHeader : UserControl
|
|
{
|
|
public string ProjectNumber { get => tbProjectNumber.Text; set => tbProjectNumber.Text = value; }
|
|
public string MeasurementLocation { get => lbLocation.SelectedItem.ToString(); }
|
|
public string CurrentMeasurePosition { get => tbCurrentPosition.Text; set => tbCurrentPosition.Text = value; }
|
|
|
|
public int BiBID
|
|
{
|
|
get => int.TryParse(tbBibID.Text, out int result) ? result : 0;
|
|
set => tbBibID.Text = value.ToString();
|
|
}
|
|
|
|
public string DriverID
|
|
{
|
|
get => tbDriverID.Text;
|
|
set => tbBibID.Text = value;
|
|
}
|
|
|
|
public List<PslineConfig> pslineConfigs = new List<PslineConfig>();
|
|
|
|
public List<int> CheckedItems = new List<int>();
|
|
|
|
public event EventHandler MeasureButtonClick;
|
|
public event EventHandler ClearAllMeasurementsBtnClick;
|
|
public event EventHandler PopuplateHeaderEvent;
|
|
|
|
public void PopulateMeasureHeader()
|
|
{
|
|
lbMeasuredPins.Items.Clear();
|
|
foreach (var item in pslineConfigs)
|
|
{
|
|
if (item.Enabled)
|
|
{
|
|
lbMeasuredPins.Items.Add(item.Psline);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public void OnPopulateHeaderEvent(EventArgs e)
|
|
{
|
|
PopuplateHeaderEvent?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
public UserControlMeasureHeader()
|
|
{
|
|
InitializeComponent();
|
|
dataGridView1.AutoGenerateColumns = true;
|
|
PopulateHeaders();
|
|
lbLocation.SetSelected(0, true);
|
|
lbMeasuredPins.CheckOnClick = true; // Enables to single click.
|
|
}
|
|
|
|
private void PopulateHeaders ()
|
|
{
|
|
OnPopulateHeaderEvent(EventArgs.Empty);
|
|
}
|
|
public void OnClearAllMeasurementsBtnClick(EventArgs e)
|
|
{
|
|
ClearAllMeasurementsBtnClick?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
|
|
public void OnMeasureButtonClick(EventArgs e)
|
|
{
|
|
MeasureButtonClick?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
|
|
private void btnMeasure_Click(object sender, EventArgs e)
|
|
{
|
|
OnMeasureButtonClick(EventArgs.Empty);
|
|
}
|
|
|
|
private void btnNextPosition_Click(object sender, EventArgs e)
|
|
{
|
|
int position;
|
|
int increment;
|
|
int.TryParse(this.CurrentMeasurePosition,out position);
|
|
int.TryParse(tbIncrement.Text, out increment);
|
|
position+=increment;
|
|
this.CurrentMeasurePosition = position.ToString();
|
|
}
|
|
|
|
public void PopulateVoltageOld(List<Result> results)
|
|
{
|
|
results = results.OrderBy(o => o.PsLine).ToList();
|
|
|
|
dataGridView1.DataSource = null;
|
|
dataGridView1.DataSource = results;
|
|
dataGridView1.Refresh();
|
|
|
|
}
|
|
|
|
public void PopulateVoltage(List<Result> results)
|
|
{
|
|
// Convert the list to a DataTable
|
|
var dataTable = DataTableHelper.ToDataTable(results);
|
|
|
|
// Bind the DataTable to DataGridView
|
|
dataGridView1.DataSource = dataTable;
|
|
|
|
dataGridView1.CellFormatting += DataGridView_CellFormatting;
|
|
|
|
// Enable sorting for all columns
|
|
foreach (DataGridViewColumn column in dataGridView1.Columns)
|
|
{
|
|
column.SortMode = DataGridViewColumnSortMode.Automatic;
|
|
}
|
|
|
|
// Sort the DataGridView by the first column (assuming it's "ID")
|
|
if (dataGridView1.Columns.Count > 0)
|
|
{
|
|
dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);
|
|
}
|
|
}
|
|
|
|
private void DataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
|
|
{
|
|
// Check if the column is "Value"
|
|
if (dataGridView1.Columns[e.ColumnIndex].Name == "Value")
|
|
{
|
|
// Get the value from the cell
|
|
if (e.Value is double value)
|
|
{
|
|
// Get Psline
|
|
var PsLine = (int)dataGridView1.Rows[e.RowIndex].Cells["PsLine"].Value;
|
|
var tolerance = pslineConfigs.Where(x => x.Psline == PsLine).Select(x => x.Tolerance).FirstOrDefault();
|
|
// Get setpoint
|
|
var setpoint = pslineConfigs.Where(x => x.Psline == PsLine).Select(x => x.Setpoint).FirstOrDefault();
|
|
|
|
//Calculate limit
|
|
var limit = setpoint * (tolerance/100);
|
|
|
|
// check if exceeding tolerance limit
|
|
if (Math.Abs(setpoint-value) > limit)
|
|
{
|
|
// Change the background color
|
|
e.CellStyle.BackColor = System.Drawing.Color.Orange;
|
|
}
|
|
else
|
|
{
|
|
e.CellStyle.BackColor = System.Drawing.Color.Green;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnSaveToExcel_Click(object sender, EventArgs e)
|
|
{
|
|
string filePath = $@"C:\configs\P{tbProjectNumber.Text}_sub{tbSub.Text}_step_{tbStep.Text}_Header.xlsx";
|
|
string sourceFilePath = @"C:\configs\Header.xlsx";
|
|
|
|
|
|
|
|
if (!File.Exists(filePath))
|
|
{
|
|
File.Copy(sourceFilePath, filePath);
|
|
Console.WriteLine($"File did not exist. Copied from {sourceFilePath} to {filePath}.");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("File exists, proceeding to open.");
|
|
}
|
|
|
|
Excel.Application excelApp = new Excel.Application
|
|
{
|
|
Visible = true,
|
|
DisplayAlerts = false
|
|
};
|
|
|
|
Excel.Workbook workbook = excelApp.Workbooks.Open(filePath);
|
|
|
|
string copiedSheetName = "Measurements";
|
|
|
|
Excel.Worksheet worksheet = workbook.Sheets[1];
|
|
// Add column headers
|
|
for (int i = 0; i < dataGridView1.Columns.Count; i++)
|
|
{
|
|
worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
|
|
}
|
|
|
|
// Add rows
|
|
for (int i = 0; i < dataGridView1.Rows.Count; i++)
|
|
{
|
|
for (int j = 0; j < dataGridView1.Columns.Count; j++)
|
|
{
|
|
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value;
|
|
}
|
|
}
|
|
|
|
workbook.Save();
|
|
workbook.Close();
|
|
excelApp.Quit();
|
|
|
|
//ReleaseObject(workbook);
|
|
//ReleaseObject(excelApp);
|
|
}
|
|
|
|
private void lbMeasuredPins_ItemCheck(object sender, ItemCheckEventArgs e)
|
|
{
|
|
List<int> checkedItems = new List<int>();
|
|
foreach (var item in lbMeasuredPins.CheckedItems)
|
|
{
|
|
checkedItems.Add(Convert.ToInt32(item));
|
|
}
|
|
if (e.NewValue == CheckState.Checked)
|
|
checkedItems.Add(Convert.ToInt32(lbMeasuredPins.Items[e.Index]));
|
|
else
|
|
checkedItems.Remove(Convert.ToInt32(lbMeasuredPins.Items[e.Index]));
|
|
Console.WriteLine(checkedItems);
|
|
this.CheckedItems = checkedItems;
|
|
}
|
|
|
|
private void btnInvertListboxMeasuredPins_Click(object sender, EventArgs e)
|
|
{
|
|
for (int i = 0; i < lbMeasuredPins.Items.Count; i++)
|
|
{
|
|
bool isChecked = lbMeasuredPins.GetItemChecked(i);
|
|
lbMeasuredPins.SetItemChecked(i, !isChecked);
|
|
}
|
|
}
|
|
|
|
private void btnClearAllMeasurements_Click(object sender, EventArgs e)
|
|
{
|
|
OnClearAllMeasurementsBtnClick(EventArgs.Empty);
|
|
}
|
|
|
|
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|