#region Namespaces
using System;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Runtime.Remoting.Contexts;
#endregion
namespace GetWallsToExcel
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
Document doc = uidoc.Document;
SelElementSet selection = uidoc.Selection.Elements;
string info = "Selected element:\n";
StringBuilder sb1 = new StringBuilder();
//导入Excel表格
Worksheet ws = null;
Microsoft.Office.Interop.Excel.Application exl = new Microsoft.Office.Interop.Excel.ApplicationClass();
object missingValue = Type.Missing;
Workbook workbook = exl.Workbooks.Open("C:/Users/huangqx.DFD/Documents/1.xlsx",
missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue,
missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
if (selection.Size == 1)
{
foreach (Element elem in selection)
{
info += elem.Name + "\n";
}
ElementSetIterator it = selection.ForwardIterator();
it.MoveNext();
Element element = it.Current as Element;
List<string> parameteritems = new List<string>();
ParameterSet parammeter = element.Parameters;
foreach (Autodesk.Revit.DB.Parameter param in parammeter)
{
if (param == null) continue;
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}:\t", param.Definition.Name);
switch (param.StorageType)
{
case StorageType.Double:
sb.AppendFormat("\t{0}(double)", param.AsValueString());
break;
case StorageType.ElementId:
ElementId elemId = new ElementId(param.AsElementId().IntegerValue);
Element elem = doc.get_Element(elemId);
sb.Append(elem != null ? elem.Name : "Not set");
break;
case StorageType.Integer:
sb.AppendFormat("\t{0}(int)", param.AsDouble().ToString());
break;
case StorageType.String:
sb.AppendFormat("\t{0}(string)", param.AsString());
break;
case StorageType.None:
sb.AppendFormat("\t{0}(none)", param.AsString());
break;
default:
break;
}
parameteritems.Add(sb.ToString());
}
exl.Visible = true;
int n = 1;
foreach (string para in parameteritems.ToArray())
{
ws = (Worksheet)workbook.Worksheets.get_Item(1);
ws.Cells[n, 1] = para;
n++;
}
}
workbook.Save();
workbook.Close(null, null, null);
exl.Workbooks.Close();
exl.Application.Quit();
exl.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(exl);
workbook = null;
ws = null;
exl = null;
return Result.Succeeded;
}
}
}
<Revit二次开发>把wall参数导入到Excel表格中
最新推荐文章于 2025-01-23 11:45:54 发布