首页 > 解决方案 > 如何使用 C# 将过滤后的数据从 Autodesk Revit 导出到 MS-Access?

问题描述

我需要使用 C# 编程将一些族类型的参数从 Revit 文档导出到 ms-access。

我用谷歌搜索并阅读了几页,并观看了几个视频。然后我写了下面的代码。但它不起作用。(我还检查了所有设置以连接 Revit API)有人可以帮我吗?

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using System.Data.OleDb;

namespace MyRevitCommands
{
    [TransactionAttribute(TransactionMode.ReadOnly)]
    public class    GetWindows : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements, params string[] parameters)
        {
            //Get Document
            Document oDoc = commandData.Application.ActiveUIDocument.Document;

            //Get UIDocument
            UIDocument uidoc = new UIDocument(oDoc);

            //Create Filtered Element Collector
            FilteredElementCollector WinCollector = new FilteredElementCollector(oDoc).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Windows);

            //Create Filter
            ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_Windows);

            IList<Element> windows = WinCollector.WherePasses(filter).WhereElementIsNotElementType().ToElements();

            TaskDialog.Show("Windows", string.Format("{0} windows counted!", windows.Count));

            private Document oDoc;

        // parameters for database connection
        private OleDbConnection myAccessConn;
        private string connectionString;
        private OleDbDataAdapter Adapter;
        public DataSet myDataset = new DataSet();
        private ModelItemCollection MySearchResult = new ModelItemCollection();

        {
            oDoc = Document;
            MySearchResult = oDoc.CurrentSelection.SelectedItems;
            DataTable Mydatatable = new DataTable();
            DataRow MyDataRow;
            try
            {
                connectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;" + ("Data Source=" + "D:\\Data.accdb"));
                myAccessConn = new OleDbConnection(connectionString);
                myAccessConn.Open();
                Adapter = new OleDbDataAdapter("SELECT * FROM Windows;", myAccessConn);
                Adapter.Fill(myDataset, "Windows");
                Mydatatable = myDataset.Tables["Windows"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString);
            }

            int counter = 0;
            string str;
            DataProperty classproperty;
            Mydatatable.Clear();
            foreach (Element e in WinCollector)
            {
                counter=counter+1
                MyDataRow = Mydatatable.NewRow;
            }
            return Result.Succeeded;
        }
        }
    }
}

标签: c#ms-accessrevit-api

解决方案


我猜你把数据放在这个 foreach 里面

foreach (Element e in WinCollector)
{
    //Get the value from the parameter
    string paramValue = e.LookupParameter("parameter_name").AsValueString();

    counter=counter+1
    MyDataRow = Mydatatable.NewRow;

    //Set the value in the data table
    MyDataRow["some_col"] = paramValue;
}

有关 revit API 的更多信息,请查看此链接


推荐阅读