<Revit二次开发>把wall参数导入到Excel表格中

#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;
        }
    }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值