流程:创建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;
}
}
}