10 Replies Latest reply on May 7, 2015 9:54 AM by Dan Sikorski

    Extract custom properties using Get:All

    Mark Coville

      Hello,

       

      I know how to get all custom properties my question in how do I pull out certain custom properties? I don't want to just export all of them, only certain ones. Should I just be using Get4?

       

      Thanks,

        • Re: Extract custom properties using Get:All
          Daniel Andersson

          If you know the name of the property you could simply use the Get5 method (SolidWorks 2014).

          Otherwise you could still get all properties and then loop thru all...

           

          The reason for choosing which one is based on your needs. E.g Get5 requires you to know exactly the property name.

          But if you choose to get all and loop, you could do operations with strings to sort out if the property is intresting or not.. E.g you want all properties that starts with A... 

           

          Here is an example from the API reference. The example shows how to use the different methods for the CustomPropertyManager.

            • Re: Extract custom properties using Get:All
              Mark Coville

              So using get4 or get5 would probably be the same # of lines as using getall and looping thru for certain properties?

                • Re: Extract custom properties using Get:All
                  Daniel Andersson

                  In general it is not possible to make that conclusion. Since it is dependent on what you will do with the properties and the data.

                   

                  Let's say you have 30 properties. To get all data and do something with it would be quite many rows of code, if using the Get5 method and process each property. GetAll2 is way more efficient since you could easily loop thru all data.

                   

                  Another scenario is if you have two properties as date format. You know the name if the properties and want the code to just get the values, remove them and add them again with the format as text. Then it is better to directly target them with Get5.

                    • Re: Extract custom properties using Get:All
                      Mark Coville

                      Ok so with the 30 properties scenario what would the loop look like using getall2?

                        • Re: Extract custom properties using Get:All
                          Daniel Andersson

                          Here is a simple example that will print all common (not configuration specific) custom properties to the immediate windows in VBA.

                           

                          Dim swApp As SldWorks.SldWorks

                          Dim swModel As SldWorks.ModelDoc2

                          Dim swExt As SldWorks.ModelDocExtension

                          Dim swCustPropMgr As SldWorks.CustomPropertyManager

                           

                          Sub main()

                           

                          Set swApp = Application.SldWorks

                          Set swModel = swApp.ActiveDoc

                          Set swExt = swModel.Extension

                          Set swCustPropMgr = swExt.CustomPropertyManager("")

                           

                          Dim vPropNames As Variant

                          Dim vPropTypes As Variant

                          Dim vPropValues As Variant

                          Dim vResolved As Variant

                          Dim iRetVal As Integer

                           

                          iRetVal = swCustPropMgr.GetAll2(vPropNames, vPropTypes, vPropValues, vResolved)

                           

                          For i = 0 To UBound(vPropNames)

                              Debug.Print "Name:     " & vPropNames(i)

                              Debug.Print "Type:     " & vPropTypes(i)

                              Debug.Print "Values:   " & vPropValues(i)

                              Debug.Print "Resolved: " & vResolved(i)

                              Debug.Print ""

                              'Etc etc... do what you want here with each property.

                          Next

                           

                          End Sub

                           

                          P.s iRetVal could be used to determine for how long the loop should keep going. I used Ubound of old habit.

                            • Re: Extract custom properties using Get:All
                              Mark Coville

                              Ok so at 'Etc etc... do what you want here with each property. is what I don't know how to do.

                               

                              ie I want to print the values of Description, Number, PartNumber, Material, etc, etc. How do I print those without going

                               

                              if vpropName(i) = "Description" then

                              debug.print vResolved(i)

                              end if

                               

                              If I have 30 properties and I have to do this 30 times would it not be just as easy to say

                               

                              boolstatus = swCPM.get4 ("Description", Type, valout, DescriptionRetVal)

                               

                               

                              Is there a quicker way to loop through all the variants?

                                • Re: Extract custom properties using Get:All
                                  Daniel Andersson

                                  To make it easier you could make an array that contains the names of the properties you want to get. This array could then be used in two different concepts. Both concepts will reduce the number of IF statements.

                                   

                                  First concept.

                                  Here is the idea that you add another loop in the loop for all properties. In this loop you will check the custom property at position i in the variant array and if it matches a value in your array it is printed.

                                   

                                  Second concept.

                                  You loop thru your array and use the Get5 get the data you want. One positive thing with this code is that you could handle exceptions if the property does not exist in a easy way. Since Get5 also returns a value to determine the output from the method.

                                   

                                  Both concepts will do the job and can be extended to handle exceptions.

                                   

                                  I recommend that you start with a method that you are comfortable with, since I do not know if you are new to coding or not.

                                   

                                  If you are new, do not let the efforts to find the best and optimal code kill the joy.

                                    • Re: Extract custom properties using Get:All
                                      Daniel Andersson

                                      Code examples [VBA]

                                       

                                      First concept:

                                      Dim swApp As SldWorks.SldWorks

                                      Dim swModel As SldWorks.ModelDoc2

                                      Dim swExt As SldWorks.ModelDocExtension

                                      Dim swCustPropMgr As SldWorks.CustomPropertyManager

                                       

                                      Sub main()

                                       

                                      Set swApp = Application.SldWorks

                                      Set swModel = swApp.ActiveDoc

                                      Set swExt = swModel.Extension

                                      Set swCustPropMgr = swExt.CustomPropertyManager("")

                                       

                                      Dim vPropNames As Variant

                                      Dim vPropTypes As Variant

                                      Dim vPropValues As Variant

                                      Dim vResolved As Variant

                                      Dim iRetVal As Integer

                                       

                                      Dim sPrint(2) As String

                                       

                                      sPrint(0) = "Description"

                                      sPrint(1) = "PartNo"

                                      sPrint(2) = "Material" 'Add further positions in the array if needed. Do not forget to change the dimension of the array sPrint

                                       

                                      iRetVal = swCustPropMgr.GetAll2(vPropNames, vPropTypes, vPropValues, vResolved)

                                       

                                      For i = 0 To UBound(vPropNames)

                                          For j = 0 To UBound(sPrint)

                                              If vPropNames(i) = sPrint(j) Then

                                                  Debug.Print "Name:     " & vPropNames(i)

                                                  Debug.Print "Type:     " & vPropTypes(i)

                                                  Debug.Print "Values:   " & vPropValues(i)

                                                  Debug.Print "Resolved: " & vResolved(i)

                                                  Debug.Print ""

                                                  'Etc etc... do what you want here with each property.

                                              End If

                                          Next

                                      Next

                                      End Sub

                                       

                                      Second concept:

                                      Dim swApp As SldWorks.SldWorks

                                      Dim swModel As SldWorks.ModelDoc2

                                      Dim swExt As SldWorks.ModelDocExtension

                                      Dim swCustPropMgr As SldWorks.CustomPropertyManager

                                       

                                      Sub main()

                                       

                                      Set swApp = Application.SldWorks

                                      Set swModel = swApp.ActiveDoc

                                      Set swExt = swModel.Extension

                                      Set swCustPropMgr = swExt.CustomPropertyManager("")

                                       

                                      Dim sPropValue As String

                                      Dim sResVal As String

                                      Dim bResolved As Boolean

                                      Dim iRetVal As Integer

                                       

                                      Dim sPrint(2) As String

                                       

                                      sPrint(0) = "Description"

                                      sPrint(1) = "PartNo"

                                      sPrint(2) = "Material" 'Add further positions in the array if needed. Do not forget to change the dimension of the array sPrint

                                       

                                      For i = 0 To UBound(sPrint)

                                          iRetVal = swCustPropMgr.Get5(sPrint(i), True, sPropValue, sResVal, bResolved)

                                           'Options of the Get5 method might be needed to be reconsidered.

                                          Debug.Print "Name:       " & sPrint(i)

                                          Debug.Print "Values:      " & sPropValue

                                          Debug.Print "ResolvedVal: " & sResVal

                                          Debug.Print "WasResolved: " & bResolved

                                          'Etc etc... do what you want here with property.

                                      Next

                                      End Sub