Been investigating this and I believe that although you can dimension within the sketch for the UI this can't be done for the API. I've trialled both:
Line end point references
Line references
Both for the model line geometry curves that are part of the sketch (OST_SketchLines). Although the referece values for these match what is in the reference array for the dimension in Revit lookup I still get the 'invalid number of references' exception message. I think the issue stems from the fact the sketch lines are not visible in the view required by the .NewDimension method. Others may be able to advise otherwise on this.
If not you need to instead use the edges of the geometry of the extrusion, similar to below. This approach has certain disadvantages, the main one being that the shape of the extrusion can't be changed from outside the sketch with dimensions like it can when you place dimension within the sketch in the UI.
Private Function SamePoints(Pt1 As XYZ, Pt2 As XYZ, Optional Tolerance As Double = 0.5 / 304.8) As Boolean
Return Pt1.DistanceTo(Pt2) <= Tolerance
End Function
Private Function SameLines(LN1 As Line, LN2 As Line, Optional IgnoreEndOrder As Boolean = True,
Optional Tolerance As Double = 0.5 / 304.8) As Boolean
If IgnoreEndOrder = False Then
Return SamePoints(LN1.GetEndPoint(0), LN2.GetEndPoint(0), Tolerance) AndAlso
SamePoints(LN1.GetEndPoint(1), LN2.GetEndPoint(1), Tolerance)
Else
If SamePoints(LN1.GetEndPoint(0), LN2.GetEndPoint(0), Tolerance) Then
Return SamePoints(LN1.GetEndPoint(1), LN2.GetEndPoint(1), Tolerance)
Else
If SamePoints(LN1.GetEndPoint(0), LN2.GetEndPoint(1), Tolerance) Then
Return SamePoints(LN1.GetEndPoint(1), LN2.GetEndPoint(0), Tolerance)
Else
Return False
End If
End If
End If
End Function
Private Function EdgesForExtrusion(Extrusion As Extrusion, Lines As Line()) As Edge()
Dim GeomObj As GeometryElement =
Extrusion.Geometry(New Options With {.ComputeReferences = True, .DetailLevel = ViewDetailLevel.Fine})
Dim S As Solid = GeomObj(0) 'Always a single top level solid for family extrusions? (probably).
Dim EdgesOut As New List(Of Edge)
For i = 0 To S.Edges.Size - 1
If Lines.Length = EdgesOut.Count Then Exit For Else
Dim E As Edge = S.Edges(i)
Dim C As Line = TryCast(E.AsCurve, Line)
If C Is Nothing Then Continue For Else
'I index the input based on the count of items in the output:
If SameLines(Lines(EdgesOut.Count), C, True) Then
EdgesOut.Add(E)
End If
Next
Return EdgesOut.ToArray
End Function
Private Function TObj120(ByVal commandData As Autodesk.Revit.UI.ExternalCommandData,
ByRef message As String, ByVal elements As Autodesk.Revit.DB.ElementSet) As Result
If commandData.Application.ActiveUIDocument Is Nothing Then Return Result.Cancelled Else
Dim UIDoc As UIDocument = commandData.Application.ActiveUIDocument
Dim Doc As Document = UIDoc.Document
If Doc.IsFamilyDocument = False Then Return Result.Cancelled Else
Dim AcView As ViewPlan = TryCast(Doc.ActiveView, ViewPlan)
If AcView Is Nothing Then Return Result.Cancelled Else
Const EdgeLength As Double = 1
Dim LN1 = Line.CreateBound(XYZ.Zero, New XYZ(EdgeLength, 0, 0))
Dim LN2 = Line.CreateBound(New XYZ(EdgeLength, 0, 0), New XYZ(EdgeLength, EdgeLength, 0))
Dim LN3 = Line.CreateBound(New XYZ(EdgeLength, EdgeLength, 0), New XYZ(0, EdgeLength, 0))
Dim LN4 = Line.CreateBound(New XYZ(0, EdgeLength, 0), XYZ.Zero)
Dim Ca As New CurveArray
Ca.Append(LN1)
Ca.Append(LN2)
Ca.Append(LN3)
Ca.Append(LN4)
Dim Caa As New CurveArrArray
Caa.Append(Ca)
Using tx As New Transaction(Doc, "Extrude and Dimension")
If tx.Start = TransactionStatus.Started Then
Dim Ext As Extrusion = Doc.FamilyCreate.NewExtrusion(True, Caa, AcView.SketchPlane, 1)
Const EndPointRefs As Boolean = False
Dim ra As New ReferenceArray
If EndPointRefs = False Then
'This works and is ok when you flex the family because the edges are parallel
Dim Edges As Edge() = EdgesForExtrusion(Ext, New Line() {LN4, LN2})
If Edges.Length <> 2 Then
tx.RollBack()
Else
ra.Append(Edges(0).Reference)
ra.Append(Edges(1).Reference)
End If
Else
'This you can also do but when you flex the family it will fail (constraints not satisfied)
'Outside the sketch you can't change the shape of the extrusion
Dim Edges As Edge() = EdgesForExtrusion(Ext, New Line() {LN1})
If Edges.Length <> 1 Then
tx.RollBack()
Else
ra.Append(Edges(0).GetEndPointReference(0))
ra.Append(Edges(0).GetEndPointReference(1))
End If
End If
Dim DM As Dimension = Doc.FamilyCreate.NewDimension(AcView, LN1, ra)
Dim PM As FamilyParameter =
Doc.FamilyManager.AddParameter("UniqueNamedParam", BuiltInParameterGroup.PG_GENERAL, ParameterType.Length, True)
DM.FamilyLabel = PM
tx.Commit()
End If
End Using
End Function