Message 1 of 13
API Constraint management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello all,
I am trying to create a simple family with some defined constraints, however I don't get the same results like doing the process manually in Revit interface.
Here is the code:
private void CreateContents()
{
var view = m_context.GetView();
var width = 1000.0;
// step 0, create parameters
var parameterWidth = m_context.Document.FamilyManager.AddParameter("Width", BuiltInParameterGroup.INVALID, ParameterType.Length, true);
var parameterAlpha = m_context.Document.FamilyManager.AddParameter("Alpha", BuiltInParameterGroup.INVALID, ParameterType.Angle, true);
{
var view = m_context.GetView();
var width = 1000.0;
// step 0, create parameters
var parameterWidth = m_context.Document.FamilyManager.AddParameter("Width", BuiltInParameterGroup.INVALID, ParameterType.Length, true);
var parameterAlpha = m_context.Document.FamilyManager.AddParameter("Alpha", BuiltInParameterGroup.INVALID, ParameterType.Angle, true);
Trace.TraceInformation("Parameter Width " + parameterWidth.Id);
Trace.TraceInformation("Parameter Alpha " + parameterAlpha.Id);
Trace.TraceInformation("Parameter Alpha " + parameterAlpha.Id);
// step 1, create reference plane
var bubbleEnd = new XYZ(width, width, 0);
var freeEnd = new XYZ(-width/2, width, 0);
var widthReferencePlane = m_context.Factory.NewReferencePlane(Conversion.ToImperial(bubbleEnd), Conversion.ToImperial(freeEnd), XYZ.BasisZ, view);
widthReferencePlane.Name = "OuterAxis";
var bubbleEnd = new XYZ(width, width, 0);
var freeEnd = new XYZ(-width/2, width, 0);
var widthReferencePlane = m_context.Factory.NewReferencePlane(Conversion.ToImperial(bubbleEnd), Conversion.ToImperial(freeEnd), XYZ.BasisZ, view);
widthReferencePlane.Name = "OuterAxis";
// step 2, create cut line
var cutA = XYZ.Zero;
var cutB = new XYZ(width, width, 0);
var cutLine = Line.CreateBound(Conversion.ToImperial(cutA), Conversion.ToImperial(cutB));
var cutReferenceLine = m_context.Factory.NewModelCurve(cutLine, m_context.SketchPlane) as ModelLine;
if (cutReferenceLine == null)
return;
cutReferenceLine.ChangeToReferenceLine();
var cutA = XYZ.Zero;
var cutB = new XYZ(width, width, 0);
var cutLine = Line.CreateBound(Conversion.ToImperial(cutA), Conversion.ToImperial(cutB));
var cutReferenceLine = m_context.Factory.NewModelCurve(cutLine, m_context.SketchPlane) as ModelLine;
if (cutReferenceLine == null)
return;
cutReferenceLine.ChangeToReferenceLine();
m_context.Document.Regenerate();
// paso 3, get generic family Reference Planes
var horzReferencePlane = m_context.GetElement<ReferencePlane>(@"Center (Front/Back)");
var vertReferencePlane = m_context.GetElement<ReferencePlane>(@"Center (Left/Right)");
var horzReferencePlane = m_context.GetElement<ReferencePlane>(@"Center (Front/Back)");
var vertReferencePlane = m_context.GetElement<ReferencePlane>(@"Center (Left/Right)");
if (horzReferencePlane == null || vertReferencePlane == null)
return;
return;
// step 4: get references
var axis = horzReferencePlane.GetReference();
var side = vertReferencePlane.GetReference();
var thickness = widthReferencePlane.GetReference();
var side = vertReferencePlane.GetReference();
var thickness = widthReferencePlane.GetReference();
var cut = cutReferenceLine.GeometryCurve.Reference;
var start = cutReferenceLine.GeometryCurve.GetEndPointReference(0);
var end = cutReferenceLine.GeometryCurve.GetEndPointReference(1);
var start = cutReferenceLine.GeometryCurve.GetEndPointReference(0);
var end = cutReferenceLine.GeometryCurve.GetEndPointReference(1);
// step 7, create constraints
var constraint1 = m_context.Factory.NewAlignment(view, axis, start);
var constraint2 = m_context.Factory.NewAlignment(view, side, start);
var constraint3 = m_context.Factory.NewAlignment(view, thickness, end);
var constraint1 = m_context.Factory.NewAlignment(view, axis, start);
var constraint2 = m_context.Factory.NewAlignment(view, side, start);
var constraint3 = m_context.Factory.NewAlignment(view, thickness, end);
Trace.TraceInformation("Contraint axis_start " + constraint1.Id);
Trace.TraceInformation("Contraint side_start " + constraint2.Id);
Trace.TraceInformation("Contraint thickness_end " + constraint3.Id);
Trace.TraceInformation("Contraint side_start " + constraint2.Id);
Trace.TraceInformation("Contraint thickness_end " + constraint3.Id);
// step 8, create thickness dimension
var thicknessA = new XYZ(-width/2.0,0,0);
var thicknessB = new XYZ(-width/2.0, width, 0);
var thicknessLine = Line.CreateBound(Conversion.ToImperial(thicknessA), Conversion.ToImperial(thicknessB));
var references = new ReferenceArray();
references.Append(axis);
references.Append(thickness);
var thicknessA = new XYZ(-width/2.0,0,0);
var thicknessB = new XYZ(-width/2.0, width, 0);
var thicknessLine = Line.CreateBound(Conversion.ToImperial(thicknessA), Conversion.ToImperial(thicknessB));
var references = new ReferenceArray();
references.Append(axis);
references.Append(thickness);
var thicknessDimension = m_context.Factory.NewLinearDimension(view, thicknessLine, references);
if (thicknessDimension == null)
return;
thicknessDimension.FamilyLabel = parameterWidth;
if (thicknessDimension == null)
return;
thicknessDimension.FamilyLabel = parameterWidth;
Trace.TraceInformation("Thickness Dimension " + thicknessDimension.Id);
// step 8, create angular dimension
var plane = new Plane(XYZ.BasisZ, XYZ.Zero);
var arc = Arc.Create(plane, Conversion.ToImperial(width), 0, Math.PI/4.0);
m_context.Document.Regenerate();
var angularDimension = m_context.Factory.NewAngularDimension(view, arc, cut, axis);
Trace.TraceInformation("Angular Dimension " + angularDimension.Id);
Trace.TraceInformation("Angular Dimension " + angularDimension.Id);
m_context.Document.Regenerate();
angularDimension.FamilyLabel = parameterAlpha;
}
}
When I edit the family created by this code and remove the angular dimenions then recreate it, it works fine, but with the dimension created through the API I get a "Constraint not satisfied" message.
Somehow I suspect about some interaction between the undefined parameter value and the arc creation process, but I am stuck after trying several combinations.
In the attachment I place the full source code, the created family, and the created family, once fixed manually from the Revit interface.
José.