Hide Table of Contents

Fire Notifications When Renaming Components Example (VB.NET)

This example shows how to fire notifications when you:

  • are about to rename a component.
  • rename a component.
' Preconditions:
' 1. Verify that these documents exist in public_documents\tutorial\api:
'    * beam_boltconnection.sldasm
'    * beam with holes.sldprt
' 2. 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.
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System.Runtime.InteropServices
Imports System
Imports System.Diagnostics
Imports System.Collections
Partial Class SolidWorksMacro
    Public WithEvents swAssy As AssemblyDoc
    Public Sub main()
        Dim swModel As ModelDoc2
        Dim swConfigMgr As ConfigurationManager
        Dim swConfig As Configuration
        Dim swRootComp As Component2
        Dim Children As Object
        Dim swChild As Component2
        Dim swSelMgr As SelectionMgr
        Dim swSelData As SelectData
        Dim ChildCount As Integer
        Dim oldName As String
        Dim newName As String
        Dim bOldSetting As Boolean
        Dim bRet As Boolean
        Dim i As Integer
        Dim openAssem As Hashtable
        swModel = swApp.ActiveDoc
        swAssy = swModel
        ' Set up event
        swAssy = swModel
        openAssem = New Hashtable
        swConfigMgr = swModel.ConfigurationManager
        swConfig = swConfigMgr.ActiveConfiguration
        swRootComp = swConfig.GetRootComponent3(True)
        bOldSetting = swApp.GetUserPreferenceToggle(swUserPreferenceToggle_e.swExtRefUpdateCompNames)
        swApp.SetUserPreferenceToggle(swUserPreferenceToggle_e.swExtRefUpdateCompNames, False)
        Children = swRootComp.GetChildren
        ChildCount = UBound(Children)
        swSelMgr = swModel.SelectionManager
        swSelData = swSelMgr.CreateSelectData
        For i = 0 To ChildCount
            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(swUserPreferenceToggle_e.swExtRefUpdateCompNames, bOldSetting)
    End Sub
    Sub AttachEventHandlers()
    End Sub
    Sub AttachSWEvents()
        AddHandler swAssy.PreRenameItemNotify, AddressOf Me.swAssy_PreRenameItemNotify
        AddHandler swAssy.RenameItemNotify, AddressOf Me.swAssy_RenameItemNotify
    End Sub
    Private Function swAssy_PreRenameItemNotify(ByVal EntityType As IntegerByVal oldName As StringByVal newName As StringAs Integer
        Debug.Print("PRE-NOTIFICATION - about to rename component: " & oldName)
    End Function
    Private Function swAssy_RenameItemNotify(ByVal EntityType As IntegerByVal oldName As StringByVal newName As StringAs Integer
        Debug.Print("NOTIFICATION - rename component: " & newName)
    End Function
    ''' <summary>
    ''' The SldWorks swApp variable is pre-assigned for you.
    ''' </summary>
    Public swApp As SldWorks
End Class

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:   Fire Notifications When Renaming Components Example (VB.NET)
*   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) 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.