Create Split-body Feature Example (VBA)
This example shows how to create a split-body feature.
'----------------------------------------------------------------------------
' Preconditions:
' 1. Open a part document that contains a body that is bisected by
' Top Plane.
' 2. Verify that c:\temp exists.
' 3. Open an Immediate window.
'
' Postconditions:
' 1. Creates split-body feature, Split1.
' 2. Saves a split body to c:\temp\Body1.sldprt.
' 3. Inspect the Immediate window, FeatureManager design tree, graphics area,
' and c:\temp.
'---------------------------------------------------------------------------
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)
'Get bodies created by splitting the part
Dim vResultingBodies As Variant
vResultingBodies = swFeatMgr.PreSplitBody2
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
'Set up the arrays for the post-split operation
' Specify the origins of the bodies to save;
specify Nothing to use the default origins
Set bodyOrigins(0) = Nothing
Set bodyOrigins(1) = Nothing
Set bodiesToMark(0) = vResultingBodies(0)
Set bodiesToMark(1) = vResultingBodies(1)
'Save the body marked 0
'Substitute the name of the actual folder where to save the
body
bodyNames(0) = "c:\temp\Body1.sldprt"
'Do not save body marked 1
bodyNames(1) = ""
vBodiesToMark = bodiesToMark
vBodyNames = bodyNames
vBodyOrigins = bodyOrigins
'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