// Opening an Excel template private void buttonOpenExcelTemplate_Click(object sender, EventArgs e) { clearStatus(); OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx"; openFileDialog.FilterIndex = 1; openFileDialog.RestoreDirectory = true; DialogResult dialogResult = openFileDialog.ShowDialog(); if (dialogResult == DialogResult.OK) { string filePath = openFileDialog.FileName; excelTemplate_ = new ExcelObject(filePath); if (excelTemplate_.isOpened()) { textBoxExcelTemplatePath.Text = filePath; updateExcelTemplateList(); setProjectEnabled(); setStatus("The Excel template file was successfully opened"); } } else if (dialogResult != DialogResult.Cancel) { setStatus("An error occured while choosing the template Excel file"); return; } else { return; } }
private void Copy(ExcelObject another) { package_ = another.package_; charts_ = another.charts_; chartSheets_ = another.chartSheets_; path_ = another.path_; // Markers indMarkers_ = another.indMarkers_; standardMarkers_ = another.standardMarkers_; customMarkers_ = another.customMarkers_; // Lines indLines_ = another.indLines_; standardLines_ = another.standardLines_; customLines_ = another.customLines_; }
public ExcelObject(ExcelObject template, string path, string name) { const string extension = ".xlsx"; string originalPath = template.path_; string resPath = path + "\\" + name + extension; // Looking for an available name if a template equals a resulting file if (Path.GetFileNameWithoutExtension(originalPath).Equals(name)) { bool isAvailable = false; while (!isAvailable) { name += "-copy"; resPath = path + "\\" + name + extension; isAvailable = !(new FileInfo(name).Exists); } } // Try creating an Excel instance createExcelInstance(originalPath, resPath, name, extension); Copy(new ExcelObject(resPath)); // Delete the data worksheet if existed ExcelWorkbook book = package_.Workbook; foreach (ExcelWorksheet sheet in book.Worksheets) { if (sheet.Name.Equals(workSheetName_)) { book.Worksheets.Delete(workSheetName_); break; } } // Add a new one workSheet_ = book.Worksheets.Add(workSheetName_); posCharts_ = new Dictionary <ExcelDrawing, ChartPosition>(); path_ = resPath; // Clear all the series foreach (ExcelDrawing objChart in charts_) { ExcelScatterChart chart = (ExcelScatterChart)objChart; while (chart.Series.Count > 0) { chart.Series.Delete(0); } } }
private void testExcel() { string templateDir = @"..\..\..\templates\"; string path = Path.GetFullPath(templateDir + "T50.xlsx"); excelTemplate_ = new ExcelObject(path); textBoxExcelTemplatePath.Text = path; textBoxDirectoryExcel.Text = @"C:\Users\qinterfly\Desktop"; textBoxNameExcel.Text = "TestMe"; singleFrequencyIndices_ = new List <int>(); multiFrequency_ = new Dictionary <string, double[]>(); multiFrequencyIndices_ = new Dictionary <string, List <int> >(); multiResonanceFrequencyIndices_ = new Dictionary <string, int>(); mapResponses_ = new Dictionary <string, string>(); updateExcelTemplateList(); setProjectEnabled(); charts_.read(templateDir + "Su-34.rep", modelRenderer_.containesNode, selectionDelimiter_, ref indLine_); listBoxTemplateCharts_SelectedIndexChanged(); }
// Processing all the data private void buttonProcess_Click(object sender = null, EventArgs e = null) { string templateName = textBoxNameExcel.Text; string templateDirectory = textBoxDirectoryExcel.Text; if (String.IsNullOrEmpty(templateName)) { MessageBox.Show("Filename to save is not specified", "Empty filename", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (String.IsNullOrEmpty(templateDirectory)) { MessageBox.Show("Directory to save is not specified", "Empty directory name", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } ExcelObject excelResult = new ExcelObject(excelTemplate_, templateDirectory, templateName); // Checking the project, template and selected signals if (!project.isProjectOpened() || !excelTemplate_.isOpened()) { return; } // Resolving dependencies resolveDependencies(); // Retrieve selected frequencies int nSingleFrequency = singleFrequencyIndices_.Count; ChartPosition.lastRow = 0; // Error handling errorMessage_ = null; int iError = 0; // Create a map to convert directions into labels Dictionary <ChartDirection, string> mapDirections = new Dictionary <ChartDirection, string>(); mapDirections[ChartDirection.X] = "X"; mapDirections[ChartDirection.Y] = "Y"; mapDirections[ChartDirection.Z] = "Z"; // Creating series double signData = 1.0; string inversionInfo = "No"; if (checkBoxInverseResults.Checked) { signData = -1.0; inversionInfo = "Yes"; } // Creating the header excelResult.addHeaderInfo($"Date: {DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")}"); excelResult.addHeaderInfo($"Inversion: {inversionInfo}"); excelResult.addHeaderInfo($"Template: {templateDirectory + "\\" + templateName + ".xlsx"}"); ChartPosition.lastRow += 1; foreach (string chart in listBoxTemplateCharts.Items) { // Nodes selection List <ISelection> selectedObjects = charts_.selection[chart]; // Type and direction ChartTypes type = charts_.type[chart]; ChartDirection direction = charts_.direction[chart]; SignalUnits units = charts_.units[chart]; // Error handling if (type == ChartTypes.UNKNOWN || selectedObjects.Count == 0) { continue; } if (direction == ChartDirection.UNKNOWN) { throwError(ref iError, "The direction for '" + chart + "' is not specified"); continue; } if (units == SignalUnits.UNKNOWN) { throwError(ref iError, "The units for '" + chart + "' are not specified"); continue; } // Norm double norm = charts_.normalization[chart]; ChartDirection axis = charts_.axis[chart]; int indX = 0, indY = 1; if (charts_.swapAxes[chart]) { indX = 1; indY = 0; } string infoUnits = comboBoxTemplateUnits.Items[(int)units].ToString(); // Frequency response function: real and imaginary parts if ((type == ChartTypes.REAL_FRF || type == ChartTypes.IMAG_FRF) && project.signals_.Count != 0) { List <string> chartNodes = new List <string>(); foreach (ISelection item in selectedObjects) { chartNodes.Add((string)item.retrieveSelection()); } int iType = (int)type - 1; foreach (string node in chartNodes) // Node { // Check if there is an appropriate signal if (!project.signals_.ContainsKey(node) || !project.signals_[node].ContainsKey(direction)) { throwError(ref iError, "The chosen signals do not contain the node '" + node + "'"); continue; } Response response = project.signals_[node][direction][0]; // Slice data by the selected index if (!response.data.ContainsKey(units) || response.data[units] == null) { continue; } double[,] refFullData = response.data[units]; double[,] data = new double[nSingleFrequency, 2]; int iSelected; for (int i = 0; i != nSingleFrequency; ++i) { iSelected = singleFrequencyIndices_[i]; data[i, indX] = response.frequency[iSelected]; data[i, indY] = refFullData[iSelected, iType] * signData; } string ptrNode = "т. " + node.Split(selectionDelimiter_)[1]; string info = $" - {mapDirections[direction]} ({infoUnits})"; excelResult.addSeries(chart, data, ptrNode, info); } } // Modeset else if (type == ChartTypes.MODESET && project.signals_.Count != 0) { if (axis == ChartDirection.UNKNOWN) { throwError(ref iError, "The coordinate axis for '" + chart + "' is not specified"); continue; } if (indexSingleResonanceFrequency_ < 0) { throwError(ref iError, "The resonance frequency is not chosen"); continue; } // For each line foreach (ISelection item in selectedObjects) { Lines currentLine = (Lines)item; string nameLine = currentLine.lineName_; List <string> lineNodes = (List <string>)currentLine.retrieveSelection(); List <double> coordinates = new List <double>(); List <double> values = new List <double>(); double resonanceFrequency = -1.0; foreach (string node in lineNodes) { // Check if there is an appropriate signal if (!project.signals_.ContainsKey(node) || !project.signals_[node].ContainsKey(direction)) { throwError(ref iError, "The chosen signals do not contain the node '" + node + "'"); continue; } // Retreiving the coordinate along the choosen axis string[] selectionInfo = node.Split(selectionDelimiter_); uint indNode = modelRenderer_.componentSet_.mapNodeNames[selectionInfo[0]][selectionInfo[1]]; double[,] componentCoordinates = (double[, ])modelRenderer_.componentSet_.nodeCoordinates[selectionInfo[0]]; int tInd = (int)axis - 1; coordinates.Add(componentCoordinates[indNode, tInd]); // Retrieving the function value Response response = project.signals_[node][direction][0]; if (!response.data.ContainsKey(units) || response.data[units] == null) { continue; } double[,] refFullData = response.data[units]; values.Add(refFullData[indexSingleResonanceFrequency_, 1]); // Imaginary part of the signal if (resonanceFrequency < 0) { resonanceFrequency = response.frequency[indexSingleResonanceFrequency_]; } } if (values.Count > 0) { int nNodes = coordinates.Count; double[,] data = new double[nNodes, 2]; for (int i = 0; i != nNodes; ++i) { data[i, indX] = coordinates[i] / norm; data[i, indY] = values[i] * signData; } string frequencyInfo = null; if (resonanceFrequency > 0) { frequencyInfo = $" - {resonanceFrequency.ToString("F3", CultureInfo.InvariantCulture)} Гц ({infoUnits})"; } excelResult.addSeries(chart, data, nameLine, frequencyInfo); } } } // Multi-FRF else if ((type == ChartTypes.MULTI_REAL_FRF || type == ChartTypes.MULTI_IMAG_FRF) && project.multiSignals_.Count != 0) { List <string> chartNodes = new List <string>(); int iType = 0; if (type == ChartTypes.MULTI_IMAG_FRF) { iType = 1; } foreach (ISelection item in selectedObjects) { chartNodes.Add((string)item.retrieveSelection()); } foreach (string node in chartNodes) // Node { // Check if there is an appropriate signal if (!project.multiSignals_.ContainsKey(node) || !project.multiSignals_[node].ContainsKey(direction)) { throwError(ref iError, "The chosen signals do not contain the node '" + node + "'"); continue; } var dirNodeSignals = project.multiSignals_[node][direction]; foreach (Response response in dirNodeSignals) { // Slice data by the selected index if (!response.data.ContainsKey(units) || response.data[units] == null) { continue; } double[,] refFullData = response.data[units]; List <int> indices = multiFrequencyIndices_[mapResponses_[response.path]]; int nIndices = indices.Count; double[,] data = new double[nIndices, 2]; int iSelected; for (int i = 0; i != nIndices; ++i) { iSelected = indices[i]; data[i, indX] = response.frequency[iSelected]; data[i, indY] = refFullData[iSelected, iType] * signData; } // Retrieving force value if (data.GetLength(0) > 0) { string force = $"𝐹 = {getForceValue(response.path)} Н"; string infoChart = $" - {mapDirections[direction]} ({infoUnits})"; string infoNode = $"{node}"; excelResult.addSeries(chart, data, force, infoChart, infoNode); } } } } // Frequency function else if ((type == ChartTypes.REAL_FREQUENCY || type == ChartTypes.IMAG_FREQUENCY) && project.multiSignals_.Count != 0) { List <string> chartNodes = new List <string>(); foreach (ISelection item in selectedObjects) { chartNodes.Add((string)item.retrieveSelection()); } foreach (string node in chartNodes) // Node { // Check if there is an appropriate signal if (!project.multiSignals_.ContainsKey(node) || !project.multiSignals_[node].ContainsKey(direction)) { throwError(ref iError, "The chosen signals do not contain the node '" + node + "'"); continue; } var dirNodeSignals = project.multiSignals_[node][direction]; double[,] data = new double[dirNodeSignals.Count, 2]; int k = 0; foreach (Response response in dirNodeSignals) { if (!response.data.ContainsKey(units) || response.data[units] == null) { continue; } string label = mapResponses_[response.path]; int indexMultiResonance = multiResonanceFrequencyIndices_[label]; double resonanceFrequency = multiFrequency_[label][indexMultiResonance]; Tuple <double, double> pair = response.evaluateResonanceFrequency(type, units, resonanceFrequency); if (pair != null) { data[k, indX] = pair.Item1; data[k, indY] = pair.Item2; } else { double realPart = response.data[units][indexMultiResonance, 0]; double imagPart = response.data[units][indexMultiResonance, 1]; data[k, indX] = resonanceFrequency; data[k, indY] = Math.Sqrt(Math.Pow(realPart, 2.0) + Math.Pow(imagPart, 2.0)); } ++k; } if (data.GetLength(0) > 0) { string ptrNode = "т. " + node.Split(selectionDelimiter_)[1]; string infoChart = $" - {mapDirections[direction]} ({infoUnits})"; excelResult.addSeries(chart, data, ptrNode, infoChart); } } } } ChartPosition.lastRow = 0; // Saving and opening the results excelResult.save(); excelResult.open(); if (iError == 0) { setStatus("The results were successfully processed"); } else { showErrors(iError); } }