Often we receive questions with code snippet that seems to be doing everything perfectly and yet the result is not what is expected – especially if the code modifies the Revit model in some ways. And in most cases, it can be simply resolved by either regenerating the model after making some modifications (including adding new elements to the model) or encapsulating the changes in an additional transaction.
Like in this following example, the code perfectly creates the reference planes but throws the following error while creating a simple linear dimension between the newly created reference planes. The exception mentions:
One of the conditions for the inputs was not satisfied. Consult the documentation for requirements for each argument.
Here is the code that I am referring to:
using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Revit.SDK.Samples.HelloRevit.CS
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiApp = commandData.Application;
Document doc = uiApp.ActiveUIDocument.Document;
using (Transaction trans = new Transaction(doc, "Dimension"))
{
trans.Start();
// Create Plane 1
XYZ freeEnd1 = new XYZ(1, 10, 0);
XYZ bubbleEnd1 = new XYZ(1, -10, 0);
ReferencePlane refPlane1 =
doc.FamilyCreate.NewReferencePlane(
bubbleEnd1,
freeEnd1,
XYZ.BasisZ,
doc.ActiveView);
// Create Plane 2
XYZ freeEnd2 = new XYZ(5, 10, 0);
XYZ bubbleEnd2 = new XYZ(5, -10, 0);
ReferencePlane refPlane2 =
doc.FamilyCreate.NewReferencePlane(
bubbleEnd2,
freeEnd2,
XYZ.BasisZ,
doc.ActiveView);
// Create dim line
XYZ dimPt1 = refPlane1.FreeEnd;
XYZ dimPt2 = refPlane2.FreeEnd;
Line dimline =
doc.Application.Create.NewLine(dimPt1, dimPt2, true);
// Create the Dimension
ReferenceArray ra = new ReferenceArray();
ra.Append(refPlane1.Reference);
ra.Append(refPlane2.Reference);
Dimension dim =
doc.FamilyCreate.NewLinearDimension(
doc.ActiveView,
dimline,
ra);
FamilyParameter param =
doc.FamilyManager.AddParameter(
"width",
BuiltInParameterGroup.PG_CONSTRAINTS,
ParameterType.Length,
false);
dim.Label = param;
trans.Commit();
}
return Result.Succeeded;
}
}
}
Based on the description provided in the initial part of this post, the simple way to avoid the exception and create the linear dimension is to regenerate the model after the reference planes and the dimension line has been created. This can be done by adding this line to the code above (after NewLine):
doc.Regenerate();