Dimension text position/ how can I rotate a vector using the API?

Ive written a script that repositions text on a dimension line when texts overlaps. The script is not finished yet. I put the current status underneath. It currently only works for horizontal dimension lines and it can be easily addapted for vertical dimension lines. But I also want to get it working for dimension lines with an angle other than 0 or 90 degrees. To do this within one script I need to rotate a vector using an API command and then move a point using this rotated vector. I know how to do it in designscript but this doesnt work when the Revit API is imported.
So my question is:

how can I rotate a vector and move a point in the direction of this vector? using the API

The script is attached.

Automatisch verplaatsen tekst maatlijn.dyn (12.3 KB)

To rotate a vector instantiate a transform using Autodesk.Revit.DB.Transform.CreateRotation(Autodesk.Revit.DB.XYZ axis, double angle) and call the OfVector() method on it (passing your vector into it).

Then to transform the point, use Autodesk.Revit.DB.Transform CreateTranslation(Autodesk.Revit.DB.XYZ vector) where the vector input is the rotated vector obtained from the previous step and call OfPoint() on this transform passing in your point to rotate.

Thanks!

Hi,
Your sollution seems clear but Im still running into a problem. Could you give an example of what I should enter in the part Autodesk.Revit.DB.XYZ axis? Ive tried to enter a vector, a line but it doesn`t accept this?
Wouter Hilhorst

You need to provide an XYZ (which is either a point or a vector depending on the context in the Revit API). This vector should be the normal of the imaginary plane which your dimension/text is on - the simplest way to get this vector would be to get the host view of the dimension then the ViewDirection from the view. Alternatively, if you have a Line get its direction: myLine.Direction and use that instead assuming its valid.

Hi Thomas,

I translated the direction of a dimension line into a vector:

Vector = Selected_Dimension.Curve.Direction.ToVector()

This Vector is in the XY plane, so the rotation axis should be a vector in Z direction

Then I try to Rotate it using a Z axis.

Line.CreateBound(XYZ(0,0,0), XYZ(0,0,1))

Rotated_Vector = Autodesk.Revit.DB.Transform.CreateRotation(Axis.Direction, math.radians®).OfVector(Vector)

I also just tried:

Autodesk.Revit.DB.Transform.CreateRotation(XYZ(0,0,1), math.radians®).OfVector(Vector)

But it gives an error:

expected XYZ, got Vector

Any ideas?

When you call ToVector() on an XYZ you are converting it into a ProtoGeometry Vector (a Dynamo vector) which is not the required type. I always advise not go anywhere near the ProtoGeometry or anything from Dynamo’s libraries (RevitNodes, etc) when you are in the Revit API context (unless you expressly need to) as its slower for a myriad of reasons.

Yes, that solved the problem. I didn`t know that the addition ToVector() makes it a Dynamo type.

I removed it and now it works fine.

Thanks again for your help.

1 Like