7 Replies Latest reply on Dec 26, 2013 5:41 AM by Nazar Chaykin

    Traverse assembly problem (Referenced config not same as config name)

    Michael Caulton

      I have a problem when traversing an assembly, the configuration name differs between using the Comp.ReferencedConfiguration and Config.Name methods

       

      I have simplified the code but it shows the problme I am having. Sample assembly attached too which has configurations etc setup.

       

      Macro prints swChildComp.ReferencedConfiguration + " vs. " + swChildConfig.Name into debugger window. I would expect these to be the same but they are not as per the debugger results shown below code. Some are the same some are not.

       

      '**********************************************

      Sub main()

       

          Dim swApp As SldWorks.SldWorks

          Dim swModel As ModelDoc2

          Dim swAssy As AssemblyDoc

         

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

       

          If swModel.GetType = SwConst.swDocASSEMBLY Then

              Set swAssy = swModel ' Top level assembly

              TransverseComponent swApp, swAssy, 1

          End If

       

      End Sub

       

      Sub TransverseComponent(swApp As SldWorks.SldWorks, swAssy As AssemblyDoc, nLevel As Long)

         

          Dim vChildComp As Variant

          Dim i As Long

          Dim Indent As String

          Dim swChildComp As Component2

          Dim swChildModel As ModelDoc2

          Dim swChildConfig As Configuration

          Dim swChildAssy As AssemblyDoc

         

          For i = 1 To nLevel

              Indent = Indent + "  "

          Next i

         

          vChildComp = swAssy.GetComponents(True)

          For i = 0 To UBound(vChildComp)

         

              Set swChildComp = vChildComp(i)

              Set swChildModel = swChildComp.GetModelDoc2

              Set swChildConfig = swChildModel.GetActiveConfiguration

             

              ' Compare assembly reference name to model config name

              Debug.Print Indent + swChildComp.ReferencedConfiguration + " vs. " + swChildConfig.Name

             

              If swChildModel.GetType = swDocASSEMBLY Then

                  Set swChildAssy = swChildModel

                  TransverseComponent swApp, swChildAssy, nLevel + 1

              End If

             

          Next i

         

      End Sub

      '**********************************************

       

      DEBUGGER:

        HCC0102-L1090-MS vs. HCC0102-L1090-MS

          HCC0102-1-L1070-MS vs. HCC0102-1-L1070-MS

          HCC0102-2-MS vs. HCC0102-2-MG

          HCC0102-2-MS vs. HCC0102-2-MG

        HCC0118-L1700 vs. HCC0118-L1260

        HCC0119-L1700 vs. HCC0119-L1080

        HCC0301-L1700-MS vs. HCC0301-L1700-MS

          HCC0301-1-L1700-MS vs. HCC0301-1-L1700-MS

          HCC0101-4-L1700-MS vs. HCC0101-4-L2210-MS

        HCC0302-SS vs. HCC0302-MG

        HCC0102-L1090-MS vs. HCC0102-L1090-MS

          HCC0102-1-L1070-MS vs. HCC0102-1-L1070-MS

          HCC0102-2-MS vs. HCC0102-2-MG

          HCC0102-2-MS vs. HCC0102-2-MG

        HCC0301-L1700-MS vs. HCC0301-L1700-MS

          HCC0301-1-L1700-MS vs. HCC0301-1-L1700-MS

          HCC0101-4-L1700-MS vs. HCC0101-4-L2210-MS

        HCC0118-L1700 vs. HCC0118-L1260

        HCC0119-L1700 vs. HCC0119-L1080

        HCC0302-SS vs. HCC0302-MG

      '**********************************************

        • Re: Traverse assembly problem (Referenced config not same as config name)
          Artem Taturevych

          IComponent2::ReferencedConfiguration is the configuration used by a component. The one you setup in the assembly. It means that single document can has multiple instances of components in difference referenced configurations. IModelDoc2::GetActiveConfiguration is a configuration currently active in the document (the configuration which was active while saving the document). This is the one you change in Configuration Tree tab in Feature Manager Tree. It is only possible to have single active configuration per document at a time.

          ____________________________________________________

          Regards,

          Artem Taturevych, Application Engineer at Intercad (Australia)

           

          translationXpert – add-in to translate SolidWorks models

          myIntercad an integrated tool for SolidWorks Professionals

          LinkedIn SolidWorks API Education Group

          • Re: Traverse assembly problem (Referenced config not same as config name)
            Michael Caulton

            Thanks Ivana. Dont know how that one didnt come up in my searches.

             

            Here is my code someone else might find useful

             

            Code trolls through a SW assembly getting the configuration specific custom properties and printing them to the debugger.

             

            '****************************************************

            Sub main()

             

             

                Dim swApp As SldWorks.SldWorks

                Dim swModel As ModelDoc2

                Dim swAssy As AssemblyDoc

               

                Set swApp = Application.SldWorks

                Set swModel = swApp.ActiveDoc

             

             

                If swModel.GetType = SwConst.swDocASSEMBLY Then

                    Set swAssy = swModel

                    TransverseComponent swApp, swAssy, 1

                End If

             

             

            End Sub

             

             

            Sub TransverseComponent(swApp As SldWorks.SldWorks, swAssy As AssemblyDoc, nLevel As Long)

               

                Dim vChildComp As Variant

                Dim i As Long

                Dim bRet As Boolean

                Dim Indent As String

                Dim swChildComp As Component2

                Dim swChildModel As ModelDoc2

                Dim swChildAssy As AssemblyDoc

                Dim swChildModelDocExt As ModelDocExtension

                Dim swChildCustPropMngr As CustomPropertyManager

                Dim ChildConfigName As String

                Dim Val As String

                Dim ValOut As String

                Dim vPropNames As Variant

                Dim j As Long

               

                ' Indents the text so it looks pretty in the debug window

                For i = 1 To nLevel

                    Indent = Indent + "  "

                Next i

               

                vChildComp = swAssy.GetComponents(True)

                For i = 0 To UBound(vChildComp)

               

                    Set swChildComp = vChildComp(i)

                    Set swChildModel = swChildComp.GetModelDoc2

                   

                    ' Get referenced configuration name

                    ChildConfigName = swChildComp.ReferencedConfiguration ' name of configuration we are interested in

                    Set swChildModelDocExt = swChildModel.Extension

                    Set swChildCustPropMngr = swChildModelDocExt.CustomPropertyManager(ChildConfigName) ' Sets the custom prop manager to the referenced configuration

                    Debug.Print ChildConfigName

                   

                    ' Prints details of specific custom property within referenced configuration

                    swChildCustPropMngr.Get2 "Part Number", Val, ValOut ' Can use .Get4 if using SW2011 or newer

                    Debug.Print Indent + "  " + "Part Number" + " = " + ValOut 'Prints the configuration name as per the custom properties

                   

                    ' Prints details of all custom property within referenced configuration

                    If Not swChildCustPropMngr Is Nothing Then

                        swChildCustPropMngr.GetAll vPropNames, Empty, Empty

                        If Not IsEmpty(vPropNames) Then

                            For j = 0 To UBound(vPropNames)

                                swChildCustPropMngr.Get2 vPropNames(j), Val, ValOut

                                Debug.Print Indent + "  " + vPropNames(j) + " = " + ValOut

                            Next

                        End If

                    End If

                   

                    ' Traverses next level down if file is a SW assembly

                    If swChildModel.GetType = swDocASSEMBLY Then

                        Set swChildAssy = swChildModel

                        TransverseComponent swApp, swChildAssy, nLevel + 1

                    End If

                   

                Next i

               

            End Sub

            '****************************************************