Revit二次开发示例:ErrorHandling

时间:2023-01-12 18:14:40

本示例介绍了Revit的错误处理。

 

#region Namespaces
using System;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI; #endregion namespace ErrorHandling
{
[Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Journaling(JournalingMode.NoCommandData)]
public class Command : IExternalCommand, IExternalApplication
{
public static FailureDefinitionId m_idWarning;
public static FailureDefinitionId m_idError;
private FailureDefinition m_fdWarning;
private FailureDefinition m_fdError;
private Application m_revitApp;
private Document m_doc; public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
} public Result OnStartup(UIControlledApplication application)
{
try
{
// Create failure definition Ids
Guid guid1 = new Guid("0C3F66B5-3E26-4d24-A228-7A8358C76D39");
Guid guid2 = new Guid("93382A45-89A9-4cfe-8B94-E0B0D9542D34");
Guid guid3 = new Guid("A16D08E2-7D06-4bca-96B0-C4E4CC0512F8");
m_idWarning = new FailureDefinitionId(guid1);
m_idError = new FailureDefinitionId(guid2); m_fdWarning = FailureDefinition.CreateFailureDefinition(m_idWarning, FailureSeverity.Warning, "I am the warning.");
m_fdError = FailureDefinition.CreateFailureDefinition(m_idError, FailureSeverity.Error, "I am the error"); m_fdWarning.AddResolutionType(FailureResolutionType.MoveElements, "MoveElements", typeof(DeleteElements));
m_fdWarning.AddResolutionType(FailureResolutionType.DeleteElements, "DeleteElements", typeof(DeleteElements));
m_fdWarning.SetDefaultResolutionType(FailureResolutionType.DeleteElements); m_fdError.AddResolutionType(FailureResolutionType.DetachElements, "DetachElements", typeof(DeleteElements));
m_fdError.AddResolutionType(FailureResolutionType.DeleteElements, "DeleteElements", typeof(DeleteElements));
m_fdError.SetDefaultResolutionType(FailureResolutionType.DeleteElements); }
catch (Exception)
{
return Result.Failed;
} return Result.Succeeded;
} public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
m_revitApp = commandData.Application.Application;
m_doc = commandData.Application.ActiveUIDocument.Document; Level level1 = GetLevel();
if (level1 == null)
{
throw new Exception("[ERROR] Failed to get level 1");
} try
{
try
{
Transaction transaction = new Transaction(m_doc, "Warning_FailurePreproccessor");
FailureHandlingOptions options = transaction.GetFailureHandlingOptions();
FailurePreproccessor preprocessor = new FailurePreproccessor();
options.SetFailuresPreprocessor(preprocessor);
transaction.SetFailureHandlingOptions(options);
transaction.Start();
FailureMessage fm = new FailureMessage(m_idWarning);
m_doc.PostFailure(fm);
transaction.Commit();
}
catch (Exception)
{
message = "Failed to commit transaction Warning_FailurePreprocessor";
return Result.Failed;
} try
{
Transaction transacton = new Transaction(m_doc, "Warning_FailurePreprocessor_OverlappedWall");
FailureHandlingOptions options = transacton.GetFailureHandlingOptions();
FailurePreproccessor preproccessor = new FailurePreproccessor();
options.SetFailuresPreprocessor(preproccessor);
transacton.SetFailureHandlingOptions(options);
transacton.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(); transacton.Commit();
}
catch (Exception)
{
message = "Failed to commit transaction Warning_FailurePreproccessor_OverlappedWall";
return Result.Failed;
} try
{
m_revitApp.FailuresProcessing += FailuresProcessing;
Transaction transactoin = new Transaction(m_doc, "Error_FailuresProcessingEvent");
transactoin.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);
transactoin.Commit();
}
catch (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 (Exception)
{
message = "Failed to commit transaction Error_FailuresProcessor";
return Result.Failed;
} }
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
} return Result.Succeeded;
} void FailuresProcessing(object sender, Autodesk.Revit.DB.Events.FailuresProcessingEventArgs e)
{
FailuresAccessor failuresAccessor = e.GetFailuresAccessor();
string transactionName = failuresAccessor.GetTransactionName(); IList<FailureMessageAccessor> fams = failuresAccessor.GetFailureMessages();
if (fams.Count == 0)
{
e.SetProcessingResult(FailureProcessingResult.Continue);
return;
} if (transactionName.Equals("Error_FailuresProcessingEvent"))
{
foreach (FailureMessageAccessor fma in fams)
{
FailureDefinitionId id = fma.GetFailureDefinitionId();
if (id == Command.m_idError)
{
failuresAccessor.ResolveFailure(fma);
}
} e.SetProcessingResult(FailureProcessingResult.ProceedWithCommit);
return;
} e.SetProcessingResult(FailureProcessingResult.Continue);
} private Level GetLevel()
{
Level level1 = null; FilteredElementCollector collector = new FilteredElementCollector(m_doc);
ElementClassFilter filter = new ElementClassFilter(typeof(Level));
IList<Element> levels = collector.WherePasses(filter).ToElements(); foreach (Level level in levels)
{
if (level.Name.Equals("Level 1"))
{
level1 = level;
break;
}
} return level1;
}
} public class FailurePreproccessor : IFailuresPreprocessor
{
public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor)
{
IList<FailureMessageAccessor> fmas = failuresAccessor.GetFailureMessages();
if (fmas.Count == 0)
{
return FailureProcessingResult.Continue;
} string transactionName = failuresAccessor.GetTransactionName();
if (transactionName.Equals("Warning_FailurePreprocessor"))
{
foreach (FailureMessageAccessor fma in fmas)
{
FailureDefinitionId id = fma.GetFailureDefinitionId();
if (id == Command.m_idError)
{
failuresAccessor.ResolveFailure(fma);
}
}
return FailureProcessingResult.ProceedWithCommit;
}
else
{
return FailureProcessingResult.Continue;
}
}
} public class FailuresProcessor : IFailuresProcessor
{
public void Dismiss(Document document)
{ } public FailureProcessingResult ProcessFailures(FailuresAccessor failuresAccessor)
{
IList<FailureMessageAccessor> fmas = failuresAccessor.GetFailureMessages();
if (fmas.Count == 0)
{
return FailureProcessingResult.Continue;
} string transactionName = failuresAccessor.GetTransactionName();
if (transactionName.Equals("Error_FailuresProcessor"))
{
foreach (FailureMessageAccessor fma in fmas)
{
FailureDefinitionId id = fma.GetFailureDefinitionId();
if (id == Command.m_idError)
{
failuresAccessor.ResolveFailure(fma);
}
} return FailureProcessingResult.ProceedWithCommit;
}
else
{
return FailureProcessingResult.Continue;
} }
} }

