private void btnInterpMode_Click(object sender, RoutedEventArgs e) { CurvePoint selectedPoint = graph.SelectedPoint; switch ((sender as RadioButton).Name) { case "btnLinear": selectedPoint.InterpMode = CurveMode.CIM_Linear; break; case "btnAuto": selectedPoint.InterpMode = CurveMode.CIM_CurveAuto; break; case "btnConstant": selectedPoint.InterpMode = CurveMode.CIM_Constant; break; case "btnUser": selectedPoint.InterpMode = CurveMode.CIM_CurveUser; break; case "btnBreak": selectedPoint.InterpMode = CurveMode.CIM_CurveBreak; break; case "btnClamped": selectedPoint.InterpMode = CurveMode.CIM_CurveAutoClamped; break; default: break; } graph.Paint(); graph.SelectedPoint = selectedPoint; }
public void AddPoint(CurvePoint newPoint, LinkedListNode<CurvePoint> relTo, bool before = true) { newPoint.PropertyChanged += Point_PropertyChanged; LinkedListNode<CurvePoint> addedNode; if (relTo == null) { addedNode = CurvePoints.AddFirst(newPoint); } else if (before) { addedNode = CurvePoints.AddBefore(relTo, newPoint); } else { addedNode = CurvePoints.AddAfter(relTo, newPoint); } ListModified?.Invoke(this, new Tuple<bool, int>(true, CurvePoints.IndexOf(addedNode))); SaveChanges?.Invoke(); }
public void ImportCurvesFromXLS() { var wdlg = MessageBox.Show("Do you want to import a new curve from Excel and overwrite the existing curve values?\n \nThe sheet must be in the correct format:\n- Headers must match the overwritten curves\n- All cells must contain a value\n- Time values must be ordered.\n- Values only, no links or formulas", "Import Curves", MessageBoxButton.OKCancel); if (wdlg == MessageBoxResult.Cancel) { return; } var curveList = new List <string>(); //List of headers foreach (var otrack in InterpCurveTracks) { foreach (var ocurve in otrack.Curves) { curveList.Add(ocurve.Name); } } OpenFileDialog oDlg = new OpenFileDialog //Load Excel { Filter = "Excel Files (*.xlsx)|*.xlsx", Title = "Import Excel table" }; if (oDlg.ShowDialog() != true) { return; } var Workbook = new XLWorkbook(oDlg.FileName); IXLWorksheet iWorksheet; if (Workbook.Worksheets.Count() > 1) { try { iWorksheet = Workbook.Worksheet(1); } catch { MessageBox.Show("Curve Sheet not found"); return; } } else { iWorksheet = Workbook.Worksheet(1); } try { var xlrowCount = iWorksheet.RowsUsed().Count(); //Check headers for (int hdr = 0; hdr < curveList.Count; hdr++) //skip time (first) column { var expected = curveList[hdr]; var returned = (string)iWorksheet.Cell(1, hdr + 2).Value; //+2 as XL starts at 1, and skip time column if (expected != returned) { MessageBox.Show("The imported column headers do not match.\nPlease check import sheet. Aborting.", "Import Curves", MessageBoxButton.OK); return; } } //Check time is in order float previoustime = -9999; for (int row = 2; row <= xlrowCount; row++) { var t = iWorksheet.Cell(row, 1).Value.ToString(); if (!float.TryParse(t, out float time) || time < previoustime) { MessageBox.Show("The imported timings are not in order.\nPlease check import sheet. Aborting.", "Import Curves", MessageBoxButton.OK); return; } previoustime = time; } //CHECK Every cell has a numeric value foreach (var cell in iWorksheet.RangeUsed().Cells()) { if (cell.IsNull() || cell.IsEmpty()) { MessageBox.Show("The sheet contains empty cells.\nPlease check import sheet. Aborting.", "Import Curves", MessageBoxButton.OK); return; } if (cell.Address.RowNumber > 1 && !float.TryParse(cell.Value.ToString(), out float f)) { MessageBox.Show("The values contain text.\nPlease check import sheet. Aborting.", "Import Curves", MessageBoxButton.OK); return; } } //Import data to curves foreach (var track in InterpCurveTracks) { foreach (var curve in track.Curves) { curve.CurvePoints.Clear(); string cname = curve.Name; int xlcolumn = curveList.IndexOf(cname) + 2; //Find correct column offset as XL starts at 1, skip first column (time) for (int xlrow = 2; xlrow <= xlrowCount; xlrow++) //Get Excel points start at 2 because top contains headers { var time = iWorksheet.Cell(xlrow, 1).Value.ToString(); var outval = iWorksheet.Cell(xlrow, xlcolumn).Value.ToString(); if (outval != null && float.TryParse(time, out float t) && float.TryParse(outval, out float v)) { CurvePoint point = new CurvePoint(t, v, 0, 0, CurveMode.CIM_CurveAuto); curve.CurvePoints.AddLast(point); } else { MessageBox.Show("Data error. Aborted"); return; } } } Commit(); } MessageBox.Show("Import complete.", "Import Curves"); } catch (Exception e) { MessageBox.Show("Import failed. Check Import data.\n", "Error"); #if DEBUG MessageBox.Show($"{e.FlattenException()}", "Error"); #endif } }