11 Replies Latest reply on Aug 2, 2018 2:27 AM by Deepak Gupta

    Macro to add parent assembly(ies) names to a custom property

    Jesse Niemand

      I'll preface this by saying that I am very green to the Solidworks API, but do have some coding experience.

       

      I am trying to come up with a macro that will find every assembly that a part or subassembly resides in and add the parent assembly name(s) to a custom property called "UsedOn". If the part or subassembly reside in multiple parents, all parent names must be added to the custom property separated by a single space. The macro can either be run on the top level assembly or individually on every file in a directory, that doesn't really matter to me. Can anyone offer up any suggestions?

        • Re: Macro to add parent assembly(ies) names to a custom property
          Josh Brady

          You should probably use the Document Manager API for this.  You need to request a license key, which is free.

           

          Say part resides in Sub1 which resides in Sub2 which resides in Top.

           

          Does Part need to list Sub1, Sub2, and Top, or just Sub1?

           

          Parts don't "know" where they are used.  Only the place where they're used knows.  So for any given part, you will have to examine every assembly file to see if it is referenced there.  You need to understand your file structure because you have to tell your macro how to find assemblies.

          • Re: Macro to add parent assembly(ies) names to a custom property
            Deepak Gupta

            You can use the traverse component example and can go n level deep from the top level assembly. On every traverse, get the parent name first and add the property. If model is used in more than one place, then add the additional name.

             

            I've made a similar paid application for a client, so I'm unable to share that for free.

              • Re: Macro to add parent assembly(ies) names to a custom property
                Jesse Niemand

                I've found a previous example you did for someone and have it partially working. I got it to populate the "USEDON" field with the top level assembly name in components 'n' levels deep, but I'm struggling to figure out how to get only the immediate parent assembly name populated into the field for the subassemblies. The red text is where I'm struggling.

                 

                ```

                Option Explicit

                Dim swApp As SldWorks.SldWorks

                Dim swModel As ModelDoc2

                Dim vComps As Variant

                Dim swComp As SldWorks.Component2

                Dim swAssy As SldWorks.AssemblyDoc

                Dim i As Integer

                Dim sAssyName As String

                Sub main()

                Set swApp = Application.SldWorks

                Set swModel = swApp.ActiveDoc

                 

                 

                Debug.Print "Top Level Assembly: " & swModel.GetTitle

                 

                 

                updateProperty swModel, sAssyName

                 

                If swModel.GetType = swDocASSEMBLY Then

                    MsgBox "Supressed and Lightweight components will not be updated"

                    Set swAssy = swModel

                    vComps = swAssy.GetComponents(False)

                    For i = 0 To UBound(vComps)

                        Set swComp = vComps(i)

                        If swComp.GetSuppression = swComponentFullyResolved Then

                            If swComp.GetType = swDocASSEMBLY Then

                                sAssyName = swComp.GetTitle

                                Debug.Print "Sub-Assembly " & i; ": " & i & sAssyName

                            End If

                            Set swModel = swComp.GetModelDoc2

                            updateProperty swModel, sAssyName

                        End If

                    Next i

                Else

                    MsgBox "Please open an assembly"

                End If

                End Sub

                 

                Function updateProperty(swModel As SldWorks.ModelDoc2, mValue As String) As Boolean

                Dim cpm As CustomPropertyManager

                Set cpm = swModel.Extension.CustomPropertyManager("")

                cpm.Add3 "USEDON", swCustomInfoText, mValue, 1

                End Function

                ```

                 

                Also, I don't know how to get it to append text to the field rather than simply overwrite it.

                  • Re: Macro to add parent assembly(ies) names to a custom property
                    Jesse Niemand

                    I'm getting closer, this example outputs the data I'm looking for, but only if you select a component in the tree first, I do not want to have to select anything in the tree. The green code is outputs the data I'd like to populate into the custom property "USEDON". I need to figure out how to combine this and the code above to get the functionality I want.

                     

                    '--------------------------------------------------------

                    ' Preconditions:

                    ' 1. Open an assembly document.

                    ' 2. Select an assembly or subassembly component.

                    ' 3. Open the Immediate window.

                    '

                    ' Postconditions:

                    ' 1. Gets the parent component of the selected assembly

                    '    or subassembly component.

                    ' 2. Examine the Immediate window.

                    '-------------------------------------------------------

                    Option Explicit

                    Sub main()

                        Dim swApp As SldWorks.SldWorks

                        Dim swModel As SldWorks.ModelDoc2

                        Dim swSelMgr As SldWorks.SelectionMgr

                        Dim swConfigMgr As SldWorks.ConfigurationManager

                        Dim swConf As SldWorks.Configuration

                        Dim swComp As SldWorks.Component2

                        Dim swParentComp As SldWorks.Component2

                        Dim bRet As Boolean

                        Set swApp = Application.SldWorks

                        Set swModel = swApp.ActiveDoc

                        Set swSelMgr = swModel.SelectionManager

                        Set swComp = swSelMgr.GetSelectedObjectsComponent4(1, -1)

                        ' Returns Nothing if selected component is a top-level assembly component

                        Set swParentComp = swComp.GetParent

                        If Nothing Is swParentComp Then

                            Set swConfigMgr = swModel.ConfigurationManager

                            Set swConf = swConfigMgr.ActiveConfiguration

                            Set swParentComp = swConf.GetRootComponent3(True)

                        End If

                        Debug.Assert Not swParentComp Is Nothing

                        Debug.Print "File = " & swModel.GetPathName

                        Debug.Print "  Component = " & swComp.Name2 & " [" & swComp.GetPathName & "]"

                        Debug.Print "    Parent component = " & swParentComp.Name2 & " [" & swParentComp.GetPathName & "]"

                    End Sub

                • Re: Macro to add parent assembly(ies) names to a custom property
                  Jesse Niemand

                  This is working for the current open assembly, but I couldn't figure out how to use GetParent to traverse n levels deep. I'll just use #Task for now to batch the process.

                   

                  One thing I noticed is that it runs on every instance of a component in the tree and populates the filed multiple times with the same number, but I'd like it to only run on unique components, as to only have unique values in the populated field. Can anyone help with that?

                   

                   

                  'Preconditions: assembly is open

                  'Results: The parent assembly name will be appended to existing data in the USEDON custom property field.

                   

                   

                  'Written by Jesse Niemand

                   

                   

                  Option Explicit

                  Dim swApp               As SldWorks.SldWorks

                  Dim swModel             As ModelDoc2

                  Dim swComp              As SldWorks.Component2

                  Dim swParentComp        As SldWorks.Component2

                  Dim swAssy              As SldWorks.AssemblyDoc

                  Dim vComps              As Variant

                  Dim i                   As Integer

                  Dim sAssyName           As String

                  Dim sUsedOn             As String

                  Dim sAppend             As String

                  Dim ResovedValOut       As String

                  Dim WasResolved         As Boolean

                  Dim nErrors             As Long

                  Dim nWarnings           As Long

                   

                   

                  Sub main()

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                  sAssyName = swModel.GetTitle

                   

                  If swModel.GetType = swDocASSEMBLY Then

                      'MsgBox "Supressed and Lightweight components will not be updated"

                      Set swAssy = swModel

                      vComps = swAssy.GetComponents(True)

                      For i = 0 To UBound(vComps)

                          Set swComp = vComps(i)

                          If swComp.GetSuppression = swComponentFullyResolved Then

                              Set swModel = swComp.GetModelDoc2

                              updateProperty swModel, sAssyName

                          End If

                      Next i

                  Else

                      'MsgBox "Please open an assembly"

                  End If

                  End Sub

                   

                  Function updateProperty(swModel As SldWorks.ModelDoc2, mValue As String) As Boolean

                  Dim cpm As CustomPropertyManager

                  Set cpm = swModel.Extension.CustomPropertyManager("")

                  cpm.Get5 "USEDON", False, sUsedOn, ResovedValOut, WasResolved

                  sAppend = sUsedOn & mValue & " "

                  cpm.Add3 "USEDON", swCustomInfoText, sAppend, 1

                  swModel.ForceRebuild3 False

                  swModel.ShowNamedView2 "*Isometric", 7

                  swModel.ViewZoomtofit2

                  swModel.Save3 swSaveAsOptions_Silent, nErrors, nWarnings

                  End Function

                    • Re: Macro to add parent assembly(ies) names to a custom property
                      Deepak Gupta

                      You may need to add more check here

                       

                      If swModel.GetType = swDocASSEMBLY Then

                          'MsgBox "Supressed and Lightweight components will not be updated"

                          Set swAssy = swModel

                          vComps = swAssy.GetComponents(True)

                          For i = 0 To UBound(vComps)

                              Set swComp = vComps(i)

                              If swComp.GetSuppression = swComponentFullyResolved Then

                                  Set swModel = swComp.GetModelDoc2

                                          If swModel.GetType = swDocASSEMBLY

                                            updateProperty swModel, sAssyName

                                              'codes to traverse the sub assembly    

                                          else

                                               updateProperty swModel, sAssyName

                                          end if

                              End If

                          Next i

                      Else

                          'MsgBox "Please open an assembly"

                      End If

                      End Sub

                      For unique component issue, you can use collection method and compare each file with the collection and if it doesn't exits into collection then add/update property else skip.