Get Assembly Bounding Box using Components Example (VBA)
This example shows how to get a bounding box for an assembly.
'--------------------------------------------
'
' Preconditions:
' (1)
Assembly is open.
' (2)
All components in assembly are fully resolved.
'
' Postconditions:
' A
3D sketch is added to assembly showing the bounding box.
' The
sketch has points at each corner of the box and
' lines
connecting the corners.
'
' NOTES:
'
' (1)
Because all assembly component bounding boxes are
' approximated,
then the bounding box for the assembly
' is
also approximated.
'
' (2)
Resulting bounding box is oriented with the assembly
' coordinate
system.
'
'----------------------------------------------
Option Explicit
Public Enum swComponentSuppressionState_e
swComponentSuppressed
= 0 '
Fully suppressed
- nothing is loaded
swComponentLightweight
= 1 '
Featherweight
- only graphics data is loaded
swComponentFullyResolved
= 2 '
Fully resolved
- model is completly loaded
End Enum
' The
visibility information possible for components.
' For
use with auComponent_c::Visibility.
Public Enum swComponentVisibilityState_e
swComponentHidden
= 0
swComponentVisible
= 1
End Enum
Function GetMax _
( _
ByVal
Val1 As Double, _
ByVal
Val2 As Double, _
ByVal
Val3 As Double _
) As Double
' Finds maximum of 3 values
GetMax
= Val1
If
Val2 > GetMax Then
GetMax
= Val2
End
If
If
Val3 > GetMax Then
GetMax
= Val3
End
If
End Function
Function GetMin _
( _
ByVal
Val1 As Double, _
ByVal
Val2 As Double, _
ByVal
Val3 As Double _
) As Double
' Finds minimum of 3 values
GetMin
= Val1
If
Val2 < GetMin Then
GetMin
= Val2
End
If
If
Val3 < GetMin Then
GetMin
= Val3
End
If
End Function
Sub main()
Const
MaxDouble As
Double = 1.79769313486231E+308
Const
MinDouble As
Double = -1.79769313486231E+308
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swAssy As
SldWorks.AssemblyDoc
Dim
swConfig As
SldWorks.Configuration
Dim
swRootComp As
SldWorks.Component2
Dim
vChild As
Variant
Dim
swChildComp As
SldWorks.Component2
Dim
vBox As
Variant
Dim
X_max As
Double
Dim
X_min As
Double
Dim
Y_max As
Double
Dim
Y_min As
Double
Dim
Z_max As
Double
Dim
Z_min As
Double
Dim
swSketchPt(8) As
SldWorks.SketchPoint
Dim
swSketchSeg(12) As
SldWorks.SketchSegment
Dim
i As
Long
Dim
nStatus As
Long
Dim
bRet As
Boolean
Set
swApp = CreateObject("SldWorks.Application")
Set
swModel = swApp.ActiveDoc
Set
swAssy = swModel
Set
swConfig = swAssy.GetActiveConfiguration
Set
swRootComp = swConfig.GetRootComponent
'
Initialize values
X_max
= MinDouble
X_min
= MaxDouble
Y_max
= MinDouble
Y_min
= MaxDouble
Z_max
= MinDouble
Z_min
= MaxDouble
vChild
= swRootComp.GetChildren
For
i = 0 To UBound(vChild)
Set
swChildComp = vChild(i)
If
swChildComp.Visible = swComponentVisible
Then
vBox
= swChildComp.GetBox(False, False)
X_max
= GetMax(vBox(0), vBox(3), X_max)
X_min
= GetMin(vBox(0), vBox(3), X_min)
Y_max
= GetMax(vBox(1), vBox(4), Y_max)
Y_min
= GetMin(vBox(1), vBox(4), Y_min)
Z_max
= GetMax(vBox(2), vBox(5), Z_max)
Z_min
= GetMin(vBox(2), vBox(5), Z_min)
End
If
Next
i
Debug.Print
"AssyBox (" + swModel.GetPathName
+ ") = "
Debug.Print
" ("
+ _
Str(X_min
* 1000#) + "," + _
Str(Y_min
* 1000#) + "," + _
Str(Z_min
* 1000#) + ") mm"
Debug.Print
" ("
+ _
Str(X_max
* 1000#) + "," + _
Str(Y_max
* 1000#) + "," + _
Str(Z_max
* 1000#) + ") mm"
swModel.Insert3DSketch2 True
swModel.SetAddToDB True
'
Draw points at each corner of bounding box
Set
swSketchPt(0) = swModel.CreatePoint2(X_min,
Y_min, Z_min)
Set
swSketchPt(1) = swModel.CreatePoint2(X_min,
Y_min, Z_max)
Set
swSketchPt(2) = swModel.CreatePoint2(X_min,
Y_max, Z_min)
Set
swSketchPt(3) = swModel.CreatePoint2(X_min,
Y_max, Z_max)
Set
swSketchPt(4) = swModel.CreatePoint2(X_max,
Y_min, Z_min)
Set
swSketchPt(5) = swModel.CreatePoint2(X_max,
Y_min, Z_max)
Set
swSketchPt(6) = swModel.CreatePoint2(X_max,
Y_max, Z_min)
Set
swSketchPt(7) = swModel.CreatePoint2(X_max,
Y_max, Z_max)
'
Draw bounding box
Set
swSketchSeg(0) = swModel.CreateLine2(X_min,
Y_min, Z_min, X_max, Y_min, Z_min)
Set
swSketchSeg(1) = swModel.CreateLine2(X_max,
Y_min, Z_min, X_max, Y_min, Z_max)
Set
swSketchSeg(2) = swModel.CreateLine2(X_max,
Y_min, Z_max, X_min, Y_min, Z_max)
Set
swSketchSeg(3) = swModel.CreateLine2(X_min,
Y_min, Z_max, X_min, Y_min, Z_min)
Set
swSketchSeg(4) = swModel.CreateLine2(X_min,
Y_min, Z_min, X_min, Y_max, Z_min)
Set
swSketchSeg(5) = swModel.CreateLine2(X_min,
Y_min, Z_max, X_min, Y_max, Z_max)
Set
swSketchSeg(6) = swModel.CreateLine2(X_max,
Y_min, Z_min, X_max, Y_max, Z_min)
Set
swSketchSeg(7) = swModel.CreateLine2(X_max,
Y_min, Z_max, X_max, Y_max, Z_max)
Set
swSketchSeg(8) = swModel.CreateLine2(X_min,
Y_max, Z_min, X_max, Y_max, Z_min)
Set
swSketchSeg(9) = swModel.CreateLine2(X_max,
Y_max, Z_min, X_max, Y_max, Z_max)
Set
swSketchSeg(10) = swModel.CreateLine2(X_max,
Y_max, Z_max, X_min, Y_max, Z_max)
Set
swSketchSeg(11) = swModel.CreateLine2(X_min,
Y_max, Z_max, X_min, Y_max, Z_min)
swModel.SetAddToDB False
swModel.Insert3DSketch2 True
End Sub
'----------------------------------------