Hello @Anonymous this is not technically an API question but it can be applied and automated to a certain extent with a macro.
If you open the Macro Manager (Manage > Macros) then create a new Module (Application Tab) with language set to VB.NET. When SharpDevelop opens replace the entire contents of ThisApplication.vb with the below. Then Build the macro (Build > Build Solution). Close SharpDevelop. Then back within the Macro Manager select the tree branch of 'CentreDimValuesOnLine' and press run for each view required. If the view scale changes in a view you'll have to run the macro again.
Option Strict off
Imports System
Imports Autodesk.Revit.UI
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.UI.Selection
Imports System.Collections.Generic
Imports System.Linq
<Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)> _
<Autodesk.Revit.DB.Macros.AddInId("EF4F27C9-EE2D-410E-B676-F3600AA5F812")> _
Partial Public Class ThisApplication
Private Sub UpdateDimText(Doc As Document, Dm As Object, TxtH As Double, GFactor As Double)
Dm.ResetTextPosition()
Doc.Regenerate()
Dim SegOrig As XYZ = Dm.Origin 'Centre of dim line between extension lines
Dim TextPos As XYZ = Dm.TextPosition
Dim S1 As XYZ = SegOrig - TextPos
Dim S1n As XYZ = S1.Normalize
Dim Shift As XYZ = TextPos + S1 + (S1n * (TxtH / 2)) + (S1n * GFactor)
Dm.TextPosition = Shift
End Sub
Public Sub CentreDimValuesOnLine
If me.ActiveUIDocument Is Nothing Then Exit sub Else
Dim UIDoc As UIDocument = me.ActiveUIDocument
Dim Doc As Document = UIDoc.Document
Dim AcView As View = UIDoc.ActiveGraphicalView
Dim FEC As New FilteredElementCollector(Doc, Doc.ActiveView.Id)
Dim ECF As New ElementCategoryFilter(BuiltInCategory.OST_Dimensions)
Dim Els As List(Of Dimension) = FEC.WherePasses(ECF).WhereElementIsNotElementType.ToElements.Cast(Of Dimension).ToList
Dim T As Transform = Transform.Identity
T.BasisX = AcView.RightDirection
T.BasisY = AcView.UpDirection
T.BasisZ = AcView.ViewDirection
'Seems horizontal and vertical dimensions have a larger gap between the text and the blue origin dot
Const GapFactor = 0.75 / 2.5 '0.3
Using tx As New Transaction(Doc, "Adjust dim text position")
If tx.Start = TransactionStatus.Started Then
For i = 0 To Els.Count - 1
Dim Dm As Dimension = Els(i)
Dim Dm_Typ As DimensionType = Doc.GetElement(Dm.GetTypeId)
Dim TxtH_P As Parameter = Dm_Typ.Parameter(BuiltInParameter.TEXT_SIZE)
Dim TxtH As Double = TxtH_P.AsDouble * AcView.Scale
Dm.HasLeader = False
Dim LNdir As Line = TryCast(Dm.Curve, Line)
If LNdir Is Nothing Then Continue For Else
Dim LNdirV As XYZ = T.OfVector(LNdir.Direction)
Dim Sx As Double = CDbl(LNdirV.X.ToString("F8")) ^ 2
Dim Sy As Double = CDbl(LNdirV.Y.ToString("F8")) ^ 2
Dim GFactor As Double = 0
If Sx = 1 OrElse Sy = 1 Then 'Horizontal or vertical in view
GFactor = TxtH_P.AsDouble * GapFactor
GFactor *= AcView.Scale
End If
If Dm.Segments.Size = 0 Then
UpdateDimText(Doc, Dm, TxtH, GFactor)
Else
For si = 0 To Dm.Segments.Size - 1
Dim DSeg As DimensionSegment = Dm.Segments(si)
UpdateDimText(Doc, DSeg, TxtH, GFactor)
Next
End If
Next
tx.Commit()
End If
End Using
End Sub
End Class
The below is a further discussion that may be of interest to others dealing with the dimension text in the API.
In doing the above exercise I observed that when the dimension text is horizontal or vertical the gap between the bottom of the text and the blue dot representing the text origin increases. In the above I have therefore approximated this gap as follows:
Gap = 0.3 x Text Height
From 0.75mm Gap / 2.5mm Text Height
This above was checked against Arial 2.5mm and 5.0mm and seems fairly stable for differing angles between vertical and horizontal.
Comparison of large gap on vertical dimension and small gap on non vertical or horizontal