Dimension direction changed unexpectedly.

Dimension direction changed unexpectedly.

phantbinh99
Enthusiast Enthusiast
1,152 Views
17 Replies
Message 1 of 18

Dimension direction changed unexpectedly.

phantbinh99
Enthusiast
Enthusiast

Hi everyone,
I have an issue with the dimension direction changing automatically.

I've created a dimension from point to point (with the dimension line being vertical), but when I change the TextPosition or LeaderEndPosition property, the dimension direction changes to point-to-point.

phantbinh99_0-1733990458930.png

 

This error only happens with dimensions created using the Revit API.

Does anyone know about this or have any ideas?

0 Likes
1,153 Views
17 Replies
Replies (17)
Message 2 of 18

harilalmnCBXDY
Enthusiast
Enthusiast

Can you please share the code?

0 Likes
Message 3 of 18

phantbinh99
Enthusiast
Enthusiast

```

var airTer1 = Document.GetElement(airTerRef1) as FamilyInstance;
var airTer2 = Document.GetElement(airTerRef2) as FamilyInstance;

var ops = new Options();
ops.IncludeNonVisibleObjects = true;
ops.ComputeReferences = true;

var airRef1 = airTer1.get_Geometry(ops).First(x => x is Point) as Point;
var airRef2 = airTer2.get_Geometry(ops).First(x => x is Point) as Point;

var startPoint = airRef1.Coord.Project(ActiveView.GetPlane());
var endPoint = airRef2;

var refArray = new ReferenceArray();
refArray.Append(airRef1.Reference);
refArray.Append(airRef2.Reference);


Dimension dim;

using (Transaction t = new Transaction(Document, "Create Dim"))
{
t.Start();

dim = Document.Create.NewDimension(ActiveView, Line.CreateUnbound(startPoint, XYZ.BasisY), refArray);

t.Commit();
}
```

It simply gets the Reference from the point of the element and dims them together. The dimension line would be created from one point to the basis Y.

0 Likes
Message 4 of 18

harilalmnCBXDY
Enthusiast
Enthusiast
Can you please share the family too?
0 Likes
Message 5 of 18

phantbinh99
Enthusiast
Enthusiast

Yes, here is the model.
Do you have any ideas?

0 Likes
Message 6 of 18

harilalmnCBXDY
Enthusiast
Enthusiast

Try this;

public void Run()
{
    try
    {
        List<Reference> fis = new List<Reference>();
        
        Reference ref1 = uidoc.Selection.PickObject(ObjectType.Element, "item1");
        Element element = doc.GetElement(ref1);
        fis.Add((element as FamilyInstance).GetReferences(FamilyInstanceReferenceType.CenterFrontBack).FirstOrDefault());
        
        Reference ref2 = uidoc.Selection.PickObject(ObjectType.Element, "item2");
        Element element2 = doc.GetElement(ref2);
        fis.Add((element2 as FamilyInstance).GetReferences(FamilyInstanceReferenceType.CenterFrontBack).FirstOrDefault());
        
        View view = uidoc.ActiveView;
        
        FamilyInstance fi1 = doc.GetElement(fis[0]) as FamilyInstance;
        FamilyInstance fi2 = doc.GetElement(fis[1]) as FamilyInstance;
        
        LocationPoint loc1 = fi1.Location as LocationPoint;
        LocationPoint loc2 = fi2.Location as LocationPoint;
        
        XYZ point1 = loc1.Point;
        XYZ pointt2 = loc2.Point;
        
        point1 = new XYZ(point1.X, point1.Y, 0);
        pointt2 = new XYZ(pointt2.X, pointt2.Y, 0);
        
        using (Transaction tx = new Transaction(doc, "create dimension"))
        {
            tx.Start();
            
            ReferenceArray refArray = new ReferenceArray();
            refArray.Append(fis[0]);
            refArray.Append(fis[1]);
            
            XYZ dimPoint1 = new XYZ(point1.X + 2, point1.Y + 1, point1.Z);
            XYZ dimPoint2 = new XYZ(point1.X + 2, pointt2.Y + 1, pointt2.Z);
            
            Line dimLine = Line.CreateBound(dimPoint1, dimPoint2);
            Dimension dim = doc.Create.NewDimension(view, dimLine, refArray);
            
            TaskDialog.Show("Creating Dimension", "Dimension created successfully");
            TaskDialog.Show("Moving Simension", "Move the text from middle of the dimension line to the top of the dimension line");
            
            XYZ textPosition = dim.TextPosition;
            textPosition = new XYZ(textPosition.X, textPosition.Y - 1, textPosition.Z);
            dim.HasLeader = false;
            dim.TextPosition = textPosition;
            
            tx.Commit();
        }
    }
    catch (Exception e)
    {
        TaskDialog.Show("Error", e.Message);
    }
}
0 Likes
Message 7 of 18

