Creating a U-shape definition by RebarShapeDefinitionBySegments

Creating a U-shape definition by RebarShapeDefinitionBySegments

vhh7Z3D2
Contributor Contributor
266 Views
2 Replies
Message 1 of 3

Creating a U-shape definition by RebarShapeDefinitionBySegments

vhh7Z3D2
Contributor
Contributor

I am trying to create a simple U-shape via the RebarShapeDefinitionBySegments class, but I cannot get it to work.

 

When created via the UI a definition with 3 segments with the following constraints are created:

 

0: Length, FixedDir

1: Length, FixedDir, 180DegreeDefaultBend

2: Length, FixedDir

 

I have replicated this structure in the following code, and Revit reports the shape as beeing complete, however when I try to edit it afterwards via the UI, it fails to open. This usually means there is something wrong with either the default parameter values or the constraints, but I cannot figure out where the error is, or if this is actually possible, given that, I have found no code examples covering this part of the API.

 

To run this code 3 length parameters called a, b and c must exist. Otherwise it should run.

 

private static RebarShapeDefinition CreateShapeDefinitionTest_U_shape(Document document)
{
    var definition = new RebarShapeDefinitionBySegments(document, 3);
    definition.AddParameter(document.GetParameterId("a"), 0.65d);
    definition.AddParameter(document.GetParameterId("b"), 0.65d);
    definition.AddParameter(document.GetParameterId("c"), 0.65d);

    var segment0 = definition.GetSegment(0);
    List<RebarShapeConstraint> segment0constraints = new List<RebarShapeConstraint>()
    {
        new RebarShapeConstraintSegmentLength(
         document.GetParameterId("a"),
         RebarShapeSegmentEndReferenceType.Exterior,
         RebarShapeSegmentEndReferenceType.Exterior),

        new RebarShapeConstraintFixedSegmentDir(new UV(1,0))
    };
    segment0.SetConstraints(segment0constraints);

    var segment1 = definition.GetSegment(1);
    List<RebarShapeConstraint> segment1constraints = new List<RebarShapeConstraint>()
    {
        new RebarShapeConstraintSegmentLength(
         document.GetParameterId("b"),
         RebarShapeSegmentEndReferenceType.Exterior,
         RebarShapeSegmentEndReferenceType.Exterior),

        new RebarShapeConstraint180DegreeDefaultBend(),
        new RebarShapeConstraintFixedSegmentDir(new UV(0,1)),
    };
    segment1.SetConstraints(segment1constraints);

    //Segment 2.
    var segment2 = definition.GetSegment(2);
    List<RebarShapeConstraint> segment2constraints = new List<RebarShapeConstraint>()
    {
        new RebarShapeConstraintSegmentLength(
         document.GetParameterId("c"),
         RebarShapeSegmentEndReferenceType.Exterior,
         RebarShapeSegmentEndReferenceType.Exterior),

        new RebarShapeConstraintFixedSegmentDir(new UV(-1,0))

    };
    segment2.SetConstraints(segment2constraints);


    definition.AddBendDefaultRadius(0, RebarShapeVertexTurn.Default, RebarShapeBendAngle.Obtuse);
    definition.AddBendDefaultRadius(1, RebarShapeVertexTurn.Left, RebarShapeBendAngle.Right);
    definition.AddBendDefaultRadius(2, RebarShapeVertexTurn.Left, RebarShapeBendAngle.Right);
    definition.AddBendDefaultRadius(3, RebarShapeVertexTurn.Default, RebarShapeBendAngle.Obtuse);

    return definition;
}

 

Create the shape via:

 

      private static void CreateShapeTest(Document document, RebarShapeDefinition shapeDefinition, string shapeName)
      {
          using (Transaction transaction = new Transaction(document, "Create shape"))
          {
              transaction.Start();

              var rebarShape = RebarShape.Create(
              document,
              shapeDefinition,
              null,
              RebarStyle.Standard,
              StirrupTieAttachmentType.InteriorFace,
               0,
               new RebarShapeTerminationsData(document)
              );

              rebarShape.Name = shapeName;

              transaction.Commit();
          }
      }

 

