Thank you Brian for all this beautiful code, I have been "borrowing" a lot. I found that running that code can be very slow when the part as several faces, it seems that sorting was the issue. Below is a code alteration:
Public Function calculateTightBoundingBox(body As SurfaceBody, Optional Tolerance As Double = 0) As Box
Dim vertCount As Long
Dim facetCount As Long
Dim vertCoords() As Double = {}
Dim normVectors() As Double = {}
Dim vertInds() As Integer = {}
' If the tolerance is zero, use the best display mesh available.
If Tolerance <= 0 Then
' Get the best display mesh available.
Dim tolCount As Integer
Dim tols() As Double = {}
Call body.GetExistingFacetTolerances(tolCount, tols)
Dim i As Integer
Dim bestTol As Double
bestTol = tols(0)
For i = 1 To tolCount - 1
If tols(i) < bestTol Then
bestTol = tols(i)
End If
Next
Call body.GetExistingFacets(bestTol, vertCount, facetCount, vertCoords, normVectors, vertInds)
Else
' Calculate a new mesh based on the input tolerance.
Call body.CalculateFacets(Tolerance, vertCount, facetCount, vertCoords, normVectors, vertInds)
End If
Dim tg As TransientGeometry
tg = _invApp.TransientGeometry
' Calculate the range of the mesh.
Dim smallPnt As Point
Dim largePnt As Point
smallPnt = tg.CreatePoint(vertCoords(0), vertCoords(1), vertCoords(2))
largePnt = tg.CreatePoint(vertCoords(0), vertCoords(1), vertCoords(2))
Dim vertCoordsX(((vertCoords.Length) / 3) - 1) As Double
Dim vertCoordsY(((vertCoords.Length) / 3) - 1) As Double
Dim vertCoordsZ(((vertCoords.Length) / 3) - 1) As Double
Dim w As Integer = 0
For x_ = 0 To vertCoords.Count - 1
vertCoordsX(w) = vertCoords(x_)
x_ = x_ + 2
w = w + 1
Next
Array.Sort(vertCoordsX)
w = 0
For y = 1 To vertCoords.Count - 1
vertCoordsY(w) = vertCoords(y)
y = y + 2
w = w + 1
Next
Array.Sort(vertCoordsY)
w = 0
For z = 2 To vertCoords.Count - 1
vertCoordsZ(w) = vertCoords(z)
z = z + 2
w = w + 1
Next
Array.Sort(vertCoordsZ)
smallPnt.X = vertCoordsX(0)
smallPnt.Y = vertCoordsY(0)
smallPnt.Z = vertCoordsZ(0)
largePnt.X = vertCoordsX(UBound(vertCoordsX))
largePnt.Y = vertCoordsY(UBound(vertCoordsY))
largePnt.Z = vertCoordsZ(UBound(vertCoordsZ))
' Create and return a Box as the result.
calculateTightBoundingBox = tg.CreateBox()
calculateTightBoundingBox.MinPoint = smallPnt
calculateTightBoundingBox.MaxPoint = largePnt
End Function