MACRO LIBRARY

Get weldment bounding box

Free public access. No account or subscription required.

Files

Get weldment bounding box Associated model

Code

Module:

'Demonstrates how to get the length of any weldment body, even if it is not a structural member,
'   by using IModelDocExtension::Create3DBoundingBox. Contains two functions:
    
'Create3DBoundingBox - can be used to create a 3D bounding box on a selected cut-list folder
'GetLengthOf3DBoundingBox - gets the length of the selected cut list item and prints to debug window
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Sub Create3DBoundingBox()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    swModel.Extension.Create3DBoundingBox
End Sub
    
Sub GetLengthOf3DBoundingBox()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swFeat As SldWorks.Feature
    Dim swSketch As SldWorks.Sketch
    Dim swSeg As SldWorks.SketchSegment
    Dim swLongestSeg As SldWorks.SketchSegment
    Dim vSegs As Variant
    Dim i As Integer
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    Set swSelMgr = swModel.SelectionManager
    
    If swSelMgr.GetSelectedObjectType3(1, -1) <> swSelSKETCHES Then Exit Sub
    
    Set swFeat = swSelMgr.GetSelectedObject6(1, -1)
    Set swSketch = swFeat.GetSpecificFeature2
    
    vSegs = swSketch.GetSketchSegments
    
    If IsEmpty(vSegs) Then Exit Sub
    
    For i = 0 To UBound(vSegs)
        If i = 0 Then
            Set swLongestSeg = vSegs(i)
        Else
            Set swSeg = vSegs(i)
            If swSeg.GetLength > swLongestSeg.GetLength Then Set swLongestSeg = swSeg
        End If
    Next i
    
    Debug.Print Round(swLongestSeg.GetLength, 6)
    
End Sub