Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross 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
22
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

What is your end goal to select the parts?

You can use IAssemblyDoc.GetComponents wtih False option to get all the assembly component. Then you can get the modeldoc for each component, get the model type, and select only the parts.
 
What is your end goal to select the parts?

You can use IAssemblyDoc.GetComponents wtih False option to get all the assembly component. Then you can get the modeldoc for each component, get the model type, and select only the parts.
Thanks, I want to rename parts name. I did as you suggested but the parts are not being renamed. Where did I get it wrong ?

Code:
Option Explicit

Sub main()
    ' Declare necessary variables
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swAssembly As SldWorks.AssemblyDoc
    Dim swComponents As Variant
    Dim swComp As SldWorks.Component2
    Dim swPartModel As SldWorks.ModelDoc2
    Dim OldName As String
    Dim NewName As String
    Dim bOldSetting 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

    If swModel Is Nothing Or swModel.GetType <> swDocASSEMBLY Then
        MsgBox "Please open an assembly to run this macro.", vbExclamation, "No Assembly Open"
        Exit Sub
    End If

    Set swAssembly = swModel

    ' Save the current user preference setting for external reference updates
    bOldSetting = swApp.GetUserPreferenceToggle(swExtRefUpdateCompNames)
    swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, False

    ' Get all components of the assembly, including those in subassemblies
    swComponents = swAssembly.GetComponents(False)

    ' Loop through all components
    For i = LBound(swComponents) To UBound(swComponents)
        Set swComp = swComponents(i)

        ' Check if the component is a part (not a subassembly)
        Set swPartModel = swComp.GetModelDoc2
        If Not swPartModel Is Nothing And swPartModel.GetType = swDocPART Then
            ' Get the old name of the part
            OldName = swComp.Name2
            Description = OldName  ' Use the current name as description

            ' Construct the new name
            NewName = Codice & "_" & CStr(Position) & "_" & Description

            ' Debug output
            Debug.Print "Old name = " & OldName
            Debug.Print "New name = " & NewName
            Debug.Print ""

            ' Rename the part
            swComp.Name2 = NewName

            ' Increment the position for the next part
            Position = Position + 1
        End If
    Next i

    ' Restore the original user preference setting
    swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, bOldSetting
End Sub
 
Do you need to open the part file to rename it? I ran into a similar issue, not renaming though, and it turned out that I needed to open the file in order to make the change.
 
Do you need to open the part file to rename it? I ran into a similar issue, not renaming though, and it turned out that I needed to open the file in order to make the change.
Oh you are right. I might need to open the part to rename it. The example I found in the API help section doesn't need to open the part but seems like I must open it first. I will look at it again and let you know if that works. Thanks for the help !

Edit : I am able to rename parts using the first macro without opening
 
Last edited:
Curious how big is the assy? How many parts? If not too big, you can just rename parts in the feature tree.
 
Curious how big is the assy? How many parts? If not too big, you can just rename parts in the feature tree.
It depends. But most of the assemblies, can contains up to 50 parts. Currently the one I am working on, has 21 parts.
 
Google for Bibotac. With it you can manage Sw Files without breaking links.
 
Are you trying to rename your components automatically using a macro? If not, you will need to open the top level assembly and "save" each of your components to a different name.

Best regards,

Alex
 
Are you trying to rename your components automatically using a macro? If not, you will need to open the top level assembly and "save" each of your components to a different name.

Best regards,

Alex
Yes I am trying to rename the components using a macro
 

koubaleite,​

Is it common at your company to rename files? If so, then a macro may worth figuring out.
If not, and it's only a couple assemblies and parts, quicker to just rename each one.
 

koubaleite,​

Is it common at your company to rename files? If so, then a macro may worth figuring out.
If not, and it's only a couple assemblies and parts, quicker to just rename each one.
Yeah it's common, reason why we are opting for the macro
 
If you need to rename your Solidworks files all the time, then there is something wrong with your processes.

Best regards,

Alex
 
If you need to rename your Solidworks files all the time, then there is something wrong with your processes.

Best regards,

Alex
That's right ! We are making changes to improve processes
 
If I were you, I would rename them from bottom-up per assembly rather than transverse to sub- and sub-sub assemblies, etc.

Best regards,

Alex
 
I always properly name the files as I go. I pull P/N's, save them in their own folders in PDM.
It seems you are naming them random names then going back later to give them P/N's?
 
Like any other 3D software, Solidworks is object-oriented. Name of each file is like "social security" number of a person. It needs to be fixed and preferably be set to "Stone".

Hi, koubaleite:

Are you trying to develop a generic macro for all assemblies? If that is the case, then you need to consider an assembly may contain multiples sub-assemblies who may contain their own sub-sub-assemblies. There are better ways to rename them if you just want to rename one assembly with one sub-assembly.

Best regards,

Alex
 
I always properly name the files as I go. I pull P/N's, save them in their own folders in PDM.
It seems you are naming them random names then going back later to give them P/N's?
Yes do make it simpler to other people that might use the file after
 
Thanks, I want to rename parts name. I did as you suggested but the parts are not being renamed. Where did I get it wrong ?
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
 

Part and Inventory Search

Sponsor