7 Replies Latest reply on Jun 18, 2018 4:43 AM by Luke Jones

    Adding Custom Properties to multiple files

    Luke Jones

      I am trying to add the custom property “Finished Length” to a batch of files. Some files have the custom property already and some do not. Some of the files that have the custom property have a value for that custom property. I want to add “Finished Length” to all the parts that don’t already have it, but I don’t want to remove the value present in the files that already have “Finished Length”

       

      The example below shows what I am trying to achieve. The top section is what we have currently have and the bottom section is what we are aiming for.

       

      Part Name – Custom Property – Value

      Part A – BLANK - BLANK

      Part B – “Finished Length” - BLANK

      Part C – “Finished Length” – 123

       

      Part Name – Custom Property – Value

      Part A – “Finished Length” - BLANK

      Part B – “Finished Length” - BLANK

      Part C – “Finished Length” – 123

       

      I have found a couple of macros that do something similar but they all overwrite the existing values with a blank string. I know that if I use the Custom property tab builder and apply the form that I create to a part, it adds the custom property if it doesn’t exist but does not overwrite existing ones. I just need to do this on a mass scale. Hopefully I can replicate this functionality?

        • Re: Adding Custom Properties to multiple files
          Alex Burnett

          Have you taken a look at the #Task tool? The below screen is what I could find regarding batch adding a custom property.

           

            • Re: Adding Custom Properties to multiple files
              Luke Jones

              Hello Alex,

               

              I have tried #Task. Its an incredible tool and has done a lot of really useful things for us, but the macros for adding custom properties seem to overwrite exisiting ones with a blank value. Unless of course Im not using it right? Is there any syntax that I can use that would ignore properties that are already present?

                • Re: Adding Custom Properties to multiple files
                  Alex Burnett

                  I haven't actually used it for that yet so it's hard to say. The only other thing that I can think of is to write something with the API to access the properties of the file and check if the property exists before adding it. I think the Document Manager can be used to change attributes without opening the file in SW but I believe it requires a serial number that you can request through your VAR.

                   

                  Again, I haven't used these tools for that specifically so there may be something I am missing.

                  • Re: Adding Custom Properties to multiple files
                    Deepak Gupta

                    You can use following codes and batch run it via #TASK.

                    Option Explicit
                    Dim swApp As SldWorks.SldWorks
                    Dim swModel As SldWorks.ModelDoc2
                    Dim swCustProp As CustomPropertyManager
                    Dim ValOut As String
                    Dim ResolvedValOut As String
                    Dim wasResolved As Boolean
                    Dim resolved As Variant
                    Dim nErrors As Long
                    Dim nWarnings As Long
                    
                    Sub main()
                    
                    Set swApp = Application.SldWorks
                    Set swModel = swApp.ActiveDoc
                    Set swCustProp = swModel.Extension.CustomPropertyManager("")
                    
                    swCustProp.Get5 "Finished Length", False, ValOut, ResolvedValOut, wasResolved
                    
                    If ValOut = "" Then
                    swCustProp.Add3 "Finished Length", 30, "Some value here", 1
                    End If
                    
                    swModel.Save3 swSaveAsOptions_Silent, nErrors, nWarnings
                        
                    End Sub