koubaleite
Mechanical
- Nov 27, 2024
- 9
Hello, I have an assembly that contains subassemblies. Our goal is to select all parts within the assembly (and subassemblies). Thanks for any help, suggestion
Our current macro doesn't allow us to select parts within subassemblies.
Our current macro doesn't allow us to select parts within subassemblies.
Code:
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 ChildCount As Long
Dim OldName As String
Dim NewName As String
Dim bOldSetting As Boolean
Dim bRet As Boolean
Dim i As Long
Dim Codice As String
Dim Position As Long
Dim Description As String
' Get user input for Codice (fixed prefix) and Position (starting number)
Codice = InputBox("Enter Codice (fixed prefix):")
Position = CLng(InputBox("Enter starting Position number:"))
' Initialize SolidWorks application
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swConfigMgr = swModel.ConfigurationManager
Set swConfig = swConfigMgr.ActiveConfiguration
Set swRootComp = swConfig.GetRootComponent3(True)
' Save the current user preference setting for external reference updates
bOldSetting = swApp.GetUserPreferenceToggle(swExtRefUpdateCompNames)
swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, False
' Get all child components of the root component
Children = swRootComp.GetChildren
ChildCount = UBound(Children)
' Initialize selection manager and data
Set swSelMgr = swModel.SelectionManager
Set swSelData = swSelMgr.CreateSelectData
' Loop through all the components in the assembly
For i = 0 To ChildCount
Set swChild = Children(i)
' Get the model document for the child component
'Dim swChild As SldWorks.Component2
Set swChildModel = swChild.GetModelDoc2
' Select the component
bRet = swChild.Select4(False, swSelData, False)