1.Create a new project
选择新建项目中的C#–类库,建立项目名称为“HelloWorld”。
2.Add Reference
1)在Revit安装路径中添加RevitAPI.dll。例如,安装路径为:D:\Learning Software\Revit\Revit 2014\RevitAPI.dll。
2)添加引用后,右键选择 RevitAPI.dll属性,将复制本地种的true改为false。
3)RevitAPIUI.dll 按以上步骤添加。
3.Add Code
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
namespace HelloWorld
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
//此处的TransactionMode比如由Automatic改为Manual,不然在调试时会出现“revit无法运行外部程序”
public class Class1:IExternalCommand
{
public Autodesk.Revit.UI.Result Execute(ExternalCommandData revit, ref string message, ElementSet elements)
{
TaskDialog.Show("Revit", "Hello World");
return Autodesk.Revit.UI.Result.Succeeded;
}
}
}
4.Build the Program
5.Create a .addin manifest file
在C#中新建一个文本文件,将代码写入
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<RevitAddIns>
<Assembly>E:\C#\sample\HelloWorld\HelloWorld\bin\Debug\HelloWorld.dll</Assembly>
//此处的HelloWorld.dll在项目生成后才会有,开始看不到
<AddInId>239BD853-36E4-461f-9171-C5ACEDA4E721</AddInId>
//此处的ID在解决方案管理器项目中Properties的AssemblyInfo.cs中可以查看
<FullClassName>HelloWorld.Class1</FullClassName>
//此处的HelloWorld为项目名,Class1为类名
<Text>HelloWorld</Text>
<VendorId>ADSK</VendorId>
<VendorDescription>Autodesk, www.autodesk.com</VendorDescription>
</AddIn>
</RevitAddIns>
将文本格式保存在.addin格式,并保存在C:\ProgramData\Autodesk\Revit\Addins\2014(2014为目前revit的版本)
注:C:\ProgramData为隐藏文件,若要显示,步骤如下:
组织–文件夹和搜索选项–查看–显示隐藏的文件、文件夹和驱动器
6.Debug the Add-in
在解决方案资源管理器中,将项目的内容展示出来,点击其中的Properties,窗口会出现。
点击其中的调试(Debug),选择启动外部程序,将revit.exe的路径写出来即可。
最后进行调试,调试时会自动开启revit软件,在添加外部工具中可找到helloworld。