This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
This macro allows to exclude the selected bodies from the weldment or sheet metal cut list using SOLIDWORKS API.
Bodies can be selected in the graphics view or feature tree which makes the process easier as it is not required to find the corresponding cut-list feature to exclude the body.
It is possible to use selection filters for bodies to simplify the picking of required ones from the graphics area.
It is also possible to select face, edge or vertex of the body to be excluded.
Dim swApp As SldWorks.SldWorks
Sub main()
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
IfNot swModel IsNothingThenSet swSelMgr = swModel.SelectionManager
Dim swCutListsColl As Collection
Set swCutListsColl = New Collection
Dim i AsIntegerDim hasBodies AsBooleanFor i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
OnErrorResumeNextDim swBody As SldWorks.Body2
Set swBody = GetSelectedObjectBody(swSelMgr, i)
IfNot swBody IsNothingThenDim swCutListFeat As SldWorks.Feature
Set swCutListFeat = GetCutListFromBody(swModel, swBody)
IfNot swCutListFeat IsNothingThenIfNot Contains(swCutListsColl, swCutListFeat) Then
swCutListsColl.Add swCutListFeat
EndIfElse
MsgBox "Failed to find the cut list item for " & swBody.Name
EndIfEndIfNextIf swCutListsColl.Count() > 0 ThenFor i = 1 To swCutListsColl.Count
swCutListsColl(i).ExcludeFromCutList = TrueNextElse
MsgBox "Please select bodies to exclude from cut list"EndIfElse
MsgBox "Please open model"EndIfEndSubFunction GetSelectedObjectBody(selMgr As SldWorks.SelectionMgr, index AsInteger) As SldWorks.Body2
Dim swBody As SldWorks.Body2
Dim selObj AsObjectSet selObj = selMgr.GetSelectedObject6(index, -1)
IfNot selObj IsNothingThenIfTypeOf selObj Is SldWorks.Body2 ThenSet swBody = selObj
ElseIfTypeOf selObj Is SldWorks.Face2 ThenDim swFace As SldWorks.Face2
Set swFace = selObj
Set swBody = swFace.GetBody
ElseIfTypeOf selObj Is SldWorks.Edge ThenDim swEdge As SldWorks.Edge
Set swEdge = selObj
Set swBody = swEdge.GetBody
ElseIfTypeOf selObj Is SldWorks.Vertex ThenDim swVertex As SldWorks.Vertex
Set swVertex = selObj
Set swBody = swVertex.GetBody
EndIfEndIfSet GetSelectedObjectBody = swBody
EndFunctionFunction GetCutListFromBody(model As SldWorks.ModelDoc2, body As SldWorks.Body2) As SldWorks.Feature
Dim swFeat As SldWorks.Feature
Dim swBodyFolder As SldWorks.BodyFolder
Set swFeat = model.FirstFeature
DoWhileNot swFeat IsNothingIf swFeat.GetTypeName2 = "CutListFolder"ThenSet swBodyFolder = swFeat.GetSpecificFeature2
Dim vBodies AsVariant
vBodies = swBodyFolder.GetBodies
Dim i AsIntegerIfNot IsEmpty(vBodies) ThenFor i = 0 To UBound(vBodies)
Dim swCutListBody As SldWorks.Body2
Set swCutListBody = vBodies(i)
If swApp.IsSame(swCutListBody, body) = swObjectEquality.swObjectSame ThenSet GetCutListFromBody = swFeat
ExitFunctionEndIfNextEndIfEndIfSet swFeat = swFeat.GetNextFeature
LoopEndFunctionFunction Contains(coll As Collection, item AsObject) AsBooleanDim i AsIntegerFor i = 1 To coll.CountIf coll.item(i) Is item Then
Contains = TrueExitFunctionEndIfNext
Contains = FalseEndFunction
Notifications
Join session by SOLIDWORKS and PDM API expret Artem Taturevych at 3DEXPERIENCE World 2025 on Feb 26 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more