revit 二次开发 可停靠窗体winform控件

创建一个可停靠窗体,同时wpf上承载的是winform控件

1.类中代码


        [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
        [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
        [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
        public class RevitdingeForm : IExternalCommand
        {
            Document m_doc;
            public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
            {
                UIApplication uiApp = commandData.Application;
                Autodesk.Revit.ApplicationServices.Application app = uiApp.Application;
                m_doc = uiApp.ActiveUIDocument.Document;

                string m_mainPageGuid = "fa52df6a-8afe-4e71-8701-a1a0cd544ff9";

                Guid retval = Guid.Empty;
                try
                {
                    retval = new Guid(m_mainPageGuid);
                }
                catch (Exception)
                {
                }

                DockablePaneId sm_UserDockablePaneId = new DockablePaneId(retval);

                //sm_UserDockablePaneId = new DockablePaneId(retval);
                DockablePane pane = uiApp.GetDockablePane(sm_UserDockablePaneId);
                pane.Show();
                //DockablePaneId sm_UserDockablePaneId = new DockablePaneId(retval);

                //sm_UserDockablePaneId = new DockablePaneId(retval);
                //DockablePane pane = uiApp.GetDockablePane(sm_UserDockablePaneId);
                //pane.Show();
                return Result.Succeeded;
            }

            [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
            [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
            [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
            public class DockForm : IExternalApplication
            {
                static string AddInPath = typeof(DockForm).Assembly.Location;

                public Result OnShutdown(UIControlledApplication application)
                {
                    return Result.Succeeded;
                }

                public Result OnStartup(UIControlledApplication application)
                {
                    try
                    {
                        string TabName = "工程定额对话框";
                        application.CreateRibbonTab(TabName);

                        string ArchitecturePanelName = "工程定额";
                        RibbonPanel ArchitecturePanel = application.CreateRibbonPanel(TabName, ArchitecturePanelName);

                        PulldownButton SetCarporNum = ArchitecturePanel.AddItem(new PushButtonData("工程定额", "工程定额", AddInPath, "RevitDockForm.RevitdingeForm")) as PulldownButton;
                        //SetCarporNum.ToolTip = "projectDinge";

                        string m_mainPageGuid = "fa52df6a-8afe-4e71-8701-a1a0cd544ff9";
                        UserControl1 m_mainPage = new UserControl1();

                        Guid retval = Guid.Empty;
                        retval = new Guid(m_mainPageGuid);
                        DockablePaneId sm_UserDockablePanId = new DockablePaneId(retval);

                        application.RegisterDockablePane(sm_UserDockablePanId, "RevitDockForm", m_mainPage as IDockablePaneProvider);

                        return Result.Succeeded;
                    }
                    catch (Exception ex)
                    {
                        TaskDialog.Show("错误", ex.ToString());
                        return Result.Failed;
                    }
                }
            }
        }
  

2.wpf窗体代码

/// 
    /// UserControl1.xaml 的交互逻辑
    /// 
    public partial class UserControl1 : Page, IDockablePaneProvider
    {
        public UserControl1()
        {
            InitializeComponent();
        }
        public void SetupDockablePane(DockablePaneProviderData data)
        {
            data.FrameworkElement = this as FrameworkElement;
            DockablePaneProviderData d = new DockablePaneProviderData();

            data.InitialState = new DockablePaneState();
            data.InitialState.DockPosition = Autodesk.Revit.UI.DockPosition.Bottom;
        }

        WindowsFormsSet1 form = new WindowsFormsSet1();

    }

3.WindowsFormSet代码

 public partial class WindowsFormsSet1 : UserControl
    {
        public WindowsFormsSet1()
        {
            InitializeComponent();
        }

        public static string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Administrator.WQ-20160328DSWI\\Desktop\\深圳市建筑工程定额(2003).accdb";
        OleDbConnection connection = new OleDbConnection(strCon);
        OleDbDataAdapter oap;

        private void WindowsFormsSet1_Load(object sender, EventArgs e)
        {
            FillTreeView();
            FillDataGridView();
        }

        /// 
        /// 填充treeview
        /// 
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();
        private void FillTreeView()
        {
           string sql = "select 章节名称,子节点 from WBS order by 子节点";
            oap = new OleDbDataAdapter(sql, connection);
            oap.Fill(dt);
            treeView1.ItemHeight = dingedataGridView.ColumnHeadersHeight + 2;
            treeView1.Indent = 5;

            if (dt.Rows.Count == 0) return;

            int i;
            for (i = 0; i < dt.Rows.Count; i++)
            {

                string strProjectName = dt.Rows[i]["章节名称"].ToString();
                string strWBS = dt.Rows[i]["子节点"].ToString();

                TreeNode node = new TreeNode();
                node.Name = strWBS;
                node.Text = strProjectName;



                if (strWBS.Length == 2)
                {
                    treeView1.Nodes.Add(node);

                }
                else if (strWBS.Length == 5)
                {
                    foreach (TreeNode t in treeView1.Nodes)
                    {
                        string x = t.Name;
                        string y = strWBS.Substring(0, 2);
                        if (x == y)
                        {

                            treeView1.Nodes[t.Index].Nodes.Add(node);
                        }
                    }
                }
                TreeNode nd = new TreeNode();
                nd.Name = strWBS;
                nd.Text = strProjectName;
                if (strWBS.Length == 8)
                {

                    // int length = treeView1.Nodes.Count - 1;
                    //foreach (TreeNode t in treeView1.Nodes)
                    //{
                    //string x = t.Name;
                    //string y = strWBS.Substring(0, 5);
                    //if (x == y)
                    //{
                    treeView1.Nodes.Add(nd);
                    //tv.Nodes[t.Index].Nodes.Add(node);

                    //treeView1.Nodes[length].Nodes[t.Index].Nodes.Add(node);
                    //}
                    //}
                }
                else if (strWBS.Length == 11)
                    foreach (TreeNode t in treeView1.Nodes)
                    {
                        string x = t.Name;
                        string y = strWBS.Substring(0, 8);
                        if (x == y)
                        {
                            treeView1.Nodes[t.Index].Nodes.Add(nd);
                        }
                    }
            }
        }

        DataTable dtt = new DataTable();
        private void FillDataGridView()
        {
            //取消自动生成列
            dingedataGridView.AutoGenerateColumns = false;
            //取消自动生成行
            dingedataGridView.AllowUserToAddRows = false;

            //设置选中行的模式
            dingedataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //设置datagridview可以多选
            dingedataGridView.MultiSelect = true;
            //设置选中行的颜色
            dingedataGridView.DefaultCellStyle.SelectionBackColor = Color.Blue;
            dingedataGridView.DefaultCellStyle.SelectionForeColor = Color.LightBlue;

            //为datagridview添加复选框列并置于第一列
            DataGridViewCheckBoxColumn dgvCheckBoxColumn = new DataGridViewCheckBoxColumn();
            dgvCheckBoxColumn.HeaderText = "选项";
            dgvCheckBoxColumn.Name = "选项";
            dgvCheckBoxColumn.ReadOnly = false;
            dingedataGridView.Columns.Insert(0, dgvCheckBoxColumn);
            //dataGridView1.SelectedRows[i].Cells[1].Style.BackColor = Color.Blue;

            //dgvCheckBoxColumn.Selected=false;

            //选择状态解除
            //dataGridView1.ClearSelection();

            //设定交替行颜色
            dingedataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.White;
            dingedataGridView.RowsDefaultCellStyle.BackColor = Color.LightGray;

            //head文字居中   
            dingedataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            //可否手动调整行大小
            dingedataGridView.AllowUserToResizeRows = false;
            dingedataGridView.AutoGenerateColumns = true;

            //设置表格分割线的颜色
            this.dingedataGridView.GridColor = System.Drawing.Color.FromArgb(248, 161, 109);

            //取消行头
            dingedataGridView.RowHeadersVisible = false;

            //this.dataGridView1.CellValuePushed += new DataGridViewCellValueEventHandler(dataGridView1_CellValuePushed);
            // this.dataGridView1.CellValueNeeded += new DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);

            //填充数据
            string sql = "select 定额编号,WBS,定额名称,标签,标准费率 from 建筑工程定额";
            oap = new OleDbDataAdapter(sql, connection);
            oap.Fill(dtt);
            dingedataGridView.DataSource = dtt; 
        }

        private void dingedataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                if (e.RowIndex >= 0)
                {

                    if ((bool)dingedataGridView.Rows[e.RowIndex].Cells[0].EditedFormattedValue == true)
                    {
                        dingedataGridView.Rows[e.RowIndex].Cells[0].Value = true;
                        dingedataGridView.Rows[e.RowIndex].Selected = true;
                        Color c = Color.FromArgb(100, 200, 200, 200);
                        //Color c = SystemColors.Window;
                        dingedataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = c;
                    }
                    else
                    {
                        dingedataGridView.Rows[e.RowIndex].Cells[0].Value = false;
                        dingedataGridView.Rows[e.RowIndex].Cells[0].Selected = false;
                        Color c = SystemColors.Window;
                        //Color c = Color.FromArgb(100, 200, 200, 200);
                        dingedataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = c;
                    }
                }
            }
        }

        DataSet dss = new DataSet();
        DataTable dttt = new DataTable();
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            dingedataGridView.DataSource = null;
            //dttt.Clear();
            //dataGridView1.Columns.Clear();
            TreeNode node = treeView1.SelectedNode;
            //if (node == null) return;
            //if (node.Level == 1) return;

            if (treeView1.SelectedNode != null)
            {
                string strWBS = treeView1.SelectedNode.Name;
                string strSql = "select 定额编号,WBS,定额名称,标签,标准费率 from 建筑工程定额 where WBS='" + strWBS + "'";
                oap = new OleDbDataAdapter(strSql, connection);
                oap.Fill(dss, "建筑工程定额");
                DataTable dttt = dss.Tables["建筑工程定额"];
                dingedataGridView.DataSource = dttt;

            }
            dingedataGridView.Update();
        }

        private void btnOutput_Click(object sender, EventArgs e)
        {
            try
            {
                //DataRow dr;
                //连接字符串
                string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\我的文档\\Access5D数据库.accdb";
                OleDbConnection odcConnection = new OleDbConnection(strConnection);
                //打开连接对象
                //此处无法正常连接
                odcConnection.Open();

                //将datagridview中的数据插入到数据表中
                if (dingedataGridView.SelectedRows.Count == 0)
                {
                    MessageBox.Show("你没有选择任何资源");
                }
                else
                {
                    int i;
                    for (i = 0; i < dingedataGridView.SelectedRows.Count; i++)
                    {
                        //string id = dataGridView1.Rows[i].Cells[0].Value.ToString();
                        string debh = dingedataGridView.SelectedRows[i].Cells[1].Value.ToString();
                        string demc = dingedataGridView.SelectedRows[i].Cells[3].Value.ToString();
                        string dw = dingedataGridView.SelectedRows[i].Cells[4].Value.ToString();
                        string jg = dingedataGridView.SelectedRows[i].Cells[5].Value.ToString();

                        //用sql语句将数据插入到access5D数据库的资源表中
                        string revitQuery = "Insert into 资源表(定额编号,定额名称,单位,基价) Values('" + debh + "','" + demc + "','" + dw + "','" + jg + "')";
                        //用sql语句构件对应的资源数据传入资源配置表
                        //string sql="Insert into 资源配置表(资源ID,资源名称,资源量单位)Values('"+debh+"','"+demc+"','"+dw+"')";
                        OleDbCommand oldCmd = new OleDbCommand(revitQuery, odcConnection);

                        int r = oldCmd.ExecuteNonQuery();
                        if (r > 0)
                        {
                            MessageBox.Show("保存成功");
                        }
                        //oldConnection.Close();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //DataRow dr;
            try
            {
                //连接字符串
                string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\我的文档\\Access5D数据库.accdb";
                OleDbConnection odcConnection = new OleDbConnection(strConnection);
                //打开连接对象
                //此处无法正常连接
                odcConnection.Open();
                //将datagridview中的数据插入到数据表中
                if (dingedataGridView.SelectedRows.Count == 0)
                {
                    MessageBox.Show("你没有选择任何资源");
                }
                else
                {
                    int i;
                    for (i = 0; i < dingedataGridView.SelectedRows.Count; i++)
                    {
                        string zyid = dingedataGridView.SelectedRows[i].Cells[1].Value.ToString();
                        string zymc = dingedataGridView.SelectedRows[i].Cells[3].Value.ToString();
                        string zyldw = dingedataGridView.SelectedRows[i].Cells[4].Value.ToString();
                        //用sql语句构件对应的资源数据传入资源配置表
                        string sql = "Insert into 资源配置表 (资源ID,资源名称,资源量单位) Values('" + zyid + "','" + zymc + "','" + zyldw + "')";
                        OleDbCommand oldCmd = new OleDbCommand(sql, odcConnection);
                        int r = oldCmd.ExecuteNonQuery();
                        if (r > 0)
                        {
                            MessageBox.Show("保存成功");
                        }
                        odcConnection.Close();
                        return;
                    }
                }
            }
            catch (Exception em)
            {
                MessageBox.Show(em.Message, "错误提示");
            }

            //DataTable deliveryinformationTable = StoreDeliveryInfo();
            //Form1 frm = new Form1(deliveryinformationTable);
            // frm.ShowDialog();
        }

        /// 
        /// 收集信息表
        /// 
        /// 
        private DataTable StoreDeliveryInfo()
        {
            DataTable informationTable = CreateDeliveryInfo();
            DataRow newRow = informationTable.NewRow();

            string zyid = null;
            string zymc = null;
            string zyldw = null;
            // for (int j = 0; j < dingedataGridView.SelectedRows.Count; j++)
            //{
            for (int i = 0; i < dingedataGridView.SelectedRows.Count; i++)
            {
                if (dingedataGridView.SelectedRows[i].Cells[0].Value != null)
                {
                    zyid = dingedataGridView.SelectedRows[i].Cells[1].Value.ToString();
                    zymc = dingedataGridView.SelectedRows[i].Cells[3].Value.ToString();
                    zyldw = dingedataGridView.SelectedRows[i].Cells[4].Value.ToString();

                    newRow["资源ID"] = zyid;
                    newRow["资源名称"] = zymc;
                    newRow["资源量单位"] = zyldw;

                    informationTable.Rows.Add(newRow);
                }
            }
            // }
            return informationTable;
        }

        /// 
        /// 创建空白表
        /// 
        /// 
        private DataTable CreateDeliveryInfo()
        {
            DataTable infomationTable = new DataTable("InfomationTable");

            DataColumn zyidColumn = new DataColumn();
            zyidColumn.DataType = typeof(System.String);
            zyidColumn.ColumnName = "资源ID";
            zyidColumn.Caption = "资源ID";
            zyidColumn.ReadOnly = false;
            infomationTable.Columns.Add(zyidColumn);

            DataColumn zymcColumn = new DataColumn();
            zymcColumn.DataType = typeof(System.String);
            zymcColumn.ColumnName = "资源名称";
            zymcColumn.Caption = "资源名称";
            zymcColumn.ReadOnly = false;
            infomationTable.Columns.Add(zymcColumn);

            DataColumn zyldwColumn = new DataColumn();
            zyldwColumn.DataType = typeof(System.String);
            zyldwColumn.ColumnName = "资源量单位";
            zyldwColumn.Caption = "资源量单位";
            zyldwColumn.ReadOnly = false;
            infomationTable.Columns.Add(zyldwColumn);

            return infomationTable;
        }

    }

 

你可能感兴趣的:(revit 二次开发 可停靠窗体winform控件)