AnsweredAssumed Answered

Macro to rename all components in assembly by running number.

Question asked by Pichaiyan Prabaharan on May 19, 2020
Latest reply on May 23, 2020 by Peter Brinkhuis

Hi all,

I'm trying to rename all Sub-assembly and its components with running no.

But I got below issue,

   1. Its processing all components, but its not renaming all.

   2. Its not processing in order (Like Top to bottom).

Please help me to fix this..

 

Codes are here.

 

Imports System
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst

Public Class Form1

    'Declarations

    Dim SWApp As SldWorks
    Dim FileName As String
    Dim Type As Integer
    Dim Options As Integer
    Dim Configuration As String
    Dim Errors As Integer
    Dim Warnings As Integer
    Dim SWModel As ModelDoc2
    Dim swAssy As AssemblyDoc
    Dim swConf As Configuration
    Dim swRootComp As Component2
    Dim bRet As Boolean
    Dim bOldSetting As Boolean
    Dim errorsRename As Long
    Dim errorsSave As Long
    Dim saveStatus As Boolean

 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ' Get/Create SolidWorks

        Try
            SWApp = GetObject(Nothing"SldWorks.application")
        Catch ex As Exception
            SWApp = CreateObject("SldWorks.application")
        End Try

        Opendocument()
        SWApp.Visible = True
        SWModel = SWApp.ActiveDoc
        swConf = SWModel.GetActiveConfiguration
        swRootComp = swConf.GetRootComponent3(True)

        TraverseComponent(swRootComp1)

        saveStatus = SWModel.Save3(swSaveAsOptions_e.swSaveAsOptions_Silent + swSaveAsOptions_e.swSaveAsOptions_SaveReferenced, Errors, Warnings)

        SWModel.Rebuild(True)

        If saveStatus = False Then
            MessageBox.Show("Document not saved.!")
        Else
            MessageBox.Show("Document Saved.!")
        End If

        Dim result3 As DialogResult = MessageBox.Show("Process Done!""AEM Assembly Renamer Automation ", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

    

    Public Function Opendocument() As SolidWorks.Interop.sldworks.ModelDoc2

        FileName = Assembly_location_txt.Text.ToString()
        Type = SolidWorks.Interop.swconst.swDocumentTypes_e.swDocASSEMBLY
        Options = SolidWorks.Interop.swconst.swOpenDocOptions_e.swOpenDocOptions_Silent
        Configuration = ""

        SWModel = SWApp.OpenDoc6(FileName, Type, Options, Configuration, Errors, Warnings)

        If Errors <> 0 OrElse Warnings <> 0 Then

            MessageBox.Show($"Error: {Errors}, Warning: {Warnings} ")
        End If

        Return SWModel

    End Function
   
    
    
    Sub TraverseComponent(swComp As Component2, nLevel As Long)

        Dim vChildComp As Object
        Dim swChildComp As Component2
        Dim sPadStr As String = ""
        Dim i As Long
        Dim swChildModel As ModelDoc2
        Dim NewName As String
        Dim RunningNum As Long = 1000
        Dim swModelDoc As ModelDoc2
        Dim swSelMgr As SelectionMgr
        Dim swSelData As SelectData

 

        For i = 0 To nLevel - 1
            sPadStr = sPadStr + " "
        Next

        vChildComp = swComp.GetChildren

        For i = 0 To UBound(vChildComp)

            NewName = "Modified"
            NewName = NewName & RunningNum.ToString
            swChildComp = vChildComp(i)
            swChildModel = swChildComp.GetModelDoc

            If swChildModel.GetType = swDocumentTypes_e.swDocPART Then

                swChildComp.Select2(False0)
                swModelDoc = swChildComp.GetModelDoc2
                swSelMgr = SWModel.SelectionManager
                swSelData = swSelMgr.CreateSelectData
                bRet = swChildComp.Select4(False, swSelData, False)
                SWModel.Extension.RenameDocument(NewName)
                RunningNum = RunningNum + 1
                Result_Txt.AppendText(vbNewLine & swChildComp.Name2)

            ElseIf swChildModel.GetType = swDocumentTypes_e.swDocASSEMBLY Then

                swChildComp.Select2(False0)
                swModelDoc = swChildComp.GetModelDoc2
                swSelMgr = SWModel.SelectionManager
                swSelData = swSelMgr.CreateSelectData
                bRet = swChildComp.Select4(False, swSelData, False)
                SWModel.Extension.RenameDocument(NewName)
                RunningNum = RunningNum + 1
                Result_Txt.AppendText(vbNewLine & swChildComp.Name2)

            End If

            TraverseComponent(swChildComp, nLevel + 1)
        Next

        SWModel.ForceRebuild3(True)

           End Sub

 

End Class

 

 

Thanks

Prabaharan

Outcomes