Revit SDK:ErrorHandling

前言

本文介绍 Revit 的错误处理机制。

内容

程序员对错误处理的定义和理解

程序的错误处理机制可以分为两种类型:错误返回码和异常捕捉。

错误返回码是指在程序中遇到错误时,通过函数返回值来表明错误的类型和信息。错误返回码可以在程序中被预测和处理,但是需要开发者在每个函数中都进行判断和处理,增加了代码的复杂度和冗余。此外,错误返回码只能在同步编程中使用,对于异步编程则无法发挥作用。

异常捕捉则是指在程序中遇到错误时,通过抛出异常来表明错误的类型和信息。异常会一直向上层传递,直到被捕捉或者程序崩溃。异常捕捉可以大大减少代码的复杂度和冗余,同时也能够在异步编程中发挥作用。

不同类型的软件可能有不同的错误处理机制,但是错误返回码和异常捕捉都是常见的处理方式。除此之外,一些软件还会采用日志记录、告警通知等方式来处理错误。错误处理机制的选择主要取决于具体的应用场景和需求。

Revit API 中错误处理机制

从日常的编码过程中,Revit API 在出错的时候是会抛出异常的,并且提供机会让用户自己来处理异常。因此,可以认为 Revit API 中的错误处理机制包含了异常。同时,Revit API 也内置了多种错误码,因此也可以认为它是有错误返回嘛的。
从 Revit 官网可以查到它的错误处理机制:Failure Posting and Handling
在Revit API 中,错误包含两种类型,一种是Revit自带的错误,另外一种是用户定义的错误。前者,系统会自动抛出;后者,需要通过 Revit API 定义。

SDK例子中的错误处理

抛出一个 Warning 并在 FailurePreproccessor 中处理

下面两段代码都是抛出一个 Warning 并在 FailurePreproccessor 中处理,差别在于一个是主动抛出,另一个是让墙体重合,让 Revit 自动抛出。重点注意,事务Transaction 有接口用来设置错误处理Transaction.SetFailureHandlingOptions,传入的参数为 FailureHandlingOptions 。而 FailureHandlingOptions 可以设置前置错误处理 FailureHandlingOptions.SetFailuresPreprocessor

在这里插入图片描述

// 主动抛出 warning,并在FailurePreprocessor中处理
string message = "";
try
{
	Transaction transaction = new Transaction(m_doc, "Warning_FailurePreprocessor");
	FailureHandlingOptions options = transaction.GetFailureHandlingOptions();
	FailurePreprocessor preprocessor = new FailurePreprocessor(); 
	options.SetFailuresPreprocessor(preprocessor);
	transaction.SetFailureHandlingOptions(options);
	transaction.Start();
	FailureMessage fm = new FailureMessage(m_idWarning);
	m_doc.PostFailure(fm);
	transaction.Commit();
}
catch(System.Exception)
{
	message = "Failed to commit transaction Warning_FaiurePreprocessor";
	return Result.Failed;
}

// 墙体重合,Revit 自动抛出warning,并在FailurePreprocessor中处理
try
{
	Transaction transaction = new Transaction(m_doc, "Warning_FailurePreproccessor_OverlappedWall");
	FailureHandlingOptions options = transaction.GetFailureHandlingOptions();
	FailurePreprocessor preproccessor = new FailurePreprocessor();
	options.SetFailuresPreprocessor(preproccessor);
	transaction.SetFailureHandlingOptions(options);
	transaction.Start();

	// 在相同的位置创建两堵墙
	Line line = Line.CreateBound(new XYZ(-10, 0, 0), new XYZ(-20, 0, 0));
	Wall wall1 = Wall.Create(m_doc, line, level1.Id, false);
	Wall wall2 = Wall.Create(m_doc, line, level1.Id, false);
	m_doc.Regenerate();

	transaction.Commit();
}
catch (System.Exception)
{
	message = "Failed to commit transaction Warning_FailurePreproccessor_OverlappedWall";
	return Result.Failed;
}

通过 Application 来处理错误

包含两种:使用事件来处理错误,注册回调来处理错误。
在这里插入图片描述

// Autodesk.Revit.ApplicationServices.Application
try
{
	m_revitApp.FailuresProcessing += new EventHandler<Autodesk.Revit.DB.Events.FailuresProcessingEventArgs>(FailuresProcessing);
	Transaction transaction = new Transaction(m_doc, "Error_FailuresProcessingEvent");
	transaction.Start();
	Line line = Line.CreateBound(new XYZ(0, 10, 0), new XYZ(20, 10, 0));
	Wall wall = Wall.Create(m_doc, line, level1.Id, false);
	m_doc.Regenerate();
	FailureMessage fm = new FailureMessage(m_idError);
	FailureResolution fr = DeleteElements.Create(m_doc, wall.Id);
	fm.AddResolution(FailureResolutionType.DeleteElements, fr);
	m_doc.PostFailure(fm);
	transaction.Commit();
}
catch (System.Exception)
{
	message = "Failed to commit transaction Error_FailuresProcessingEvent";
	return Result.Failed;
}

try
{
	FailuresProcessor processor = new FailuresProcessor();
	Application.RegisterFailuresProcessor(processor);
	Transaction transaction = new Transaction(m_doc, "Error_FailuresProcessor");
	transaction.Start();
	Line line = Line.CreateBound(new XYZ(0, 20, 0), new XYZ(20, 20, 0));
	Wall wall = Wall.Create(m_doc, line, level1.Id, false);
	m_doc.Regenerate();
	FailureMessage fm = new FailureMessage(m_idError);
	FailureResolution fr = DeleteElements.Create(m_doc, wall.Id);
	fm.AddResolution(FailureResolutionType.DeleteElements, fr);
	m_doc.PostFailure(fm);
	transaction.Commit();
}
catch (System.Exception)
{
	message = "Failed to commit transaction Error_FailuresProcessor";
	return Result.Failed;
}

错误处理的具体内容详见SDK源码。源码中的拼写错误 FailurePreproccessor 多了一个 c,实际为 FailurePreprocessor

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客BIM工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值