4 Replies Latest reply on Mar 29, 2017 6:13 AM by Deepak Gupta

    Macro to verify child part metadata

    Matt Hunter

      Hello talented API community,

      I am fairly new to working with macros. I recently built a macro designed to validate data cards (at my company we have a lot of metadata that is carried with the part). I have built a function called DataCheck that works incredibly well and I added this bit of code below to allow the user to check all data cards when you have an assembly open. It works rather well currently, but is having issues when I run into subassemblies. It causes a debug error at this line "Set swChildModel = swChildComp.GetModelDoc2".

       

      I have a feeling it finds difficulty with child components of child components. I would love some help as I've stared at this for far too long. If anyone is feeling ambitious... I can certainly add more information.

       

      vChildComp = swModel.GetComponents(False)
          For j = 1 To UBound(vChildComp)

              Set swChildComp = vChildComp(j)
                  Set swChildModel = swChildComp.GetModelDoc2
                  PartFileName = swChildModel.GetPathName
                  Set swConfig = swChildModel.GetActiveConfiguration
                  result = DataCheck(PartFileName)
                  Next j

        • Re: Macro to verify child part metadata
          Peter Brinkhuis

          Hi Matt,

           

          I've added a bit so I could run the macro and commented out the data check itself. This is the result and it gives no error here. It also does nothing You can probably spot the difference with your code, otherwise just post some more of it.

           

          Btw: according to the spec GetActiveConfiguration is an old method and should be replaced.

           

          1. Option Explicit
          2. Sub main()
          3.     Dim swApp As SldWorks.SldWorks
          4.     Set swApp = Application.SldWorks
          5.   
          6.     Dim swModel As AssemblyDoc
          7.     Set swModel = swApp.ActiveDoc
          8.   
          9.     Dim vChildComp() As Object
          10.     Dim swChildComp As Component2
          11.     Dim swChildModel As ModelDoc2
          12.     Dim PartFileName As String
          13.     Dim swConfig As Object
          14.     Dim j As Integer
          15.       
          16.     vChildComp = swModel.GetComponents(False)
          17.     For j = 1 To UBound(vChildComp)
          18.         Set swChildComp = vChildComp(j)
          19.         Set swChildModel = swChildComp.GetModelDoc2
          20.         PartFileName = swChildModel.GetPathName
          21.         Set swConfig = swChildModel.GetActiveConfiguration
          22.         'result = DataCheck(PartFileName)
          23.       Next j
          24.   
          25.     MsgBox ("Done")
          26. End Sub
            • Re: Macro to verify child part metadata
              Matt Hunter

              Thanks Peter! It doesn't totally address my issue... I'll post more code. Essentially I wanted to have the macro first address the current part, so I have it run through the function first. If it is a part and not an assembly, then it'll exit the macro in the next section. Then I have its child parts run in the next section. The integer k is used later in the function. If I have a subassembly in the model, it'll have a debug error at this line: Set swConfig = swChildModel.ActiveConfiguration

               

               

              Dim swApp As SldWorks.SldWorks

              Set swApp = Application.SldWorks

              Dim swModel As AssemblyDoc

              Set swModel = swApp.ActiveDoc

              Dim vChildComp() As Object

              Dim swChildComp As Component2

              Dim swChildModel As ModelDoc2

              Dim PartFileName As String

              Dim swConfig As Object

              Dim j As Integer, k as Integer

              Dim swConfigMgr As SldWorks.ConfigurationManager

               

              Sub main()

               

              If swModel.GetType = swDocDRAWING Then
                  Exit Function
              End If

              Set swConfigMgr = swModel.ConfigurationManager
              Set swConfig = swConfigMgr.ActiveConfiguration

              k = 1

               

              PartFileName = swModel.GetPathName
              result = DataCheck(PartFileName)

               

              If swModel.GetType = swDocPART Then
                  Exit Function

              End If

               

                  vChildComp = swModel.GetComponents(False)
                  For j = 1 To UBound(vChildComp)

                      Set swChildComp = vChildComp(j)
                          Set swChildModel = swChildComp.GetModelDoc2
                          PartFileName = swChildModel.GetPathName
                          Set swConfig = swChildModel.ActiveConfiguration
                          k = k + 1
                          result = DataCheck(PartFileName)
                          Next j
                         
              End Function