Creating a rebar U-shape definition by RebarShapeDefinitionBySegments

Creating a rebar U-shape definition by RebarShapeDefinitionBySegments

vhh7Z3D2
Contributor Contributor
288 Views
2 Replies
Message 1 of 3

Creating a rebar U-shape definition by RebarShapeDefinitionBySegments

vhh7Z3D2
Contributor
Contributor

I am trying to create a simple u-shape definition of a rebar using the 

RebarShapeDefinitionBySegments 

 class, however I cannot get it to work even though I set it up exactly like Revit does when the same shape is created in the UI and then inspected. The shape has complete = true and creates it, however it cannot be opened for editing afterwards, which indicates something is wrong with either the default value of the parameters or the constraints. I am wondering what exactly I am doing wrong.

 

To make a u-shape, I define 3 segments with the following constraints:

 

0: Length, FixedDir (1,0)

1: Length, FixedDir (0,1), 180degreeDefaultBend

2: Length, FixedDir (-1,0)

 

(To use 3 length parameters named a, b and c must exist but otherwise code should be able to be used as is )

 

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

            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;
        }

 

and to use:

 

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

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

                rebarShape2.Name = shapeName;

                transaction.Commit();
            }
        }

 

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

Radwan-Almsora
Enthusiast
Enthusiast

UI, you need to fix how the segments are constrained to each other and correct the bend indices.The main issue preventing editing is that you over-constrained the directions using FixedSegmentDir on every segment instead of using relative turn constraints. Additionally, a 3-segment shape only has internal bends, so declaring 4 bend radii corrupts the shape definition structure The Corrected CodeReplace your current definition method with this clean, fully editable setup:

 

private static RebarShapeDefinition CreateShapeDefinitionTest_U_shape(Document document)
{
    // 3 segments mean 3 lines and 2 internal corners (bends)
    var definition = new RebarShapeDefinitionBySegments(document, 3);
    
    definition.AddParameter(document.GetParameterId("a"), 200d.FromMillimeters());
    definition.AddParameter(document.GetParameterId("b"), 200d.FromMillimeters());
    definition.AddParameter(document.GetParameterId("c"), 200d.FromMillimeters());

    // --- 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)) // Base orientation anchor
    };
    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),
        // Tells Revit this segment turns 90 degrees left relative to segment 0
        new RebarShapeConstraintCircularity(RebarShapeVertexTurn.Left) 
    };
    segment1.SetConstraints(segment1constraints);

    // --- Segment 2 (Third Leg) ---
    var segment2 = definition.GetSegment(2);
    List<RebarShapeConstraint> segment2constraints = new List<RebarShapeConstraint>()
    {
        new RebarShapeConstraintSegmentLength(
            document.GetParameterId("c"), 
            RebarShapeSegmentEndReferenceType.Exterior, 
            RebarShapeSegmentEndReferenceType.Exterior),
        // Tells Revit this segment turns 90 degrees left relative to segment 1
        new RebarShapeConstraintCircularity(RebarShapeVertexTurn.Left)
    };
    segment2.SetConstraints(segment2constraints);

    // --- Define Bend Radii ---
    // A 3-segment shape has only 2 vertices (index 0 and 1). 
    definition.AddBendDefaultRadius(0, RebarShapeVertexTurn.Left, RebarShapeBendAngle.Right);
    definition.AddBendDefaultRadius(1, RebarShapeVertexTurn.Left, RebarShapeBendAngle.Right);

    return definition;
}

 Did you find this post helpful? If it gave you one or more solutions,

don't forget to click the Accept Solution button and leave a < like !

 

 

@Radwan-Almsora -- this post has been edited due to Community Rules & Etiquette violation 

0 Likes
Message 3 of 3

vhh7Z3D2
Contributor
Contributor

This looks like an AI answer, and it is incorrect. There is no RebarShapeConstraintCircularity class. Also there is 4 vertexes in a 3 segment shape, but you don't have to specify the first and last vertex radii as they always have the same values, but you do have to specify the ones in between. A 180 degree bend is a special case where the bend is created by a constraint on the segment itself, as otherwise the lines would overlap. For every other type of angle, it is created by adding a constraint to the vertex.

 

0 Likes