Hide Table of Contents

Get Bounding Box Example (VBA)

This example shows how to get the bounding box for the selected feature. This example also shows how to draw a 3D sketch depicting the bounding box.

 

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

'

' Preconditions: Model document is open and a feature is selected.

'

' Postconditions: 3D sketch of the bounding box is created.

'

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

Option Explicit

Sub main()

    Dim swApp                       As SldWorks.SldWorks

    Dim swModel                     As SldWorks.ModelDoc2

    Dim swSelMgr                    As SldWorks.SelectionMgr

    Dim swFeat                      As SldWorks.feature

    Dim vBox                        As Variant

    Dim swSketchPt(8)               As SldWorks.SketchPoint

    Dim swSketchSeg(12)             As SldWorks.SketchSegment

    Dim bRet                        As Boolean

    

    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set swSelMgr = swModel.SelectionManager

    Set swFeat = swSelMgr.GetSelectedObject5(1)

    

    Debug.Print "Feature = " & swFeat.Name

    bRet = swFeat.GetBox(vBox): Debug.Assert bRet

    Debug.Print "  Pt1 = " & _

            "(" & _

            vBox(0) * 1000# & ", " & _

            vBox(1) * 1000# & ", " & _

            vBox(2) * 1000# & _

            ") mm"

    Debug.Print "  Pt2 = " & _

            "(" & _

            vBox(3) * 1000# & ", " & _

            vBox(4) * 1000# & ", " & _

            vBox(5) * 1000# & _

            ") mm"

    swModel.Insert3DSketch2 True

    swModel.SetAddToDB True

    swModel.SetDisplayWhenAdded False

    

    ' Draw points at each corner of bounding box

    Set swSketchPt(0) = swModel.CreatePoint2(vBox(3), vBox(1), vBox(5))

    Set swSketchPt(1) = swModel.CreatePoint2(vBox(0), vBox(1), vBox(5))

    Set swSketchPt(2) = swModel.CreatePoint2(vBox(0), vBox(1), vBox(2))

    Set swSketchPt(3) = swModel.CreatePoint2(vBox(3), vBox(1), vBox(2))

    Set swSketchPt(4) = swModel.CreatePoint2(vBox(3), vBox(4), vBox(5))

    Set swSketchPt(5) = swModel.CreatePoint2(vBox(0), vBox(4), vBox(5))

    Set swSketchPt(6) = swModel.CreatePoint2(vBox(0), vBox(4), vBox(2))

    Set swSketchPt(7) = swModel.CreatePoint2(vBox(3), vBox(4), vBox(2))

    

    ' Now draw bounding box

    Set swSketchSeg(0) = swModel.CreateLine2(swSketchPt(0).x, swSketchPt(0).y, swSketchPt(0).z, swSketchPt(1).x, swSketchPt(1).y, swSketchPt(1).z)

    Set swSketchSeg(1) = swModel.CreateLine2(swSketchPt(1).x, swSketchPt(1).y, swSketchPt(1).z, swSketchPt(2).x, swSketchPt(2).y, swSketchPt(2).z)

    Set swSketchSeg(2) = swModel.CreateLine2(swSketchPt(2).x, swSketchPt(2).y, swSketchPt(2).z, swSketchPt(3).x, swSketchPt(3).y, swSketchPt(3).z)

    Set swSketchSeg(3) = swModel.CreateLine2(swSketchPt(3).x, swSketchPt(3).y, swSketchPt(3).z, swSketchPt(0).x, swSketchPt(0).y, swSketchPt(0).z)

    

    Set swSketchSeg(4) = swModel.CreateLine2(swSketchPt(0).x, swSketchPt(0).y, swSketchPt(0).z, swSketchPt(4).x, swSketchPt(4).y, swSketchPt(4).z)

    Set swSketchSeg(5) = swModel.CreateLine2(swSketchPt(1).x, swSketchPt(1).y, swSketchPt(1).z, swSketchPt(5).x, swSketchPt(5).y, swSketchPt(5).z)

    Set swSketchSeg(6) = swModel.CreateLine2(swSketchPt(2).x, swSketchPt(2).y, swSketchPt(2).z, swSketchPt(6).x, swSketchPt(6).y, swSketchPt(6).z)

    Set swSketchSeg(7) = swModel.CreateLine2(swSketchPt(3).x, swSketchPt(3).y, swSketchPt(3).z, swSketchPt(7).x, swSketchPt(7).y, swSketchPt(7).z)

    

    Set swSketchSeg(8) = swModel.CreateLine2(swSketchPt(4).x, swSketchPt(4).y, swSketchPt(4).z, swSketchPt(5).x, swSketchPt(5).y, swSketchPt(5).z)

    Set swSketchSeg(9) = swModel.CreateLine2(swSketchPt(5).x, swSketchPt(5).y, swSketchPt(5).z, swSketchPt(6).x, swSketchPt(6).y, swSketchPt(6).z)

    Set swSketchSeg(10) = swModel.CreateLine2(swSketchPt(6).x, swSketchPt(6).y, swSketchPt(6).z, swSketchPt(7).x, swSketchPt(7).y, swSketchPt(7).z)

    Set swSketchSeg(11) = swModel.CreateLine2(swSketchPt(7).x, swSketchPt(7).y, swSketchPt(7).z, swSketchPt(4).x, swSketchPt(4).y, swSketchPt(4).z)

    

    swModel.SetDisplayWhenAdded True

    swModel.SetAddToDB False

    swModel.Insert3DSketch2 True

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 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) 2012 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.