Revit二次开发示例:ErrorHandling的更多相关文章

  1. Revit二次开发示例:HelloRevit

    本示例实现Revit和Revit打开的文件的相关信息. #region Namespaces using System; using System.Collections.Generic; using ...

  2. Revit二次开发示例:EventsMonitor

    在该示例中,插件在Revit启动时弹出事件监控选择界面,供用户设置,也可在添加的Ribbon界面完成设置.当Revit进行相应操作时,弹出窗体会记录事件时间和名称. #region Namespace ...

  3. Revit二次开发示例:ChangesMonitor

    在本示例中,程序监控Revit打开文件事件,并在创建的窗体中更新文件信息.   #region Namespaces using System; using System.Collections.Ge ...

  4. Revit二次开发示例:AutoStamp

    该示例中,在Revit启动时添加打印事件,在打印时向模型添加水印,打印完成后删除该水印.   #region Namespaces using System; using System.Collect ...

  5. Revit二次开发示例:ModelessForm&lowbar;ExternalEvent

    使用Idling事件处理插件任务. #region Namespaces using System; using System.Collections.Generic; using Autodesk. ...

  6. Revit二次开发示例:Journaling

    关于Revit Journal读写的例子.   #region Namespaces using System; using System.Collections.Generic; using Sys ...

  7. Revit二次开发示例:DisableCommand

    Revit API 不支持调用Revit内部命令,但可以用RevitCommandId重写它们(包含任意选项卡,菜单和右键命令).使用RevitCommandId.LookupCommandId()可 ...

  8. Revit二次开发示例:DesignOptions

    本例只要演示Revit的类过滤器的用法,在对话框中显示DesignOption元素. #region Namespaces using System; using System.Collections ...

  9. Revit二次开发示例:DeleteObject

    在本例中,通过命令可以删除选中的元素. 需要注意的是要在代码中加入Transaction,否则的话会出现Modifying  is forbidden because the document has ...

随机推荐

  1. Sql Server系列:Transact-SQL概述

    结构化查询语言(Structure Query Language,SQL)是对数据库进行查询和修改的语言.Transact-SQL是SQL的一种实现形式,它包含了标准的SQL语言部分. 根据完成的具体 ...

  2. Issue 2:Introduction 方法论

    1 清晰的思路 论文投稿完毕,彻底完事儿后,原本很反感的论文书写过程也有了些许心得:论文就是科研工作的文字总结,知识再提炼的过程. 想要写好Introduction,脑子里的一条主线必须清楚. 1.1 ...

  3. 使用ruby过程中遇到安装gem失败的一些通用解决方案

    ruby语言升级还是比较勤快的.但是数量众多的版本使得程序库的兼容性成了大问题.有些gem表示明确不支持某个特定版本以前的ruby,而有些gem则与较高的版本不兼容.再加上gem本身也有版本,简直是乱 ...

  4. 手机下的ev&period;pageX无效

      把  ev.pageX  换成  e.originalEvent.targetTouches[0].pageX;   例子: var start_x, start_y, end_x, end_y, ...

  5. 如何用JS&sol;HTML将时间戳转换为&OpenCurlyDoubleQuote;xx天前”的形式【附源码,转

    如果我们有一份过去时间戳,如何使用JS/HTML将时间戳转换为"xx天前"的形式呢,以下是完整代码 <!DOCTYPE html> <html> <h ...

  6. Android计时器实现

    Wyy.java package com.test; import android.app.Activity;import android.app.Service;import android.os. ...

  7. ASP&period;NET Core 2&period;0 使用支付宝PC网站支付

    前言 最近在使用ASP.NET Core来进行开发,刚好有个接入支付宝支付的需求,百度了一下没找到相关的资料,看了官方的SDK以及Demo都还是.NET Framework的,所以就先根据官方SDK的 ...

  8. Spring AOP 的proxy详解

    spring 提供了多种不同的方案实现对 bean 的 aop proxy, 包括 ProxyFactoryBean, 便利的 TransactionProxyFactoryBean 以及 AutoP ...

  9. &lbrack;Java&rsqb; Thread的start&lpar;&rpar;和run&lpar;&rpar;函数区别

    1.start()方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码: 通过调用Thread类的start()方法来启动一个线程,这时此线程是处于就绪状 ...

  10. java调用ruby代码

    问题: 最近在做一个应用的时候碰到了一个问题.客户端需要调用服务器端传回的脚本信息,然后执行.其中脚本类型包括ruby.而java中调用ruby的代码大致如下: String jrubyCode=&q ...