Issue
I uses GetMinimumDistance to get the distance from a cylinder to a cone face. If the cone has no tip (minor and major radius > 0), the distance looks correct. But if it has tip, the distance is no correct. It looks it chooses a random point on conical surface, instead of the tip point. e.g. as shown in the fig below. The attachment is a code reproduce it.
Public Sub GetMinDistance()
Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument
Dim oDef As PartComponentDefinition
Set oDef = oDoc.ComponentDefinition
Dim oTransientBRep As TransientBRep
Set oTransientBRep = ThisApplication.TransientBRep
' Create a point representing the center of the bottom of the cone
Dim oBottom As Point
Set oBottom = ThisApplication.TransientGeometry.CreatePoint(0, 0, 0)
' Create a point representing the tip of the cone
Dim oTop As Point
Set oTop = ThisApplication.TransientGeometry.CreatePoint(0, 2.54, 0)
' Create a transient cone body
Dim oBody As SurfaceBody
Set oBody = oTransientBRep.CreateSolidCylinderCone(oBottom, oTop, 5, 5, 2)
' Reset the top and bottom point of the cylinder
Set oTop = ThisApplication.TransientGeometry.CreatePoint(0, 2.54 + 1.27, 0)
Set oBottom = ThisApplication.TransientGeometry.CreatePoint(0, 2.54 + 1.27 + 2.54, 0)
' Create a transient cylinder body
Dim oCylBody As SurfaceBody
'Set oCylBody = oTransientBRep.CreateSolidCylinderCone(oBottom, oTop, 2.5, 2.5, 2.5)
Set oCylBody = oTransientBRep.CreateSolidCylinderCone(oBottom, oTop, 2.5, 2.5, 0)
' Boolean the bodies; "oBody" will return the result
Call oTransientBRep.DoBoolean(oBody, oCylBody, kBooleanTypeUnion)
Dim oBaseFeature As NonParametricBaseFeature
Set oBaseFeature = oDef.Features.NonParametricBaseFeatures.Add(oBody)
Dim ocyl As Face
Dim ocone As Face
Dim f As Face
For Each f In oBaseFeature.Faces
If TypeOf f.Geometry Is Cone Then
Set ocone = f
Exit For
End If
Next
For Each f In oBaseFeature.Faces
If TypeOf f.Geometry Is Cone And (Not f Is ocone) Then
Set ocyl = f
Exit For
End If
Next
Dim cont As NameValueMap
Dim dist As Double
' The distance returned below is same as return By UI
dist = ThisApplication.MeasureTools.GetMinimumDistance(ocone, ocyl, kNoInference, kNoInference, cont)
Dim pt1 As Point
Set pt1 = cont.Item(2)
Dim pt2 As Point
Set pt2 = cont.Item(3)
oDef.WorkPoints.AddFixed pt1
oDef.WorkPoints.AddFixed pt2
End Sub
Solution
Assume there are one cylinder and one cone (with tip). The minimum distance between cylindrical face and cone with tip is calculated incorrectly as we have seen. But if we select planar face of cylinder and conical face then distance is shown correctly between tip and planar face.
If we create a 2D model bases on the 3D model like below figure.
The 2D model is created on a plane which is across the axis of the cone and cylinder, so now we can find that the minimum distance of the conical face and cylindrical face is the same of the minimum distance between the black line and the blue line. So create a line from the top end(red point) of the black line and perpendicular to the blue line like the picture shows, we will get the yellow line, the yellow line is the shortest one between the black line and blue line.
Actually this is affected by the apex angle of the cone(also the position of the cone tip), so like below if the apex angle of the cone is sharp enough that the yellow line has its perpendicular point on the extension of the blue line then the shortest line between the black line and the blue line would be like magenta line in below figure: