简体   繁体   中英

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

I need to export some parameters of a family type from a Revit document to ms-access using C# programming.

I googled and read several pages, and I watched several videos. Then I wrote the following code. But it doesn't work. (I also checked all settings to connect of Revit API) Could anyone help me, please?

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

I guess you put the data in inside this 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;
}

For more information about the revit API check this link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM