7 Replies Latest reply on Jun 27, 2017 3:57 AM by Prabaharan Pichaiyan

    Get a particular Custom property and assign that to desired variable.

    Prabaharan Pichaiyan

      I need a macro to get a particular custom property in assembly and assign those value to desired variable.

      For example.

       

      I have 3 parts in my assembly. Need to get a "description" Custom property from each model and assign those values to new variable like

       

      A= (Part 1 Description value)

      B= (Part 2 Description value)

      C= (Part 3 Description value)

       

      Thank you

        • Re: Get a particular Custom property and assign that to desired variable.
          Viktor Bovzdarenko

          Hi Prabaharan,

          You can get all components in assembly: System.Object= IAssemblyDoc.GetComponents(ToplevelOnly) (example)
          and then get CustomPropertyManager for each component in assembly.

          After that you can get custom properties using ICustomPropertyManager (example)
          System.Integer= ICustomPropertyManager.Get5(FieldName, UseCached, ValOut, ResolvedValOut, WasResolved)
          And then use it for ant purpose.

          Regards

          Viktor

            • Re: Get a particular Custom property and assign that to desired variable.
              Prabaharan Pichaiyan

              Thank you for your reply.

               

              Still I'm New to API. So i don't know how to arrange those codes.

              I tried but its getting error.

               

              Thanks

                • Re: Get a particular Custom property and assign that to desired variable.
                  Deepak Gupta

                  Have you looked into the examples? Share the codes you tried with?

                    • Re: Get a particular Custom property and assign that to desired variable.
                      Prabaharan Pichaiyan

                      Thanks.

                       

                      This what I tried,

                       

                       

                      Option Explicit

                      Dim swApp As SldWorks.SldWorks

                       

                      Sub main()

                      Dim swModel As ModelDoc2

                       

                      Dim vComps As Variant

                      Dim swComp As SldWorks.Component2

                      Dim swAssy As SldWorks.AssemblyDoc

                      Dim i As Integer

                      Set swApp = Application.SldWorks

                      Set swModel = swApp.ActiveDoc

                       

                      updateProperty swModel

                      If swModel.GetType = swDocASSEMBLY Then

                          Set swAssy = swModel

                          vComps = swAssy.GetComponents(False)

                          For i = 0 To UBound(vComps)

                              Set swComp = vComps(i)

                              Set swModel = swComp.GetModelDoc2

                              updateProperty swModel

                          Next i

                      End If

                      End Sub

                      Function updateProperty(swModel As SldWorks.ModelDoc2) As Boolean

                      Dim cpm As CustomPropertyManager

                      Dim ValOut As String

                      Dim ResolvedValOut As String

                      Dim wasResolved As Boolean

                      Dim swCustPropMgr As SldWorks.CustomPropertyManager

                      Dim path As String, filename As String, Description As String

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

                      path = swModel.GetPathName

                      filename = Mid$(path, InStrRev(path, "\") + 1) ' With extension

                      filename = Left$(filename, InStrRev(filename, ".") - 1) ' Remove extension

                       

                      If InStr(Left(filename, 1), "t") = True Then

                          Description = UCase(Right(filename, Len(filename) - 7))

                      Else

                          'Description = UCase(Right(filename, Len(filename) - 6))

                      End If

                      Description = Replace(Description, "_", " ")

                      Set swCustPropMgr = swModel.Extension.CustomPropertyManager(Empty)

                      swCustPropMgr.Get5 "Description", False, ValOut, ResolvedValOut, wasResolved

                      Debug.Print ValOut

                      End Function

                       

                       

                       

                      And I got results of my Description values. But I dont know how to assign my result to a new variables like I said before

                      A= (Part1 Description)

                      B= (Part2 Description)

                      C= (Part3 Description)

                       

                       

                      Thanks

                        • Re: Get a particular Custom property and assign that to desired variable.
                          Deepak Gupta

                          The function you're using is not returning any value. So I would suggest to use that for updating description property only. But your function doesn't have the code line to update the description property. So add that as well.

                           

                          Further you can add the lines to get the property value in the main module itself and use as desired. Here is a quick example:

                           

                          Option Explicit
                          Dim swApp               As SldWorks.SldWorks
                          Dim swModel             As ModelDoc2
                          Dim swCustPropMgr       As SldWorks.CustomPropertyManager
                          Dim vComps              As Variant
                          Dim swComp              As SldWorks.Component2
                          Dim swAssy              As SldWorks.AssemblyDoc
                          Dim i                   As Integer
                          Dim ValOut              As String
                          Dim ResolvedValOut(100) As String
                          Dim wasResolved         As Boolean
                          Dim A, B, C             As String
                          Sub main()
                          
                          Set swApp = Application.SldWorks
                          Set swModel = swApp.ActiveDoc
                          updateProperty swModel
                          
                          If swModel.GetType = swDocASSEMBLY Then
                              Set swAssy = swModel
                              vComps = swAssy.GetComponents(False)
                              
                              For i = 0 To UBound(vComps)
                                  Set swComp = vComps(i)
                                  Set swModel = swComp.GetModelDoc2
                                  updateProperty swModel
                                  Set swCustPropMgr = swModel.Extension.CustomPropertyManager(Empty)
                                  swCustPropMgr.Get5 "Description", False, ValOut, ResolvedValOut(i), wasResolved
                             Next i
                          End If
                          
                          A = ResolvedValOut(0)
                          B = ResolvedValOut(1)
                          C = ResolvedValOut(2)
                          
                          Debug.Print A & " - " & B & " - " & C
                          
                          End Sub
                          
                          Function updateProperty(swModel As SldWorks.ModelDoc2) As Boolean
                          Dim cpm As CustomPropertyManager
                          Dim path As String, filename As String, Description As String
                          
                          Set cpm = swModel.Extension.CustomPropertyManager("")
                          path = swModel.GetPathName
                          
                          filename = Mid$(path, InStrRev(path, "\") + 1) ' With extension
                          filename = Left$(filename, InStrRev(filename, ".") - 1) ' Remove extension
                          
                          If InStr(Left(filename, 1), "t") = True Then
                              Description = UCase(Right(filename, Len(filename) - 7))
                          Else
                              'Description = UCase(Right(filename, Len(filename) - 6))
                          End If
                          Description = Replace(Description, "_", " ")
                          End Function