【Revit二次开发学习笔记】将 helloworld 添加到选项卡

第一步:理清思路

要创建组定义按钮,需要实现 IExternalApplication 接口

这个类必须同时实现两个接口

OnStartup  和  OnShutdown

需要注意的是大小写,必须要一样才可以,想要调用这个接口必须实现这两个成员。

 

第二步:添加代码

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

using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using System.Windows.Media.Imaging;

namespace ClassLibrary2
{
    public class Class1:IExternalApplication
    {
        public Result OnStartup(UIControlledApplication application)
        {
            //添加一个新的Ribbon面板
            RibbonPanel ribbonPanel = application.CreateRibbonPanel("NewRibbonPanel");

            //在新的Ribbon面板上添加一个按钮
            //点击这个按钮里面对应路径的程序会被执行
            PushButton pushButton = ribbonPanel.AddItem(new PushButtonData("hellorevit", "HELLOREVIT", @"D:\1a_zys\revitproject\hellorevit\ClassLib
### 实现Revit API C# 输出 'hello world' 为了在Revit环境中使用C#输出'hello world',可以创建一个外部命令类并重写`Execute`方法,在该方法内编写控制台打印语句来展示消息[^1]。 下面是一个简单的例子: ```csharp using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.UI; [Transaction(TransactionMode.Manual)] public class HelloWorldCommand : IExternalCommand { public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { // 获取当前文档实例 Document doc = commandData.Application.ActiveUIDocument.Document; // 使用TaskDialog显示信息给用户 TaskDialog.Show("Message", "Hello World"); return Result.Succeeded; } } ``` 此代码片段展示了如何利用Revit的API接口向用户弹窗显示“Hello World”。这里采用了`TaskDialog.Show()`函数用于呈现对话框窗口[^3]。当加载含有上述代码逻辑的插件DLL文件至Revit环境时,执行对应的命令将会触发这个弹窗事件。 对于希望进一步扩展功能的情况,比如为项目文件添加自定义属性,则可参照如下方式操作: ```csharp Document doc = commandData.Application.ActiveUIDocument.Document; doc.ProjectInformation.Description = "This is a test"; // 添加自定义属性 if (null == doc.ProjectInformation.LookupParameter("CustomProp")) { Parameter p = doc.ProjectInformation.get_Parameter(new Guid("{A76F45B2-8D9E-4CE0-A3CF-EDEAECAEA3FF}")); if (p != null && !p.IsReadOnly) { p.Set("Hello"); } // 或者直接设置自定义参数值 using (Transaction trans = new Transaction(doc)) { trans.Start("Add Custom Property"); IndependentTag tag = ... ; // 假设已经获取到了某个标签对象 tag.GetAssociatedElementId(out ElementId elemId); FamilyInstance fi = doc.GetElement(elemId) as FamilyInstance; if (fi != null) { fi.get_Parameter("Comments").Set("world"); // 如果存在名为"Comments"的参数则赋值 } trans.Commit(); } } else { doc.ProjectInformation.set_ParameterValueByName("CustomProp", "world"); } // 对于其他类型的元素也可以采用类似的方法为其附加额外的信息 file.CustomProperties.Add("Hello", "world")[^2]; ``` 需要注意的是,实际应用中应当根据具体需求调整代码细节,以上仅作为概念性的指导说明。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值