Data volume of components in revit is large. Database is needed when doing standardization or audit software. Importing data into database enables calculation to verify separated from software, which is also a kind of burden reduction for alternative software.

demo

  1. Get component - Test project casually select several components to output data

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    UIDocument uidoc = commandData.Application.ActiveUIDocument;
    Document doc = uidoc.Document;
    var refer = uidoc.Selection.PickObjects(ObjectType.Element, "pick onjects");
    List<string> categorys = new List<string>();
    List<int> elementIds = new List<int>();
    List<string> symbolNames = new List<string>();
    foreach(Reference re in refer)
    {
    Element e = doc.GetElement(re);
    string symbolName = e.Name;
    string category = e.Category.Name;
    int elementId = e.Id.IntegerValue;
    symbolNames.Add(symbolName);
    categorys.Add(category);
    elementIds.Add(elementId);

    }
  2. Connect to Database

  • Because it is a test, mysql connection is set to local connection. This step should be creating Mysql file to transfer to server database for reading
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public MySQLConnection2(List<int> elementids,List<string> symbolNames,List<string> categorys)
{
MySqlConnection conn;
string myConnectionString;
myConnectionString = "server=localhost;uid=root;password=*****;port=3306;database=test01";
try
{
conn = new MySqlConnection();
conn.ConnectionString = myConnectionString;
conn.Open();
string command = SetData("revitDatas", elementids, symbolNames, categorys);
MySqlCommand cmd = new MySqlCommand(command,conn);
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
TaskDialog.Show("MysqlError",ex.Message);
}
}
  1. Display in form. Frontend code is relatively simple, just pass data stream in
1
2
3
4
5
6
7
8
9
10
11
12
13

string myConnectionString;
myConnectionString = "server=localhost;uid=root;password=*****;port=3306;database=test01";
MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);
conn.Open();
string cmd = "SELECT * FROM revitDatas";
MySql.Data.MySqlClient.MySqlCommand mscmd = new MySql.Data.MySqlClient.MySqlCommand(cmd, conn);
MySql.Data.MySqlClient.MySqlDataAdapter adpter = new MySql.Data.MySqlClient.MySqlDataAdapter(mscmd);
DataTable table = new DataTable();
adpter.Fill(table);
dataGridView1.DataSource = table;
conn.Close();
conn.Dispose();

Image Description
Up to this step data can be extracted and utilized, but empty rows or empty columns will appear in datagrid of frontend. Need to set parameters
dataGridView1.AllowUserToAddRows = false; change value to false. Reference link: https://blog.csdn.net/wanglejun/article/details/39231075
Image Description