Revit二次开发——一个简单的插件

Revit简单插件

流程:创建windows窗体应用-》配置Revit类库引用-》窗体设计-》代码编写-》编译运行

窗体设计

在这里插入图片描述
在这里插入图片描述

在工具箱中选择控件

在窗口中添加控件

点击控件可以修改代码

在这里插入图片描述

##

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;

namespace SelectionDemo
{
    public partial class Form1 : Form
    {
        public Form1(string idsText = "")
        {
            InitializeComponent();
            textBox1.Text = idsText;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}

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 Form = System.Windows.Forms.Form;

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace SelectionDemo
{
    public partial class Form2 : Form
    {
        private Document doc;
        public Form2(Document doc)
        {
            this.doc = doc;
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.button1_Click(sender, e);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //选集有效性检查
            if (null != RevitSelection && RevitSelection.IsValidObject)
            {
                RevitSelection.SetElementIds(new List<ElementId>());
                string[] idTexts = textBox1.Text.Trim().Split(',');
                string error = "将不使用下列无效图元 ID:";
                bool hasError = false;
                ICollection<ElementId> selIds = new List<ElementId>();

                for (int i = 0; i < idTexts.Length; i++)
                {
                    int id = 0;
                    if (!int.TryParse(idTexts[i], out id) || this.doc.GetElement(new ElementId(id)) == null)
                    {
                        hasError = true;
                        error += idTexts[i] + ",";
                    }
                    else
                    {
                        selIds.Add(new ElementId(id));
                    }
                }

                if (hasError)
                {
                    error = error.Remove(error.Count() - 1) + ";";
                    TaskDialog.Show("Revit", error);
                }

                RevitSelection.SetElementIds(selIds);

                if (RevitSelection.GetElementIds().Count == 0)
                {
                    TaskDialog.Show("Revit", "未找到完好的视图");
                }
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        public Selection RevitSelection { get; set; }
    
    }
}

代码编写

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace SelectionDemo
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class IdsOfSelectionCmd : IExternalCommand
    {
        public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                ICollection<ElementId> selIds = commandData.Application.ActiveUIDocument.Selection.GetElementIds();

                if (0 != selIds.Count)
                {
                    string idsText = string.Empty;
                    string spliter = ",";
                    foreach (ElementId eleId in selIds)
                    {
                        idsText += eleId.ToString() + spliter;
                    }
                    System.Windows.Forms.Form fm = new Form1(idsText.Remove(idsText.Length - 1));
                    fm.ShowDialog();
                }

                return Autodesk.Revit.UI.Result.Succeeded;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Autodesk.Revit.UI.Result.Failed;
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace SelectionDemo
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class SelectByIdCmd : IExternalCommand
    {
        public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                UIDocument uidoc = commandData.Application.ActiveUIDocument;
                Form2 fm = new Form2(uidoc.Document);
                fm.RevitSelection = uidoc.Selection;
                fm.ShowDialog();
                return Autodesk.Revit.UI.Result.Succeeded;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Autodesk.Revit.UI.Result.Failed;
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Autodesk.Revit.UI;

namespace SelectionDemo
{
    public class SelectionApp : IExternalApplication
    {
        public Autodesk.Revit.UI.Result OnStartup(Autodesk.Revit.UI.UIControlledApplication application)
        {
            try
            {
                string assemblyPath = @"D:\培训资料\test\SelectionDemo\bin\Debug\SelectionDemo.exe";

                RibbonPanel panel = application.CreateRibbonPanel("查询");
                string nmspace = "SelectionDemo.";
                // 节点面板控件集合
                panel.AddStackedItems(
                    new PushButtonData("btn_idToElement", "按ID选择", assemblyPath, nmspace + "SelectByIdCmd")
                    {
                    },
                    new PushButtonData("btn_ElementToId", "选择项的ID", assemblyPath, nmspace + "IdsOfSelectionCmd")
                    {
                    }
                );

                return Result.Succeeded;
            }
            catch
            {
                return Result.Failed;
            }
        }

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

在这里插入图片描述

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孤影墨客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值