Revit调用winform

来源:互联网 发布:淘宝app里面是h5吗 编辑:程序博客网 时间:2024/06/08 01:38

//以下代码写在revit的class中是为了在revit 调用API中能找到对应的方法:cmdShowForm 和 cmdFromForm

using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.ApplicationServices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DB = Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace ZSTCRevit
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class cmdShowForm : IExternalCommand
    {
        public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elements)
        {
            FrmCmd frmCmd = new FrmCmd(cmdData, msg, elements);
            frmCmd.Show();
            return Result.Succeeded;
        }
    }
    //在窗体里执行Revit命令
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class cmdFromForm : IExternalCommand
    {
        public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elements)
        {
            string version = cmdData.Application.Application.VersionBuild;
            version += "\n" + cmdData.Application.Application.VersionName;
            version += "\n" + cmdData.Application.Application.VersionNumber;
            TaskDialog.Show("info", version);
            return Result.Succeeded;
        }
    }

}


//以下代码是写winform中 ,当然了要先建立一个winform

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DB = Autodesk.Revit.DB;

namespace ZSTCRevit
{
    public partial class FrmCmd : Form
    {
        Autodesk.Revit.UI.ExternalCommandData cmdDataForm;
        string msgForm;
        DB.ElementSet elementsForm = new DB.ElementSet();
        public FrmCmd()
        {
            InitializeComponent();
        }
        public FrmCmd(Autodesk.Revit.UI.ExternalCommandData cmdData, string msg, DB.ElementSet elements)
        {
            InitializeComponent();
            cmdDataForm = cmdData;
            msgForm = msg;
            elementsForm = elements;
        }

//以下是测试在form中增加一些数据。
        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.Items.Add("param1");
            listBox1.Items.Add("param2");
          }

        private void button1_Click(object sender, EventArgs e)
        {
            cmdFromForm fromForm = new cmdFromForm();
            fromForm.Execute(cmdDataForm, ref msgForm, elementsForm);
        }
    }
}

原创粉丝点击