Option Explicit On Sub Main() Dim compDef As PartComponentDefinition = ThisDoc.Document.ComponentDefinition Dim body As SurfaceBody Dim oFaces As Faces Dim oFace As Face body = compDef.SurfaceBodies(1) oFaces = body.Faces Dim faceCount As Integer = body.Faces.Count Dim featureAddedCount As Integer = 0 For faceIndex = 1 To faceCount oFace = oFaces(faceIndex) Dim featureAdded = ThreadBuild(oFace) If featureAdded Then featureAddedCount = featureAddedCount + 1 If featureAddedCount = 1 Then ' For some reason, the face references might be stale after we add the first feature. ' Get fresh references: body = compDef.SurfaceBodies(1) oFaces = body.Faces End If End If Next End Sub Function ThreadBuild(oFace As Face) As Boolean Logger.Debug(oFace.SurfaceType.ToString) Dim oTFs As ThreadFeatures = ThisDoc.Document.ComponentDefinition.Features.ThreadFeatures Dim oTF As ThreadFeature Dim oEdge As Edge Dim oThreadInfo As ThreadInfo Dim sThreadType As String If oFace.SurfaceType = kCylinderSurface ' Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL)) If oFace.ThreadInfos Is Nothing sThreadType = GetThreadTypeByHoleDia(2 * oFace.Geometry.Radius * 10) If sThreadType <> String.Empty oEdge = oFace.Edges(1) oThreadInfo = oTFs.CreateStandardThreadInfo(True, True, "ISO Metric Profile", sThreadType, "6H") oTF = oTFs.Add(oFace, oEdge, oThreadInfo, False, True) Return True ' feature created End If End If End If Return False ' no feature created End Function Function GetThreadTypeByHoleDia(dHoleDia As Double) As String Dim sThreadType As String Select Ceil(dHoleDia * 100) / 100 Case 1.57 ' 1.567 sThreadType = "M2x0.4" Case 2.46 ' 2.459 sThreadType = "M3x0.5" Case 3.25 ' 3.242 !! sThreadType = "M4x0.7" Case 4.14 ' 4.134 !! sThreadType = "M5x0.8" Case 4.92 ' 4.917 sThreadType = "M6x1" Case 6.65 ' 6.647 sThreadType = "M8x1.25" Case 8.38 ' 8.376 sThreadType = "M10x1.5" Case 10.11 ' 10.106 sThreadType = "M12x1.75" Case 11.84 ' 11.835 sThreadType = "M14x2" Case 13.84 ' 13.835 sThreadType = "M16x2" End Select Return sThreadType End Function