private void buttonExport_Click(object sender, RoutedEventArgs e) { try { typeProperties.Clear(); var selectedCategories = from cat in categories where cat.IsSelected select cat; if (selectedCategories.Count() > 0) { List <CategoryInfo> categoryList = selectedCategories.ToList(); string modelName = m_doc.Title; foreach (CategoryInfo catInfo in categoryList) { var elementTypeFound = from eType in elementTypes where null != eType.Category && eType.Category.Id == catInfo.CategoryId select eType; if (elementTypeFound.Count() > 0) { foreach (ElementType eType in elementTypeFound) { string familyName = ""; familyName = eType.FamilyName; FamilyTypeProperties ftp = new FamilyTypeProperties(modelName, eType.Id.IntegerValue, familyName, eType.Name); ftp.SetCurrentFamily(eType); typeProperties.Add(ftp); } } } if (typeProperties.Count > 0) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Title = "Save a CSV File"; saveFileDialog.DefaultExt = ".csv"; saveFileDialog.Filter = "Comma Separated Values (.csv)|*.csv"; if ((bool)saveFileDialog.ShowDialog()) { fileName = saveFileDialog.FileName; if (WriteCSV(fileName, typeProperties)) { MessageBox.Show("File successfully saved in \n" + fileName, "File Saved", MessageBoxButton.OK, MessageBoxImage.Information); this.DialogResult = true; } } } } } catch (Exception ex) { MessageBox.Show("Failed to export family and type names.\n" + ex.Message, "Export Names", MessageBoxButton.OK, MessageBoxImage.Warning); } }
private void ReadCSV(string filePath) { try { modelNames.Clear(); categoryNames.Clear(); typeProperties.Clear(); if (File.Exists(filePath)) { using (TextFieldParser parser = new TextFieldParser(filePath)) { parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(","); bool firstRow = true; bool formatMismatched = false; while (!parser.EndOfData) { string[] fields = parser.ReadFields(); if (firstRow) { for (int i = 0; i < columnNames.Length; i++) { if (fields[i] != columnNames[i]) { formatMismatched = true; break; } } if (formatMismatched) { MessageBox.Show("Column names should be the following orders.\n[" + columnNames[0] + "], [" + columnNames[1] + "], [" + columnNames[2] + "], [" + columnNames[3] + "]", "Column Names", MessageBoxButton.OK, MessageBoxImage.Warning); } firstRow = false; } else { string modelName = fields[0]; int typeId = -1; int.TryParse(fields[1], out typeId); string familyName = fields[2]; string typeName = fields[3]; FamilyTypeProperties ftp = new FamilyTypeProperties(modelName, typeId, familyName, typeName); var symbolFound = from elementType in elementTypes where elementType.Id.IntegerValue == typeId select elementType; if (symbolFound.Count() > 0) { ftp.SetCurrentFamily(symbolFound.First()); this.TypeProperties.Add(ftp); } } } } if (typeProperties.Count > 0) { var modelNameFound = from ftp in typeProperties select ftp.ModelName; if (modelNameFound.Count() > 0) { this.ModelNames = new ObservableCollection <string>(modelNameFound.Distinct().OrderBy(o => o).ToList()); } var categoryNameFound = from ftp in TypeProperties select ftp.CategoryName; if (categoryNameFound.Count() > 0) { this.CategoryNames = new ObservableCollection <string>(categoryNameFound.Distinct().OrderBy(o => o).ToList()); } this.SelectedModelIndex = 0; this.SelectedCategoryIndex = 0; } } } catch (Exception ex) { MessageBox.Show("Failed to read the csv file.\n" + ex.Message, "Read CSV File", MessageBoxButton.OK, MessageBoxImage.Warning); } }