- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have written a code for importing parameter value for pipe project parameter.The datas are reading from excel but when i run the file data is empty in revit Please let me if there is anything i missed up in the code
using System;
using System.Collections.Generic;
using System.IO;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using OfficeOpenXml; // Ensure EPPlus namespace is used
namespace RevitExcelAddIn
{
public class UpdatePipeParametersCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
Document doc = commandData.Application.ActiveUIDocument.Document;
string filePath = @"C:\Users\xxxxx\xxx)\Pipe_Schedule1.xlsx";
var familyData = ReadExcelFile(filePath);
foreach (var data in familyData)
{
try
{
UpdateParametersInRevit(doc, data);
}
catch (Exception ex)
{
message = $"An error occurred while updating parameters: {ex.Message}";
return Result.Failed;
}
}
return Result.Succeeded;
}
static List<FamilyData> ReadExcelFile(string filePath)
{
var data = new List<FamilyData>();
FileInfo fileInfo = new FileInfo(filePath);
using (var package = new ExcelPackage(fileInfo))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
for (int row = 2; row <= worksheet.Dimension.End.Row; row++)
{
var familyType = worksheet.Cells[row, 1].Text;
var familyName = worksheet.Cells[row, 2].Text;
var diameter = worksheet.Cells[row, 3].Text;
var parameters = new Dictionary<string, string>
{
{ "ComponentGroup", worksheet.Cells[row, 4].Text },
{ "ComponentGroupDescription", worksheet.Cells[row, 5].Text },
// Add remaining parameters
};
data.Add(new FamilyData
{
FamilyType = familyType,
FamilyName = familyName,
Diameter = diameter,
Parameters = parameters
});
}
}
return data;
}
static void UpdateParametersInRevit(Document doc, FamilyData data)
{
var collector = new FilteredElementCollector(doc)
.OfCategory(BuiltInCategory.OST_PipeCurves)
.WhereElementIsNotElementType();
foreach (FamilyInstance instance in collector)
{
if (instance.Symbol.Family.Name == data.FamilyName && instance.Symbol.Name == data.FamilyType)
{
using (Transaction trans = new Transaction(doc, "Update Pipe Parameters"))
{
trans.Start();
foreach (var kvp in data.Parameters)
{
Parameter param = instance.LookupParameter(kvp.Key);
if (param != null)
{
try
{
if (param.StorageType == StorageType.String)
{
param.Set(kvp.Value);
}
else if (param.StorageType == StorageType.Double)
{
if (double.TryParse(kvp.Value, out double doubleValue))
{
param.Set(doubleValue);
}
else
{
throw new FormatException($"Cannot convert '{kvp.Value}' to Double.");
}
}
else if (param.StorageType == StorageType.Integer)
{
if (int.TryParse(kvp.Value, out int intValue))
{
param.Set(intValue);
}
else
{
throw new FormatException($"Cannot convert '{kvp.Value}' to Integer.");
}
}
else
{
throw new NotSupportedException($"Parameter storage type '{param.StorageType}' is not supported.");
}
}
catch (Exception ex)
{
TaskDialog.Show("Parameter Update Error", $"Error updating parameter '{kvp.Key}': {ex.Message}");
}
}
}
trans.Commit();
}
}
}
}
}
class FamilyData
{
public string FamilyType { get; set; }
public string FamilyName { get; set; }
public string Diameter { get; set; }
public Dictionary<string, string> Parameters { get; set; }
}
}
Solved! Go to Solution.