下载app免费领取会员
在Revit 里很多操作都会弹出警告和错误提示,
比如墙的高度降低,墙顶部的窗出现在墙的外面
这个会弹一个错误提示框,
比如在同一个位置创建两面墙,
会弹出一个警告提示框
如果在自己写的程序里出现了这种弹窗,会影响到用户体验,
其实API里提供一些错误和警告的处理,
下面提供一个例子解决上面的两种情况,
关键代码如下:
public class FailuresPreprocessor : IFailuresPreprocessor
{
public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor)
{
IList<FailureMessageAccessor> listFma =failuresAccessor.GetFailureMessages();
if (listFma.Count == 0)
return FailureProcessingResult.Continue;
foreach (FailureMessageAccessor fma in listFma)
{
if (fma.GetSeverity() == FailureSeverity.Error)
{
if (fma.HasResolutions())
failuresAccessor.ResolveFailure(fma);
}
if (fma.GetSeverity() == FailureSeverity.Warning)
{
failuresAccessor.DeleteWarning(fma);
}
}
return FailureProcessingResult.ProceedWithCommit;
}
}
bool testError = true;
// 处理错误
if (testError)
{
Wall wall = doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element)) as Wall;
Transaction trans = new Transaction(doc, "test");
trans.Start();
FailureHandlingOptions fho = trans.GetFailureHandlingOptions();
fho.SetFailuresPreprocessor(new FailuresPreprocessor());
trans.SetFailureHandlingOptions(fho);
Parameter p = wall.LookupParameter("无连接高度");
double h = p.AsDouble();
p.Set(h / 2);
trans.Commit();
}
// 处理警告
else
{
FilteredElementCollector lvlFilter = new FilteredElementCollector(doc);
lvlFilter.OfClass(typeof(Level));
Level lvl = lvlFilter.First() as Level;
Transaction trans1 = new Transaction(doc, "wall");
FailureHandlingOptions fho1 = trans1.GetFailureHandlingOptions();
fho1.SetFailuresPreprocessor(new FailuresPreprocessor());
trans1.SetFailureHandlingOptions(fho1);
trans1.Start();
Line line = Line.CreateBound(new XYZ(), new XYZ(10, 0, 0));
Wall.Create(doc, line, lvl.Id, false);
Wall.Create(doc, line, lvl.Id, false);
trans1.Commit();
}
return Result.Succeeded;
本文版权归腿腿教学网及原创作者所有,未经授权,谢绝转载。