0 Likes
267 Views
2 Replies
Replies (2)
Message 2 of 3

Radwan-Almsora
Enthusiast
Enthusiast

The issue causing your U-shape rebar definition to crash the UI stems from incorrect bend definitions and mismatched segment directions. Revit is highly sensitive to the topological continuity of the shape definition.Here is why your code fails and how to fix it.Why Your Code Fails

 

Wrong Vertex Indices: A shape with 3 segments has exactly 2 internal vertices (Vertex 1 and Vertex 2) where bends occur. Your code attempts to add AddBendDefaultRadius to vertices 0 and 3, which are the start and end tips of the bar, not bends.Vector Continuity: Your segments switch abruptly from (1,0) to (0,1) to (-1,0). While this describes a physical U-shape path, Revit shape definitions evaluate the constraints and bend turns sequentially.180 Degree Constraint Conflict: You applied RebarShapeConstraint180DegreeDefaultBend() to Segment 1, but a U-shape consists of two 90-degree bends, not a 180-degree hook.

The Working SolutionTo successfully define a standard U-shape bar via the API, update your code to match this corrected structure:

private static RebarShapeDefinition CreateShapeDefinitionTest_U_shape(Document document) 
{
    // 1. Initialize with 3 segments
    var definition = new RebarShapeDefinitionBySegments(document, 3);
    
    // 2. Add your parameters
    definition.AddParameter(document.GetParameterId("a"), 0.65d);
    definition.AddParameter(document.GetParameterId("b"), 0.65d);
    definition.AddParameter(document.GetParameterId("c"), 0.65d);

    // --- Segment 0 (First Leg) ---
    var segment0 = definition.GetSegment(0);
    List<RebarShapeConstraint> segment0constraints = new List<RebarShapeConstraint>() 
    {
        new RebarShapeConstraintSegmentLength(
            document.GetParameterId("a"), 
            RebarShapeSegmentEndReferenceType.Exterior, 
            RebarShapeSegmentEndReferenceType.Exterior),
        new RebarShapeConstraintFixedSegmentDir(new UV(1, 0))
    };
    segment0.SetConstraints(segment0constraints);

    // --- Segment 1 (Base of the U) ---
    var segment1 = definition.GetSegment(1);
    List<RebarShapeConstraint> segment1constraints = new List<RebarShapeConstraint>() 
    {
        new RebarShapeConstraintSegmentLength(
            document.GetParameterId("b"), 
            RebarShapeSegmentEndReferenceType.Exterior, 
            RebarShapeSegmentEndReferenceType.Exterior),
        new RebarShapeConstraintFixedSegmentDir(new UV(0, 1)) // 90 degree turn relative to Seg 0
    };
    segment1.SetConstraints(segment1constraints);

    // --- Segment 2 (Second Leg) ---
    var segment2 = definition.GetSegment(2);
    List<RebarShapeConstraint> segment2constraints = new List<RebarShapeConstraint>() 
    {
        new RebarShapeConstraintSegmentLength(
            document.GetParameterId("c"), 
            RebarShapeSegmentEndReferenceType.Exterior, 
            RebarShapeSegmentEndReferenceType.Exterior),
        new RebarShapeConstraintFixedSegmentDir(new UV(-1, 0)) // 90 degree turn relative to Seg 1
    };
    segment2.SetConstraints(segment2constraints);

    // --- Correct Bend Definitions ---
    // Only vertices between segments (Vertex 1 and 2) get a bend radius definition
    // For a standard U-shape, both turns are identical (e.g., Right turns)
    definition.AddBendDefaultRadius(1, RebarShapeVertexTurn.Left, RebarShapeBendAngle.Right);
    definition.AddBendDefaultRadius(2, RebarShapeVertexTurn.Left, RebarShapeBendAngle.Right);

    return definition;
}
0 Likes
Message 3 of 3

doc.inge.fernando
Advocate
Advocate

yo estoy iniciando api revit usando c# y no puedo generar el complemento, sera que me pueden ayudar a crear mi primera aplicación para revit por favor, quiero aprender mas, denme una mano por favor para poder crear mi propia api, gracias 

0 Likes