phantbinh99
Enthusiast
Enthusiast

It seems like they don't have any references at CenterFrontBack.
I get null from GetReferences(FamilyInstanceReferenceType.CenterFrontBack).

0 Likes
Message 8 of 18

harilalmnCBXDY
Enthusiast
Enthusiast

True. The family you shared, though has two reference planes named as Center(Front/Back) and Center (Left/Right), they are not set in properties. Set it 

0 Likes
Message 9 of 18

phantbinh99
Enthusiast
Enthusiast

It seems like we have some misunderstanding here.

I can create a dimension from the two elements above, but when I change the TextPosition property, the dimension direction also changes.
How can I prevent the dimension direction from changing when I modify the TextPosition property?
That is what I want to ask everyone about.

0 Likes
Message 10 of 18

harilalmnCBXDY
Enthusiast
Enthusiast

The code I shared works perfectly fine. I don't know what is it in your case. 

 

In the code I am creating a dimension between the two instances. Then move the text towards the end of the dimension line.  It doesn't change anything else.

0 Likes
Message 11 of 18

phantbinh99
Enthusiast
Enthusiast

The key point of the problem is point-to-point dimensioning.
Did you create dimensions from point to point in your model?

0 Likes
Message 12 of 18

harilalmnCBXDY
Enthusiast
Enthusiast

May be this will throw some light. The method GetFamilyInstancePointReference

Check out

The Building Coder: Picking Pairs and Dimensioning Family Instance Origin

0 Likes
Message 13 of 18

phantbinh99
Enthusiast
Enthusiast

Hi, thank you. I checked it out
But sadly, it doesn't have any information about the Dimension Direction from point to point.
It only has information about how to create a Dimension from point to point, which I can already do.

The method GetFamilyInstancePointReference is just getting a point, which is the same as what I am doing. It only ensures that the point has a reference before retrieving it.

0 Likes
Message 14 of 18

harilalmnCBXDY
Enthusiast
Enthusiast

Direction should be;

 

(instance2.Location as LocationPoint).Point - (instance1.Location as LocationPoint).Point

 

Right..?

0 Likes
Message 15 of 18

phantbinh99
Enthusiast
Enthusiast

No, the direction should be vertical based on line provided

Line.CreateUnbound(startPoint, XYZ.BasisY)

0 Likes
Message 16 of 18

harilalmnCBXDY
Enthusiast
Enthusiast

Hmm.. I too got the dimension done from point to point, but the direction is automatically changing to match the direction from point1 to point2. Strange.  Anyway,if you find a solution, kindly share here.  Eager to learn.

0 Likes
Message 17 of 18

Moustafa_K
Collaborator
Collaborator

your code is fine only one point might help, you need to commit the Transation before changing text position. then open another one for the changing the text.

 

if you are keen to keep the dimension in a vertical Direction, you can use the create linear dimension

LinearDimension.Create(Document document, View dbView, Line line, IList<Reference> references)

but that is only available since Revit 2025.

 

for older Revit versions, I used to initialize the DimensionShape value using reflection (which I don't advise, but might help for a certain case)

 Trans.Start();

 Line dimLine = Line.CreateBound(p1.GlobalPoint, p2.GlobalPoint);

 Dimension dim = Doc.Create.NewDimension(UiDoc.ActiveGraphicalView, dimLine, refArry);
 var field = typeof(Dimension).GetField("m_DimensionShape", BindingFlags.NonPublic | BindingFlags.Instance);
 field.SetValue(dim, DimensionShape.Linear);
 Trans.Commit();

// start a new transcation, and change the text position

 

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
Message 18 of 18

phantbinh99
Enthusiast
Enthusiast

Thank you, but your old version didn’t work for me. Actually, I used a trick: adding another reference point (three points will create a straight line) and removing it later. This way, the dimension will keep its direction and won’t change anymore.