Revit中自定义警告

Revit中自定义警告

想要实现效果:

当创建的墙体长度小于1米弹出警告,提示“you created a false wall”;

第一步在OnStartUp事件中创建自定义警告。

因为在Application.GetFailureDefinitionRegistry()中获取的FailureDefinitionRegistry会在Revit启动后就会加锁,所以没办法在启动以后创建自定义的警告,异常等。

        public Result OnStartup(UIControlledApplication application)
        {
            //可以通过GUID唯一的确定一个异常和警告,等下在用的时候也是通过这个GUID找到,这个FailureDefinition
            var guid = new Guid("{504BD947-0813-48DA-8784-15DE43DA6280}");

            var failureDefinitionId = new FailureDefinitionId(guid);

            var failureDefinition = 			           FailureDefinition.CreateFailureDefinition(failureDefinitionId, FailureSeverity.Warning, "you created a false wall");

            //添加处理器
            failureDefinition.AddResolutionType(FailureResolutionType.CreateElements, "create wall", typeof(DeleteElements));

            return Result.Succeeded;
        }
第二步创建对应的自定义警告预处理类
    public class WallFailurePreProcessor : IFailuresPreprocessor
    {
        public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor)
        {
            var messages = failuresAccessor.GetFailureMessages();
            if (messages.Count() == 0)
            {
                return FailureProcessingResult.Continue;
            }

            var my = messages.FirstOrDefault(x => x.GetSeverity() == FailureSeverity.Warning);
            var ids = my.GetAdditionalElementIds();
            if (ids.Count() == 1)
            {
                var wall = failuresAccessor.GetDocument().GetElement(ids.FirstOrDefault()) as Wall;
                if (wall != null)
                {
                    var curve = (wall.Location as LocationCurve).Curve;
                    if (curve.Length < 1000 / 304.8)
                    {
                        //当创建的是墙体,并长度小于1000mm时弹出警告
                        return FailureProcessingResult.Continue;
                    }
                }
            }
            return FailureProcessingResult.ProceedWithCommit;
        }
    }
第三 创建墙时post这个警告,通过自定义的警告预处理类WallFailurePreProcessor判断是否需要弹出警告
        private void FailureOption()
        {
            var registry = Application.GetFailureDefinitionRegistry();

            //跟之前注册的GUID保持一致
            var guid = new Guid("{504BD947-0813-48DA-8784-15DE43DA6280}");

            var failureDefinitionId = new FailureDefinitionId(guid);

            var failureMessage = new FailureMessage(failureDefinitionId);

            using (Transaction ts = new Transaction(_doc))
            {
                var options = ts.GetFailureHandlingOptions();

                var preProcessor = new WallFailurePreProcessor();
                options.SetFailuresPreprocessor(preProcessor);

                ts.SetFailureHandlingOptions(options);

                ts.Start("test");

                var shortWall = CreateWall(800);
  				 var longWall = CreateWall(2000);
                
                failureMessage.SetAdditionalElement(shortWall.Id);
                _doc.PostFailure(failureMessage);
                ts.Commit();
            }
        }

        private Element CreateWall(double length)
        {
            var curve = Line.CreateBound(XYZ.Zero, new XYZ(length / 304.8, 0, 0));
            var level = new FilteredElementCollector(_doc).OfCategory(BuiltInCategory.OST_Levels)
                                                        .OfClass(typeof(Level))
                                                        .Cast<Level>()
                                                        .FirstOrDefault();

            return Wall.Create(_doc, curve, level.Id, false);
        }

效果:

当调用shortWall时会弹出警告
在这里插入图片描述

当调用longWall时不会弹出警告

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值