【Revit二次开发学习笔记】HelloWorld

我看的书是《AUTODESK REVIT 二次开发基础教程》

我发现我还是因为太菜了,所以看不太懂书上前两章的内容

所以直接从实战示例跟着做

————————————————————————————————

第一步:打开 Visual Studio 新建一个类库(.dll)的项目

因为 revit 的插件只能通过  .dll  文件来进行访问(好像是这样,我也不太确定)

 

这个地方需要注意,因为是 vs2019  所以创建项目的选项和以前有些不一样

类库还有一个是  .NET Standard 的 ,这个是不行的噢。

 

第二步:添加引用

我们需要下载 RevitAPI.dll 和 RevitAPIUI.dll  并将他们引用一下,这样才可以调用到 revit  提供给我们的接口

如果不需要将他们保存到本地,可以在他们的属性菜单里面关闭

 

第三步:编写项目代码

using System;
using System.Collections.Generic;
using System.Linq;
using 
### 实现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、付费专栏及课程。

余额充值