I realised we aren't quite on the same page, as I overlooked the use of constraints.
My code wasn't an exact match to what you need, however, I have managed to fumble this together:
Sub Main()
Dim tg As TransientGeometry = ThisApplication.TransientGeometry
Dim ptDoc As PartDocument = ThisApplication.ActiveDocument
Dim ptDef As PartComponentDefinition = ptDoc.ComponentDefinition
Dim sk As PlanarSketch = ptDef.Sketches(1)
Dim linSkLine As SketchLine = sk.SketchLines(1)
Dim body As SurfaceBody = ptDef.SurfaceBodies(1)
'get the face
Dim topFace As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAllPlanarEntities, "Select the face")
If topFace Is Nothing Then Exit Sub
'get mid point of sketchline
Dim midP As Point2d = linSkLine.Geometry.MidPoint
'create a vector of the lineLine
Dim linVect As New VectorXY(New PointXY(linSkLine.Geometry.StartPoint.X, linSkLine.Geometry.StartPoint.Y),
New PointXY(linSkLine.Geometry.EndPoint.X, linSkLine.Geometry.EndPoint.Y))
'create a perpendicular vector
Dim perpVect As VectorXY = linVect.CreatePerpendicularVectorFromPoint(linVect.MidPoint, 0.01)
'create a sketchpoint on the end of the vector
Dim testPoint As SketchPoint = sk.SketchPoints.Add(tg.CreatePoint2d(perpVect.EndPoint.X, perpVect.EndPoint.Y))
'create a workpoint so we can get the true x,y,z coordinates of the point in respect to UCS
Dim testWP As WorkPoint = ptDef.WorkPoints.AddByPoint(testPoint)
testPoint.Delete()
'create our new point to test if this is on the face
Dim newPoint As Point = tg.CreatePoint(testWP.Point.X, testWP.Point.Y, testWP.Point.Z)
Dim offset As Double = 0.0
If PointIsOnFace(topFace, newPoint) Then
offset = 0.5
'MsgBox("Is on face")
Else
offset = -0.5
'MsgBox("Is not on face")
End If
'delete the workpoint
testWP.Delete()
'create the new perp vector to place our sketchpoint:
Dim newperpVect As VectorXY = linVect.CreatePerpendicularVectorFromPoint(linVect.MidPoint, offset)
'create a sketchpoint on the end of the vector
Dim finalskPoint As SketchPoint = sk.SketchPoints.Add(tg.CreatePoint2d(newperpVect.EndPoint.X, newperpVect.EndPoint.Y))
End Sub
Function PointIsOnFace(f As Face, oPoint As Point) As Boolean
If f.GetClosestPointTo(oPoint).IsEqualTo(oPoint, 0.001) Then
'"Point is on face"
Return True
Else
'"Point is not on face"
Return False
End If
End Function
Public Class PointXY
Public Property X As Double
Public Property Y As Double
Public Sub New(x As Double, y As Double)
Me.X = x
Me.Y = y
End Sub
End Class
Public Class VectorXY
Public Property StartPoint As PointXY
Public Property EndPoint As PointXY
Public Sub New(StartPoint As PointXY, EndPoint As PointXY)
Me.StartPoint = StartPoint
Me.EndPoint = EndPoint
End Sub
Public Function DX() As Double
Return EndPoint.X - StartPoint.X
End Function
Public Function DY() As Double
Return EndPoint.Y - StartPoint.Y
End Function
Public Function Length() As Double
Return Math.Sqrt(DX() * DX() + DY() * DY())
End Function
Public Function Normalize() As VectorXY
Return New VectorXY(New PointXY(0, 0), New PointXY(DX() / Length(), DY() / Length()))
End Function
Public Shared Function MovePointAlongVector(ByVal p As PointXY, ByVal v As VectorXY, ByVal distance As Double) As PointXY
Dim normalizedVector As VectorXY = v.Normalize()
Dim newPoint As PointXY
newPoint.X = Math.Round(p.X + normalizedVector.DX() * distance, 6)
newPoint.Y = Math.Round(p.Y + normalizedVector.DY() * distance, 6)
Return newPoint
End Function
Public Function MidPoint() As PointXY
Return New PointXY((StartPoint.X + EndPoint.X) / 2, (StartPoint.Y + EndPoint.Y) / 2)
End Function
Public Function CreatePerpendicularVectorFromPoint(refpoint As PointXY, length As Double) As VectorXY
Dim dx As Double = EndPoint.X - StartPoint.X
Dim dy As Double = EndPoint.Y - StartPoint.Y
' Calculate the length of the vector
Dim mag As Double = Math.Sqrt(dx * dx + dy * dy)
' Normalize the vector to a unit vector
dx /= mag
dy /= mag
' Create the perpendicular vector (-dy, dx) or (dy, -dx)
Dim newpoint As New PointXY(refpoint.X - dy * length, refpoint.Y + dx * length)
Return New VectorXY(refpoint, newpoint)
End Function
End Class