Create Split-body Feature Example (VBA)
This example shows how to create a split-body feature.
'------------------------------------------
'
' Precondition: Part document is open that
' contains
a body to split.
'
' Postcondition: Split body feature is created with
' the
split bodies consumed.
'
'------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swFeat As SldWorks.Feature
Dim swFeatMgr As SldWorks.FeatureManager
Dim swSplitBodyFeat As SldWorks.SplitBodyFeatureData
Dim boolstatus As Boolean
Sub main()
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swModelDocExt = swModel.Extension
Set
swSelMgr = swModel.SelectionManager
Set
swFeatMgr = swModel.FeatureManager
'Select
the cutting tool
boolstatus
= swModelDocExt.SelectByID2("Top
Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
'Perform
the pre-split operation (get bodies that will result from split operation)
Dim
vResultingBodies As Variant
vResultingBodies
= swFeatMgr.PreSplitBody
swModel.ClearSelection2 True
Dim
vBodiesToMark As Variant
Dim
vBodyNames As Variant
Dim
vBodyOrigins As Variant
Dim
bodiesToMark(1) As Object
Dim
bodyNames(1) As String
Dim
bodyOrigins(1) As Object
'
Select the origin of the body to save
boolstatus
= swModelDocExt.SelectByID2("",
"VERTEX", -0.07260453968254, 0.07175701587302, 0.05, False,
0, Nothing, 0)
'Set
up the arrays for the post-split operation
Set
bodiesToMark(0) = vResultingBodies(0)
'Save
body marked 0
'Substitute
the name of the actual folder where to save the body
bodyNames(0)
= "e:\splitbodyconsume\Body1.sldprt"
Set
bodyOrigins(0) = swSelMgr.GetSelectedObject6(1,
0)
Set
bodiesToMark(1) = vResultingBodies(1)
'Do
not save body marked 1
bodyNames(1)
= ""
'If
you do not want to assign origin, set it to nothing so that the default
origin is used
Set
bodyOrigins(1) = Nothing
vBodiesToMark
= bodiesToMark
vBodyNames
= bodyNames
vBodyOrigins
= bodyOrigins
'Perform
the post-split operation (create the split-body feature)
Set
swFeat = swFeatMgr.PostSplitBody((vBodiesToMark),
True, (vBodyOrigins), (vBodyNames))
If
(Not swFeat Is Nothing) Then
Debug.Print
"Name of split-body feature = " & swFeat.Name
Set
swSplitBodyFeat = swFeat.GetDefinition
Debug.Print
"Is the split-body feature consumed = " & swSplitBodyFeat.Consume
Debug.Print
" "
End
If
End Sub