1. RevitAPI 基础(上)
2020-01-29 21:38 wwx的个人博客 阅读(1863) 评论(1) 编辑 收藏 举报一 重要的基本设置:
1. 类库:revitAPI.DLL, revitAPIUI.DLL,个人理解前者包括了revit软件所特有的数据类型及软件中存在的全部后台数据,而后者是包含了大量与实现UI交互相关的接口,主要有IExternalCommand, IExternalApplication, Seletion选择功能, 菜单制作与任务对话框的制作
2. IExternalCommand:规定了外部命令扩展功能的接口供类(class)使用,只有一个抽象函数Execute,其有三个参数,commandData(输入参数,主要是Application, View, JournalData三种类型),message(输出参数,以string类为主,主要用来返回错误信息),elements(错误发生后高亮显示的元素)。
3.IExternalApplication:个人理解这个接口主要用于实现已有解决方案的拼接。这个接口本身定义了OnStartup和OnShutdown两个函数用于拼接已存在的dll以及定制UI,其不提供直接操作软件后台数据的方法。
4.IExternalDBApplication:这个口用于处理数据库级别的事件(即不涉及UI交互的后台数据增删改查),本质是3的一种特殊形式,其也通过上述两个方法拼接解决方案,但未提供定制UI的方法。
5.在继承IExternalCommand接口之前,必须定义事务Transaction的处理方式,[Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]。除mannual外还有两个readonly与Automatic两种。
二 revit文档的获取(commandData, UIApplication, Application, DocumentSet, Documents, Document, ActiveDocument之间的关系)
commandData:revit后台数据的集合及相关的属性信息 UIApplication:后台数据的集合,只能通过Application方法引用,commandData和两种document都包含UIApplication。 UIDocument:个人理解其表示可交互的文档中包含信息的集合,并提供了通过不同UI交互过程提取信息的方法,如seletion交互获取文档,只能通过uiapplication进行引用。ActiveDocument:当前的活动文档,只能通过uidocument进行引用,是UIdocument的一部分。
Document:代表一个独立的工程文件,不一定是最顶级的。ActiveUIDocument.Document代表引用了当前活动文档。Documents代表后台中的全部文档,只能用Application.Documents进行引用。
真的太乱了!还是画图吧!其实最乱的就是Document和UIDocument,其主要区别在于Document中不能用seletion方法,因而其主要用过滤器过滤元素,而UIDocument中出现了大量有交互有关的方法。
此外document类中规定了许多与文件创建相关的方法,其application属性代表的是此document实例位于的应用实例,UIdocument也同理。
三 最后贴一段代码,主要是IExternalCommand的使用,遍历了用seletion选择的元素
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
namespace chapter2
{
[Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
public class class1 : IExternalCommand//让class1继承这个接口中的方法,即Execute
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
try
{
UIApplication uIApplication = commandData.Application;
UIDocument uIDocument = uIApplication.ActiveUIDocument;
Document document = uIDocument.Document;
List<ElementId> selectedElem = new List<ElementId>();
List<Element> walls = new List<Element>();
List<Element> beams = new List<Element>();
List<ElementId> beamid= new List<ElementId>();
int countW = 0;int countBeam = 0;
foreach ( var id in uIDocument.Selection.GetElementIds())
{
Element element=uIDocument.Document.GetElement(id);
if(element is Wall)
{
selectedElem.Add(id);
walls.Add(element);
countW++;
}
if (element is BeamSystem)
{
beamid.Add(id);
beams.Add(element);
countBeam++;
}
}
TaskDialog.Show("hello revit","模型中共有"+countW.ToString()+"个墙"+countBeam.ToString()+"个柱");
foreach(var wall in walls)
{ TaskDialog.Show("墙的编号是",wall.Name);}
TaskDialogResult taskDialogResult = TaskDialog.Show(
"revit",
"TES to continue," + "NO to cancel",
TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No);
if(TaskDialogResult.Yes==taskDialogResult)
{
return Result.Succeeded;
}
else if(TaskDialogResult.No == taskDialogResult)
{
return Result.Cancelled;
}
else
{
return Result.Failed;
}
}
catch
{
message = "unexpected errors";
return Result.Failed;
}
//throw new NotImplementedException();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具