Revit二次开发 将构件信息导入到数据库中

revit中构件数据量较大,在做标准化或者审核软件的时候需要用到数据库,将数据导入数据库使得计算能够脱离软件进行计算也算是一种另类的软件减负

demo

  1. 获取构件-测试项目随便框选几个构件进行输出数据

    UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            var refer = uidoc.Selection.PickObjects(ObjectType.Element, "pick onjects");
            List categorys = new List();
            List elementIds = new List();
            List symbolNames = new List();
            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. 连接数据库

  • 因为是测试所以将mysql连接设置为本地连接,这一步应该是创建Mysql文件传递到服务器数据库中进行读取
 public MySQLConnection2(List elementids,List symbolNames,List 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. 在form中显示 前端代码相对简单将数据流传入即可

            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();

Revit二次开发 将构件信息导入到数据库中_第1张图片
到这一步便可以将数据提取进行利用,但是前端这块datagrid会出现空行或者空列,需要设置一下参数
dataGridView1.AllowUserToAddRows = false;将值改为false即可。参考链接:https://blog.csdn.net/wanglejun/article/details/39231075
Revit二次开发 将构件信息导入到数据库中_第2张图片

你可能感兴趣的:(MySQL,Revit二次开发)