Hide Table of Contents

Get Part Bounding Box Example (VBA)

This example shows how to get an accurate bounding box for a open part.

 

'-----------------------------------------------

'

' Problem:

'       IPartDoc::GetPartBox returns an approximate

'       bounding box and cannot be relied upon to be used

'       in calculations. Typically, the bounding box

'       returned is larger and not a minimal bounding box.

'       The best use for IPartDoc::GetPartbox is as a first

'       approximation to determine whether two parts are intersecting.

'

'       If there is a need for a more accurate bounding

'       box, then use one based on the display tessellation, which

'       gives a more minimal bounding box.

'

' Preconditions: Part is open.

'

' Postconditions: None

'

'-----------------------------------------------

 

Option Explicit

 

Function GetMax _

( _

    Val1 As Double, _

    Val2 As Double, _

    Val3 As Double, _

    Val4 As Double _

) As Double

' Finds maximum of four values

    GetMax = Val1

    

    If Val2 > GetMax Then

        GetMax = Val2

    End If

    If Val3 > GetMax Then

        GetMax = Val3

    End If

    If Val4 > GetMax Then

        GetMax = Val4

    End If

End Function

Function GetMin _

( _

    Val1 As Double, _

    Val2 As Double, _

    Val3 As Double, _

    Val4 As Double _

) As Double

' Finds minimum of four values

    GetMin = Val1

    

    If Val2 < GetMin Then

        GetMin = Val2

    End If

    If Val3 < GetMin Then

        GetMin = Val3

    End If

    If Val4 < GetMin Then

        GetMin = Val4

    End If

End Function

Sub ProcessTessTriangles _

( _

    vTessTriangles As Variant, _

    X_max As Double, _

    X_min As Double, _

    Y_max As Double, _

    Y_min As Double, _

    Z_max As Double, _

    Z_min As Double _

)

    Dim i                   As Long

    For i = 0 To UBound(vTessTriangles) / (1 * 9) - 1

'        ' Debugging output only

'        Debug.Print "Pt(" + Str(i) + ") = "

'        Debug.Print " (" + _

'            Str(vTessTriangles(9 * i + 0)) + "," + _

'            Str(vTessTriangles(9 * i + 1)) + "," + _

'            Str(vTessTriangles(9 * i + 2)) + ")"

'        Debug.Print " (" + _

'            Str(vTessTriangles(9 * i + 3)) + "," + _

'            Str(vTessTriangles(9 * i + 4)) + "," + _

'            Str(vTessTriangles(9 * i + 5)) + ")"

'        Debug.Print " (" + _

'            Str(vTessTriangles(9 * i + 6)) + "," + _

'            Str(vTessTriangles(9 * i + 7)) + "," + _

'            Str(vTessTriangles(9 * i + 8)) + ")"

            

        X_max = GetMax((vTessTriangles(9 * i + 0)), (vTessTriangles(9 * i + 3)), (vTessTriangles(9 * i + 6)), X_max)

        X_min = GetMin((vTessTriangles(9 * i + 0)), (vTessTriangles(9 * i + 3)), (vTessTriangles(9 * i + 6)), X_min)

        

        Y_max = GetMax((vTessTriangles(9 * i + 1)), (vTessTriangles(9 * i + 4)), (vTessTriangles(9 * i + 7)), Y_max)

        Y_min = GetMin((vTessTriangles(9 * i + 1)), (vTessTriangles(9 * i + 4)), (vTessTriangles(9 * i + 7)), Y_min)

        

        Z_max = GetMax((vTessTriangles(9 * i + 2)), (vTessTriangles(9 * i + 5)), (vTessTriangles(9 * i + 8)), Z_max)

        Z_min = GetMin((vTessTriangles(9 * i + 2)), (vTessTriangles(9 * i + 5)), (vTessTriangles(9 * i + 8)), Z_min)

    Next i

End Sub

Sub ProcessBodies _

( _

    vBodies As Variant, _

    X_max As Double, _

    X_min As Double, _

    Y_max As Double, _

    Y_min As Double, _

    Z_max As Double, _

    Z_min As Double _

)

    Dim swBody              As SldWorks.body2

    Dim swFace              As SldWorks.face2

    Dim vTessTriangles      As Variant

    Dim i                   As Long

    

    ' Probably empty if no reference surfaces

    If IsEmpty(vBodies) Then Exit Sub

    

    For i = 0 To UBound(vBodies)

        Set swBody = vBodies(i)

        Set swFace = swBody.GetFirstFace

        While Not swFace Is Nothing

            vTessTriangles = swFace.GetTessTriangles(True)

            ProcessTessTriangles vTessTriangles, X_max, X_min, Y_max, Y_min, Z_max, Z_min

            

            Set swFace = swFace.GetNextFace

        Wend

    Next i

End Sub

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 swPart              As SldWorks.PartDoc

    Dim vBodies             As Variant

    Dim vBoundBox           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 i                   As Long

    

    Set swApp = CreateObject("SldWorks.Application")

    Set swModel = swApp.ActiveDoc

    Set swPart = swModel

    

    ' Initialise to large/small values

    X_max = MinDouble

    X_min = MaxDouble

    Y_max = MinDouble

    Y_min = MaxDouble

    Z_max = MinDouble

    Z_min = MaxDouble

    

    ' Solid body

    vBodies = swPart.GetBodies2(swSolidBody, False)

    ProcessBodies vBodies, X_max, X_min, Y_max, Y_min, Z_max, Z_min

    

    ' Reference surfaces

    vBodies = swPart.GetBodies2(swSheetBody, False)

    ProcessBodies vBodies, X_max, X_min, Y_max, Y_min, Z_max, Z_min

    

    ' Approximate bounding box

    vBoundBox = swPart.GetPartBox(True)

    Debug.Print "Tessellation Quality = " + Str(swModel.GetTessellationQuality)

    Debug.Print ""

    

    Debug.Print "PartBox = "

    Debug.Print "  (" + _

                    Str(vBoundBox(0) * 1000#) + "," + _

                    Str(vBoundBox(1) * 1000#) + "," + _

                    Str(vBoundBox(2) * 1000#) + ") mm"

    Debug.Print "  (" + _

                    Str(vBoundBox(3) * 1000#) + "," + _

                    Str(vBoundBox(4) * 1000#) + "," + _

                    Str(vBoundBox(5) * 1000#) + ") mm"

    Debug.Print ""

    

    Debug.Print "TessBox = "

    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"

    Debug.Print ""

End Sub

'------------------------------------------



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Get Part Bounding Box Example (VBA)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2019 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.