Insert Surface-cut Feature Example (VBA)
This example shows how to insert a surface-cut feature.
'------------------------------------------------------------------------------
' Preconditions:
' 1. Verify that specified part to open exists.
' 2. Open the Immediate window.
'
' Postconditions:
' 1. Opens the part whose intersecting solid bodies to cut with a plane.
' 2. Creates a plane named Plane1.
' 3. Selects Plane1 to cut all intersecting solid bodies.
' 4. Inserts the surface-cut feature, which cuts all intersecting
' solid bodies by the plane.
' 5. Examine the graphics area and Immediate window.
'
' NOTE: Because this part document is used elsewhere, do not save changes.
'------------------------------------------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swFeature As SldWorks.Feature
Dim swFeatureManager As SldWorks.FeatureManager
Dim swRefPlane As SldWorks.RefPlane
Dim swSurfaceCutFeature As SldWorks.SurfCutFeatureData
Dim fileName as String
Dim status As Boolean
Dim errors As Long, warnings As Long
Sub main()
Set swApp = Application.SldWorks
' Open part to cut with a plane
fileName = "C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2018\samples\tutorial\multibody\multi_inter.sldprt"
Set swModel = swApp.OpenDoc6(FileName, swDocPart, swOpenDocOptions_Silent, "", errors, warnings)
Set swModelDocExt = swModel.Extension
' Create and select the plane to cut
' all intersecting solid bodies in the part
status = swModelDocExt.SelectByID2("Front", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
Set swFeatureManager = swModel.FeatureManager
Set swRefPlane = swFeatureManager.InsertRefPlane(swRefPlaneReferenceConstraint_Distance, 0.045, 0, 0, 0, 0)
status = swModelDocExt.SelectByID2("Plane1", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
' Insert a surface-cut feature that cuts
' all intersecting solid bodies
Set swFeature = swFeatureManager.InsertCutSurface(False, 0, False, True, Nothing, errors)
Debug.Print ("Were any errors generated by the surface cut (0 = no errors)? " & errors)
Set swSurfaceCutFeature = swFeature.GetDefinition
' Get surface-cut feature and some properties
Debug.Print ("Name of surface-cut feature: " & swFeature.Name)
Debug.Print (" Is feature scope on? " & swSurfaceCutFeature.FeatureScope)
Debug.Print (" Number of bodies cut by the plane: " & swSurfaceCutFeature.GetFeatureScopeBodiesCount)
Debug.Print (" Is auto-select on? " & swSurfaceCutFeature.AutoSelect)
End Sub