Insert Cavity Example (VBA)
This example shows how to insert a cavity in a mold base.
'---------------------------------------------------
'
' Preconditions:
' (1)
Assembly is open.
' (2)
Assembly is fully resolved.
' (3)
Three components are selected.
' (4)
First selected component is mold base.
' (5)
Second selected component is first core.
' (6)
Third selected component is second core.
' (7)
At least one of the cores intersects with mold base.
'
' Postconditions: Cavity is created in first selected
component (mold base).
'
' NOTE: If there
are no intersections, then cavity is not created.
'
'-----------------------------------------------------
Option Explicit
Public Enum swEditPartCommandStatus_e
swEditPartFailure
= -1
swEditPartAsmMustBeSaved
= -2
swEditPartCompMustBeSelected
= -3
swEditPartCompMustBeResolved
= -4
swEditPartCompMustHaveWriteAccess
= -5
swEditPartSuccessful
= 0
swEditPartCompNotPositioned
= &H1
End Enum
Public Enum swCavityScaleType_e
swAboutCentroid
= 0
swAboutOrigin
= 1
swAboutMoldBaseOrigin
= 2
swAboutCoordinateSystem
= 3
End Enum
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swAssy As
SldWorks.AssemblyDoc
Dim
swMoldBaseComp As
SldWorks.Component2
Dim
swCoreComp1 As
SldWorks.Component2
Dim
swCoreComp2 As
SldWorks.Component2
Dim
nRetval As
Long
Dim
nInfo As
Long
Dim
bRet As
Boolean
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swAssy = swModel
Set
swSelMgr = swModel.SelectionManager
Set
swMoldBaseComp = swSelMgr.GetSelectedObjectsComponent2(1)
Set
swCoreComp1 = swSelMgr.GetSelectedObjectsComponent2(2)
Set
swCoreComp2 = swSelMgr.GetSelectedObjectsComponent2(3)
Debug.Print
"File = " & swModel.GetPathName
Debug.Print
" Cavity
=
" & swMoldBaseComp.Name2
Debug.Print
" Core1
=
" & swCoreComp1.Name2
Debug.Print
" Core2
=
" & swCoreComp2.Name2
swModel.ClearSelection2 True
bRet
= swMoldBaseComp.Select2(False,
0): Debug.Assert bRet
nRetval
= swAssy.EditPart2(True, True,
nInfo)
Debug.Assert
swEditPartSuccessful = nRetval
Debug.Assert
swEditPartFailure <> nInfo
bRet
= swCoreComp1.Select2(True, 0):
Debug.Assert bRet
bRet
= swCoreComp2.Select2(True, 0):
Debug.Assert bRet
swAssy.InsertCavity4 0#, 0, 0#, True, swAboutCentroid,
0
swAssy.EditAssembly
End Sub
'---------------------------------------------