Preview | SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
PPPichaiyan Prabaharan19/05/2020
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 SystemImports SolidWorks.Interop.sldworksImports SolidWorks.Interop.swconstPublic Class Form1'DeclarationsDim SWApp As SldWorksDim FileName As StringDim Type As IntegerDim Options As IntegerDim Configuration As StringDim Errors As IntegerDim Warnings As IntegerDim SWModel As ModelDoc2Dim swAssy As AssemblyDocDim swConf As ConfigurationDim swRootComp As Component2Dim bRet As BooleanDim bOldSetting As BooleanDim errorsRename As LongDim errorsSave As LongDim saveStatus As BooleanPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click' Get/Create SolidWorksTrySWApp = GetObject(Nothing, "SldWorks.application")Catch ex As ExceptionSWApp = CreateObject("SldWorks.application")End TryOpendocument()SWApp.Visible = TrueSWModel = SWApp.ActiveDocswConf = SWModel.GetActiveConfigurationswRootComp = swConf.GetRootComponent3(True)TraverseComponent(swRootComp, 1)saveStatus = SWModel.Save3(swSaveAsOptions_e.swSaveAsOptions_Silent + swSaveAsOptions_e.swSaveAsOptions_SaveReferenced, Errors, Warnings)SWModel.Rebuild(True)If saveStatus = False ThenMessageBox.Show("Document not saved.!")ElseMessageBox.Show("Document Saved.!")End IfDim result3 As DialogResult = MessageBox.Show("Process Done!", "AEM Assembly Renamer Automation ", MessageBoxButtons.OK, MessageBoxIcon.Information)End SubPublic Function Opendocument() As SolidWorks.Interop.sldworks.ModelDoc2FileName = Assembly_location_txt.Text.ToString()Type = SolidWorks.Interop.swconst.swDocumentTypes_e.swDocASSEMBLYOptions = SolidWorks.Interop.swconst.swOpenDocOptions_e.swOpenDocOptions_SilentConfiguration = ""SWModel = SWApp.OpenDoc6(FileName, Type, Options, Configuration, Errors, Warnings)If Errors <> 0 OrElse Warnings <> 0 ThenMessageBox.Show($"Error: {Errors}, Warning: {Warnings} ")End IfReturn SWModelEnd FunctionSub TraverseComponent(swComp As Component2, nLevel As Long)Dim vChildComp As ObjectDim swChildComp As Component2Dim sPadStr As String = ""Dim i As LongDim swChildModel As ModelDoc2Dim NewName As StringDim RunningNum As Long = 1000Dim swModelDoc As ModelDoc2Dim swSelMgr As SelectionMgrDim swSelData As SelectDataFor i = 0 To nLevel - 1sPadStr = sPadStr + " "NextvChildComp = swComp.GetChildrenFor i = 0 To UBound(vChildComp)NewName = "Modified"NewName = NewName & RunningNum.ToStringswChildComp = vChildComp(i)swChildModel = swChildComp.GetModelDocIf swChildModel.GetType = swDocumentTypes_e.swDocPART ThenswChildComp.Select2(False, 0)swModelDoc = swChildComp.GetModelDoc2swSelMgr = SWModel.SelectionManagerswSelData = swSelMgr.CreateSelectDatabRet = swChildComp.Select4(False, swSelData, False)SWModel.Extension.RenameDocument(NewName)RunningNum = RunningNum + 1Result_Txt.AppendText(vbNewLine & swChildComp.Name2)ElseIf swChildModel.GetType = swDocumentTypes_e.swDocASSEMBLY ThenswChildComp.Select2(False, 0)swModelDoc = swChildComp.GetModelDoc2swSelMgr = SWModel.SelectionManagerswSelData = swSelMgr.CreateSelectDatabRet = swChildComp.Select4(False, swSelData, False)SWModel.Extension.RenameDocument(NewName)RunningNum = RunningNum + 1Result_Txt.AppendText(vbNewLine & swChildComp.Name2)End IfTraverseComponent(swChildComp, nLevel + 1)NextSWModel.ForceRebuild3(True)End SubEnd Class
Thanks
Prabaharan