最近被分配了一个任务
开发一个小插件
点击后要实现打开网页这个功能
研究了很多很多
其实一直没搞懂
到底应该怎么去开发这个东西
甚至去 反编译了别人的代码去看
看了半天发现看不懂hhhhh
最后终于整明白了
首先 默认安装路径下的 addin 文件 也就是
C:\ProgramData\Autodesk\Revit\Addins\2016
这个目录下面(当然要看你的revit的版本号)
创建一个 和 里面已经有的文件差不多格式的 addin 后缀文件就好了
<?xml version="1.0" encoding="utf-8"?>
<RevitAddIns>
<AddIn Type="Application">
<Name>ZY</Name>
<!-- <下面一条语句中的路径是项目编译(生成)时产生的dll的路径> -->
<Assembly>********************</Assembly>
<!-- <AddInId>下面一条语句是你的项目的GUID码</AddInId> -->
<AddInId>*****************</AddInId>
<!-- <下面一条语句中的类名是创建项目时的类名> -->
<FullClassName>eduweblink.Class1</FullClassName>
<!-- <下面一条语句中的在revit中显示的插件名称> -->
<Text>eduweblink1</Text>
<VendorId>NAME</VendorId>
<VendorDescription>Your Company Information</VendorDescription>
</AddIn>
</RevitAddIns>
接下来,我们就创建一个新的工程 用来在 Revit 上面的菜单栏上面添加一个自己的选项卡
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using System.Windows.Media.Imaging;
namespace eduweblink
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class Class1 : IExternalApplication
{
public Result OnStartup(UIControlledApplication application)
{
string TabName = "官网网址";
application.CreateRibbonTab(TabName);
RibbonPanel P_TabName1 = application.CreateRibbonPanel(TabName, "官网链接");
PushButtonData button110 = new PushButtonData("网站", "官方网站 ", @"C:\Users\Administrator\Desktop\RevitProject\eduweblink\eduweblink\linktoweb\bin\Debug\linktoweb.dll", "linktoweb.Class1");
Uri uriImage110 = new Uri(@"C:\Users\Administrator\Desktop\美术资源\装备图标\图标250X250\1111.png");
BitmapImage largeimage110 = new BitmapImage(uriImage110);
button110.LargeImage = largeimage110;
P_TabName1.AddItem(button110);
//P_TabName1.AddSeparator();
//P_TabName1.AddSeparator();
//P_TabName1.AddSeparator();
return Result.Succeeded;
}
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
}
}
这上面的每一个 头文件都用到了噢
忘记说了,要记得先添加 revitapi 和 revitui 的两个引用文件,
不然会报错。
那两个引用文件就不在这里说了
然后通过上面的 这个路径连接到 我们真正要实现功能的代码文件
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
namespace linktoweb
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Automatic)]
public class Class1: IExternalCommand
{
public Result Execute(ExternalCommandData revit, ref string message, ElementSet elements)
{
// TaskDialog.Show("hello", "this is my first project");
//调用系统默认的浏览器
System.Diagnostics.Process.Start("http://www.baidu.com/");
return Result.Succeeded;
}
}
}
可以看到 这上面的 这些代码是 缺一不可,
少一点都会报错
因为
想要实现接口功能,必须要继承 一个 IExternalCommand 类
还有其他两个接口目前我还没用到
然后这个类 必须要实现 Execute 函数 才行 且必须要有返回值
调用 浏览器的代码 就不多说了,这是我百度来的。hhh
我也没看懂
最后再贴一个 IExternalCommand 的实现
他是这样写的
using Autodesk.Revit.DB;
namespace Autodesk.Revit.UI
{
//
// 摘要:
// An interface that should be implemented to provide the implementation for a Revit
// add-in External Command.
//
// 备注:
// To add an external command to Autodesk Revit the developer should implement an
// object that supports the IExternalCommand interface.
public interface IExternalCommand
{
//
// 摘要:
// Overload this method to implement and external command within Revit.
//
// 参数:
// commandData:
// An ExternalCommandData object which contains reference to Application and View
// needed by external command.
//
// message:
// Error message can be returned by external command. This will be displayed only
// if the command status was "Failed". There is a limit of 1023 characters for this
// message; strings longer than this will be truncated.
//
// elements:
// Element set indicating problem elements to display in the failure dialog. This
// will be used only if the command status was "Failed".
//
// 返回结果:
// The result indicates if the execution fails, succeeds, or was canceled by user.
// If it does not succeed, Revit will undo any changes made by the external command.
Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements);
}
}
用 有道翻译 翻译一下 大概就是这个意思
摘要:
应该实现的接口,以提供Revit的实现
外接程序外部命令。
备注:
要向Autodesk Revit添加一个外部命令,开发人员应该实现一个
对象,该对象支持IExternalCommand接口。
摘要:
重载此方法以在Revit中实现和外部命令。
参数:
commandData:
一个ExternalCommandData对象,它包含对应用程序和视图的引用
外部命令需要。
信息:
错误信息可以通过外部命令返回。这将只显示
如果命令状态为“Failed”。这里有1023个字符的限制
消息;长度大于此值的字符串将被截断。
元素:
元素集,指示要在故障对话框中显示的问题元素。这
将仅在命令状态为“Failed”时使用。
返回结果:
结果表示执行失败、成功或被用户取消。
如果失败,Revit将撤销外部命令所做的任何更改。
机翻的,不过大概也是可以看懂的。
慢慢体会。