簡體   English   中英

如何使用C#將篩選的數據從Autodesk Revit導出到MS-Access?

[英]How to export filtered data from Autodesk Revit to MS-Access using C#?

我需要使用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;
        }
        }
    }
}

我想你把數據放在這個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的更多信息,請單擊此鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM