private List <MaterialFromExcel> ReadExcel(string filename) { List <MaterialFromExcel> materialsInExcel = new List <MaterialFromExcel>(); try{ Excel.ApplicationClass excel = new Excel.ApplicationClass(); excel.Visible = false; Excel.Workbook workbook = excel.Workbooks.Open(filename, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets.get_Item(1); Excel.Range range = worksheet.UsedRange; int indexRow = 1; while ((string)(range.Cells[indexRow, 1] as Excel.Range).Value2 != "FF&E Number") { indexRow++; } indexRow++; while (null != (range.Cells[indexRow, 1] as Excel.Range).Value2) { MaterialFromExcel material = new MaterialFromExcel(); if (null != (range.Cells[indexRow, 1] as Excel.Range)) { material.FFENumber = (string)(range.Cells[indexRow, 1] as Excel.Range).Value2; } if (null != (range.Cells[indexRow, 2] as Excel.Range)) { material.ReferenceValue = (string)(range.Cells[indexRow, 2] as Excel.Range).Value2; } if (null != (range.Cells[indexRow, 3] as Excel.Range)) { material.NameValue = (string)(range.Cells[indexRow, 3] as Excel.Range).Value2; } if (null != (range.Cells[indexRow, 4] as Excel.Range)) { material.SubGroup = (string)(range.Cells[indexRow, 4] as Excel.Range).Value2; } materialsInExcel.Add(material); indexRow++; } workbook.Close(false, null, null); excel.Quit(); } catch (Exception ex) { MessageBox.Show(ex.Message); } return(materialsInExcel); }
private bool CompareMaterialSets() { bool compared = false; try{ for (int i = materialsFromExcel.Count - 1; i > -1; i--) { MaterialFromExcel matExcel = materialsFromExcel[i]; int index = revitMaterials.FindIndex(x => x.MarkValue == matExcel.ReferenceValue); if (index > -1) { RevitMaterial rMaterial = revitMaterials[index]; rMaterial.MatchedMaterial = matExcel; if (rMaterial.MaterialName == matExcel.ReferenceValue && rMaterial.Description == matExcel.NameValue && rMaterial.KeynoteValue == matExcel.SubGroup) { rMaterial.MatchValue = MatchStatus.Matched; } else { rMaterial.MatchValue = MatchStatus.ToBeUpdated; } revitMaterials.RemoveAt(index); revitMaterials.Add(rMaterial); matExcel.MatchedMaterial = rMaterial; matExcel.MatchValue = MatchStatus.Matched; materialsFromExcel.RemoveAt(i); materialsFromExcel.Add(matExcel); } } compared = true; } catch (Exception ex) { compared = false; MessageBox.Show("Failed to compare materials sets between Excel and Revit project.\n" + ex.Message, "Compare Material Sets", MessageBoxButtons.OK, MessageBoxIcon.Warning); } return(compared); }
private bool UpdateMaterials(List <RevitMaterial> materialsToUpdate, out List <ElementId> updatedMaterialIds) { bool updated; updatedMaterialIds = new List <ElementId>(); try{ using (TransactionGroup tg = new TransactionGroup(m_doc)) { tg.Start("Update Materials"); foreach (RevitMaterial rm in materialsToUpdate) { using (Transaction trans = new Transaction(m_doc)) { trans.Start("Update Material"); try{ Material material = m_doc.GetElement(rm.MaterialId) as Material; if (null != material) { MaterialFromExcel mExcel = rm.MatchedMaterial; //update name if (rm.MaterialName != mExcel.ReferenceValue) { if (Material.IsNameUnique(m_doc, mExcel.ReferenceValue)) { //material.Name = mExcel.NameValue; material.Name = mExcel.ReferenceValue; } } //update description if (rm.Description != mExcel.NameValue) { Parameter param = material.LookupParameter("Description"); if (null != param) { param.Set(mExcel.NameValue); } } //update keynote if (rm.KeynoteValue != mExcel.SubGroup) { Parameter param = material.LookupParameter("Keynote"); if (null != param) { param.Set(mExcel.SubGroup); } } updatedMaterialIds.Add(material.Id); } trans.Commit(); } catch (Exception ex) { string message = ex.Message; trans.RollBack(); } } } tg.Assimilate(); } updated = true; } catch (Exception ex) { string message = ex.Message; updated = false; } return(updated); }