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时不会弹出警告