Hide Table of Contents

Fire Notifications When Renaming Components Example (VBA)

This example shows how to fire notifications when you:

  • are about to rename a component.
  • rename a component.
'---------------------------------------------------
' Preconditions:
' 1. Copy and paste Main in the main module.
' 2. Click Insert > Class module and copy and paste Class1 in the class
'    module.
' 3. Verify that these documents exist in public_documents\tutorial\api:
'    * beam_boltconnection.sldasm
'    * beam with holes.sldprt
' 4. Open the Immediate window.
'
' Postconditions:
' 1. Open public_documents\tutorial\api\beam_boltconnection.sldasm.
' 2. Fires pre-notification before appending
'    123 to each assembly component's name.
' 3. Fires notification when appending 123 to
'    each assembly component's name.
' 4. Examine the FeatureManager design tree and the
'    Immediate window.
'
' NOTE: Because the assembly is used elsewhere, do
' not save changes.
'---------------------------------------------------
'Main
Option Explicit
Sub main()

    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swConfigMgr As SldWorks.ConfigurationManager
    Dim swConfig As SldWorks.Configuration
    Dim swRootComp  As SldWorks.Component2
    Dim Children As Variant
    Dim swChild As SldWorks.Component2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swSelData As SldWorks.SelectData
    Dim swAssy As SldWorks.AssemblyDoc
    Dim swAssyEvents As Class1
    Dim ChildCount As Long
    Dim oldName As String
    Dim newName As String
    Dim bOldSetting As Boolean
    Dim bRet As Boolean
    Dim i As Long    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssy = swModel    
    'Set up events
    Set swAssy = swModel
    Set swAssyEvents = New Class1
    Set swAssyEvents.swAssy = swApp.ActiveDoc    
    Set swConfigMgr = swModel.ConfigurationManager
    Set swConfig = swConfigMgr.ActiveConfiguration
    Set swRootComp = swConfig.GetRootComponent3(True)
    bOldSetting = swApp.GetUserPreferenceToggle(swExtRefUpdateCompNames)
    swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, False
    Children = swRootComp.GetChildren
    ChildCount = UBound(Children)
    Set swSelMgr = swModel.SelectionManager
    Set swSelData = swSelMgr.CreateSelectData
    For i = 0 To ChildCount
        Set swChild = Children(i)
        ' Changing component name requires component to be selected
        bRet = swChild.Select4(False, swSelData, False)
        oldName = swChild.Name2
        newName = oldName & " 123"
        swChild.Name2 = newName
    Next i
    swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, bOldSetting

End Sub
'Class1
Option Explicit
Public WithEvents swAssy As SldWorks.AssemblyDoc
Private Function swAssy_PreRenameItemNotify(ByVal EntityType As Long, ByVal oldName As String, ByVal newName As String) As Long
    Debug.Print "PRE-NOTIFICATION - about to rename component: " & oldName
End Function
Private Function swAssy_RenameItemNotify(ByVal EntityType As Long, ByVal oldName As String, ByVal newName As String) As Long
    Debug.Print "NOTIFICATION - rename component: " & newName
End Function

 



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

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Fire Notifications When Renaming Components Example (VBA)
*Comment:  
*   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:



x

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
x

Web Help Content Version: API Help (English only) 2017 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.