Hi All,
I've been programming a macro that would select the first feature in my Cut-list folder and then create a 3D Bounding Box for the selected item.
From what I've gathered from online resources, here is my (maybe 100th) attempt at the macro.
I'm struggling with the cut-list-item selection part. Some sources I viewed online demonstrates how to select Solid Bodies, but not cut-list item IDs. Perhaps I'm over complicating things?
Help much appreciated.
Sources used:
2016 SOLIDWORKS API Help - Create 3D Bounding Box for Cut List Item Example (VBA)
2015 SOLIDWORKS API Help - Get Names of Bodies in Multibody Part Example (VBA)
2016 SOLIDWORKS API Help - Get Weldment Cut List Feature and Annotations Example (VB.NET)
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Dim modDocExt As SldWorks.ModelDocExtension
Dim boolstatus As Boolean
Dim swfeat As Feature
Dim CutItem As String
Option Explicit
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set modDocExt = Part.Extension
' Traverse FeatureManager design tree
' Get first feature in FeatureManager design tree
swfeat = Part.FirstFeature
'Get ID of first feature
CutItem = swfeat.GetID
' Once body selected, create a bounding box
boolstatus = modDocExt.SelectByID2(CutItem, "SUBWELDFOLDER", 0, 0, 0, False, 0, Nothing, 0)
modDocExt.Create3DBoundingBox
End Sub
Hi Raymond,
try this one:
Sub main()
Dim swApp As Object
Dim swmodel As ModelDoc2
Dim swPart As PartDoc
Dim boolstatus As Boolean
Dim swSelMgr As ISelectionMgr
Dim swBody As Body2
Dim vBodies As Variant
Dim j As Integer
Dim BodyFolder As SldWorks.BodyFolder
Dim CutListBdy As Body2
Set swApp = Application.SldWorks
Set swmodel = swApp.ActiveDoc
Set swPart = swmodel
Set swSelMgr = swmodel.SelectionManager
Set swBody = swSelMgr.GetSelectedObject6(0, -1)
Dim Feat As Feature
Set Feat = swmodel.FirstFeature
While Not Feat Is Nothing
If Feat.GetTypeName2 = "CutListFolder" Then
Dim swFeatName As String
swFeatName = Feat.Name
Feat.Select2 True, 0
Debug.Print swFeatName
Set BodyFolder = Feat.GetSpecificFeature2
vBodies = BodyFolder.GetBodies
For j = 0 To UBound(vBodies)
Set CutListBdy = vBodies(j)
Debug.Print CutListBdy.Name
swmodel.Extension.Create3DBoundingBox
Next j
End If
Set Feat = Feat.GetNextFeature()
Wend
End Sub
It is little bit messy, but it works.
It traverse thru all features, then select cutlist folders, then creates a bounding box for every body in cutlist folders.
Loop j is responsible for it, if you want to select only first item, then change this loop.
Preconditions:
Part need to have weldment feature and updated cutlist.
You can achive it, by using IbodyFolder --> UpdateCutList, and InsertWeldmentFeature Method (IFeatureManager)
I hope it will help you.