Revit API: OpenDocumentFile Ignore Error Warning 打开文档之后忽略错误

前言

想要打开 Revit 文件,做一些操作,然后关闭文件,或者批处理一些文件,那么遇到错误对话框怎么办呢?
如何把错误对话框通过 API 关闭?
需要重写这个接口。

revit.Application.Application.FailuresProcessing += Application_FailuresProcessing;

内容

https://forums.autodesk.com/t5/revit-api-forum/open-document-and-ignore-errors/td-p/6726423
官方 forums 有人问了类似的问题,用户需要打开文件,然后导出 IFC,最后关闭文件,但是某些文件出现了错误。
它这里实现了 revit.Application.Application.FailuresProcessing

        private void FaliureProcessor(object sender, FailuresProcessingEventArgs e)
        {
     
            bool hasFailure = false;
            FailuresAccessor fas = e.GetFailuresAccessor();
            List<FailureMessageAccessor> fma = fas.GetFailureMessages().ToList();
            List<ElementId> ElemntsToDelete = new List<ElementId>();
            foreach (FailureMessageAccessor fa in fma)
            {
     
                try
                {
     

                    //use the following lines to delete the warning elements
                    List<ElementId> FailingElementIds = fa.GetFailingElementIds().ToList();
                    ElementId FailingElementId = FailingElementIds[0];
                    if (!ElemntsToDelete.Contains(FailingElementId))
                    {
     
                        ElemntsToDelete.Add(FailingElementId);
                    }

                    hasFailure = true;
                    fas.DeleteWarning(fa);

                }
                catch (Exception ex)
                {
     
                }
            }
            if (ElemntsToDelete.Count > 0)
            {
     
                fas.DeleteElements(ElemntsToDelete);
            }
            //use the following line to disable the message supressor after the external command ends
            //CachedUiApp.Application.FailuresProcessing -= FaliureProcessor;
            if (hasFailure)
            {
     
                e.SetProcessingResult(FailureProcessingResult.ProceedWithCommit);
            }
            e.SetProcessingResult(FailureProcessingResult.Continue);
        }

实际上还可以简化,直接调用 FailuresAccessor.DeleteAllWarnings ,当然,如果需要定制,也可以像上面那些处理。
除了这个,Jeremy 大牛也做过总结:
https://thebuildingcoder.typepad.com/blog/2010/04/failure-api.html

你可能感兴趣的:(Revit,API)