Hide Table of Contents

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


End Sub


Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Insert Cavity Example (VBA)
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2012 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.