Ok, Understood. That means I need to change the USC to make sure the object aligns with the XYZ axis.
Public Sub TestTightBoundingBox()
Dim invApp As Inventor.Application = GetObject(, "Inventor.Application")
' Have a body selected.
Dim body As SurfaceBody
body = invApp.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select the body.")
' Call the function to get the tight bounding box.
Dim bndBox As Box = calculateTightBoundingBox(body)
' Draw the bounding box using a 3D sketch.
Dim partDoc As PartDocument = invApp.ActiveDocument
Dim sk As Sketch3D = partDoc.ComponentDefinition.Sketches3D.Add()
Dim lines As SketchLines3D = sk.SketchLines3D
Dim tg As TransientGeometry = invApp.TransientGeometry
Dim minXYZ As Point = bndBox.MinPoint
Dim minXYmaxZ As Point = tg.CreatePoint(bndBox.MinPoint.X, bndBox.MinPoint.Y, bndBox.MaxPoint.Z)
Dim minXmaxYZ As Point = tg.CreatePoint(bndBox.MinPoint.X, bndBox.MaxPoint.Y, bndBox.MaxPoint.Z)
Dim minXZmaxY As Point = tg.CreatePoint(bndBox.MinPoint.X, bndBox.MaxPoint.Y, bndBox.MinPoint.Z)
Dim maxXYZ As Point = bndBox.MaxPoint
Dim maxXYminZ As Point = tg.CreatePoint(bndBox.MaxPoint.X, bndBox.MaxPoint.Y, bndBox.MinPoint.Z)
Dim maxXZminY As Point = tg.CreatePoint(bndBox.MaxPoint.X, bndBox.MinPoint.Y, bndBox.MaxPoint.Z)
Dim maxXminYZ As Point = tg.CreatePoint(bndBox.MaxPoint.X, bndBox.MinPoint.Y, bndBox.MinPoint.Z)
lines.AddByTwoPoints(minXYZ, minXYmaxZ)
lines.AddByTwoPoints(minXYZ, minXZmaxY)
lines.AddByTwoPoints(minXZmaxY, minXmaxYZ)
lines.AddByTwoPoints(minXYmaxZ, minXmaxYZ)
lines.AddByTwoPoints(maxXYZ, maxXYminZ)
lines.AddByTwoPoints(maxXYZ, maxXZminY)
lines.AddByTwoPoints(maxXYminZ, maxXminYZ)
lines.AddByTwoPoints(maxXZminY, maxXminYZ)
lines.AddByTwoPoints(minXYZ, maxXminYZ)
lines.AddByTwoPoints(minXYmaxZ, maxXZminY)
lines.AddByTwoPoints(minXmaxYZ, maxXYZ)
lines.AddByTwoPoints(minXZmaxY, maxXYminZ)
End Sub
' Calculates a tight bounding box around the input body. An optional
' tolerance argument is available. This specificies the tolerance in
' centimeters. If not provided the best existing display mesh is used.
Public Function calculateTightBoundingBox(body As SurfaceBody, Optional Tolerance As Double = 0) As Box
Try
Dim vertCount As Integer
Dim facetCount As Integer
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 Long
Dim tols() As Double = {}
Call body.GetExistingFacetTolerances(tolCount, tols)
Dim bestTol As Double
bestTol = tols(0)
For i As Integer = 1 To tolCount - 1
If tols(i) < bestTol Then
bestTol = tols(i)
End If
Next
body.GetExistingFacets(bestTol, vertCount, facetCount, vertCoords, normVectors, vertInds)
Else
' Calculate a new mesh based on the input tolerance.
body.CalculateFacets(Tolerance, vertCount, facetCount, vertCoords, normVectors, vertInds)
End If
Dim tg As TransientGeometry = body.Application.TransientGeometry
' Calculate the range of the mesh.
Dim smallPnt As Point = tg.CreatePoint(vertCoords(0), vertCoords(1), vertCoords(2))
Dim largePnt As Point = tg.CreatePoint(vertCoords(0), vertCoords(1), vertCoords(2))
For i As Integer = 1 To vertCount - 1
Dim vertX As Double = vertCoords(i * 3)
Dim vertY As Double = vertCoords(i * 3 + 1)
Dim vertZ As Double = vertCoords(i * 3 + 2)
If vertX < smallPnt.X Then
smallPnt.X = vertX
End If
If vertY < smallPnt.Y Then
smallPnt.Y = vertY
End If
If vertZ < smallPnt.Z Then
smallPnt.Z = vertZ
End If
If vertX > largePnt.X Then
largePnt.X = vertX
End If
If vertY > largePnt.Y Then
largePnt.Y = vertY
End If
If vertZ > largePnt.Z Then
largePnt.Z = vertZ
End If
Next
' Create and return a Box as the result.
Dim newBox As Box = tg.CreateBox()
newBox.MinPoint = smallPnt
newBox.MaxPoint = largePnt
Return newBox
Catch ex As Exception
Return Nothing
End Try
End Function
I try to copy-paste into my Inventor rules but it shows
