Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations MintJulep on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select parts of subassemblies

koubaleite

Mechanical
Nov 27, 2024
21
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.
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)
 
Solution
I finally found the solution. I still don't know how and why because when I test the same code on another computer it does work without me adding these lines of codes.

So from the API Help for Name2 property, it is said :

If you are setting the name of a component:

  • Before executing a name change, this property checks the swExtRefUpdateCompNames setting. If swExtRefUpdateCompNames is true, then the name change fails; if swExtRefUpdateCompNames is false, then the name change succeeds. Use ISldWorks::GetUserPreferenceToggle to change the swExtRefUpdateCompNames setting. Also, remember that some special characters are reserved by SOLIDWORKS, so be sure to use valid characters in the new...
Replies continue below

Recommended for you

swComp.Name2 gives component name followed by it's instance id. So in your case, if a component is used more than once (for e.g. pattern components), the codes may give error. So you may need to do 2 things:

1. Get a unique list of the components (parts) you want to rename.
2. Use file part name and the old name instead of swComp.Name2
Thanks for the suggestion !
 
I finally found the solution. I still don't know how and why because when I test the same code on another computer it does work without me adding these lines of codes.

So from the API Help for Name2 property, it is said :

If you are setting the name of a component:

  • Before executing a name change, this property checks the swExtRefUpdateCompNames setting. If swExtRefUpdateCompNames is true, then the name change fails; if swExtRefUpdateCompNames is false, then the name change succeeds. Use ISldWorks::GetUserPreferenceToggle to change the swExtRefUpdateCompNames setting. Also, remember that some special characters are reserved by SOLIDWORKS, so be sure to use valid characters in the new name.

So I just added this :
Code:
        ' swUserPreferenceToggle_e.swExtRefUpdateCompNames must be set to
        ' False to change the name of a component using IComponent2::Name2
        swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swExtRefUpdateCompNames, False

But from now I will make sure all files are being renamed properly so I won't have to deal with this again.

Thanks everyone !
 
Solution
Are you using any type of PDM? If any of the parts are checked out by another user, it will not let you rename.
 

Part and Inventory Search

Sponsor