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;
}
}
}
本文来自博客园,作者:Patrick-Rex,转载请注明原文链接:https://www.cnblogs.com/patrickrex/p/18028776
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升