11 Replies Latest reply on Mar 21, 2017 8:23 AM by Paul Crotty

    Find Replace Custom Property in all Configs

    Paul Crotty

      Hello All,

       

      I am new to the macro/API world and have been trying my best to understand some of this stuff. This forum has been very helpful in the past but this is my first post.

       

      I found this discussion Macro Find & Replace custom property and modified it to my needs and it does work for the custom properties but not for all of the configuration specific properties. Our configs can be name a range of different things but are usually our part number. But basically we just need to delete the first two characters of the "Number" Custom and Config Specific Properties. 

       

      Any help would be greatly appreciated.

       

      Thank you

       

      Paul

       

      Here is the current code

       

      Option Explicit

      Dim swApp           As SldWorks.SldWorks

      Dim swModel         As SldWorks.ModelDoc2

      Dim swCustPropMgr   As SldWorks.CustomPropertyManager

      Dim Prop1 As String

      Dim Prop2 As String

      Sub main()

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

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

       

      'Replace P1 with the actual property name

      Prop1 = swModel.CustomInfo("")

      Prop1 = Left(Prop1, 2)

       

      Prop2 = swModel.CustomInfo("Number")

      Prop2 = Right(Prop2, 4)

       

      swModel.CustomInfo("Number") = Prop1 & Prop2

       

      End Sub

        • Re: Find Replace Custom Property in all Configs
          Peter Kennedy

          If I understand correct you want to take the current "" custom property and use the first two character and take "Number" custom property and use the last 4 characters?

           

          Or you said you want to just remove the first two lefts from Number property, are they consistently the same character length every time?

           

          If so you could go:

           

          Option Explicit

          Dim swApp           As SldWorks.SldWorks

          Dim swModel         As SldWorks.ModelDoc2

          Dim swCustPropMgr   As SldWorks.CustomPropertyManager

          Dim Prop1 As String

          Dim Prop2 As String

          Sub main()

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

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

           

          'Replace P1 with the actual property name

          Prop1 = swModel.CustomInfo("")

          Prop1 = Left(Prop1, 2)

           

          Prop2 = swModel.CustomInfo("Number")

          Prop2 = Right(Prop2, 4)

           

          'Deletes the Number custom property

          swCustPropMgr.Delete ("Number")

           

          'Adds Number custom property back but combining Prop1 and Prop2

          swModel.AddCustomInfo3 "", "Number", swCustomInfoText, (Prop1 & Prop2)

           

          End Sub

            • Re: Find Replace Custom Property in all Configs
              Paul Crotty

              Hello Peter ,

               

              Sorry I think I might of explained what I am doing wrong.

               

              Our part numbers are always DExxxx (the xxxx being 4 numbers)

               

              When we moved to Solidworks PDM our "Number" Custom property which is also in several Configuration Specific parts and assemblies was somehow changed from "1234" to "DE1234". We need to change them back to "1234" the current macro does delete the DE from the "Number" property but only from the custom tab and not from any configuration specific locations.

               

              I hope I am explaining this better.

                • Re: Find Replace Custom Property in all Configs
                  Raghvendra Bhargava

                  Try this:

                   

                  Option Explicit

                  Dim swApp           As SldWorks.SldWorks

                  Dim swModel         As SldWorks.ModelDoc2

                  Dim swCustPropMgr   As SldWorks.CustomPropertyManager

                  Dim Val As String

                  Dim valout As String

                  Dim vConfName As Variant

                  Dim i As Integer

                  Dim bool As Boolean

                   

                  Sub main()

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                   

                  vConfName = swModel.GetConfigurationNames

                  ReDim Preserve vConfName(UBound(vConfName) + 1)

                   

                  For i = 0 To UBound(vConfName)

                      Set swCustPropMgr = swModel.Extension.CustomPropertyManager(vConfName(i))

                     

                      bool = swCustPropMgr.Get4("Number", False, Val, valout)

                     

                      If valout <> "" And valout = "DE1234" Then

                          'Deletes the Number custom property

                          swCustPropMgr.Delete ("Number")

                         

                          'Adds Number custom property

                          swModel.AddCustomInfo3 vConfName(i), "Number", swCustomInfoText, Right(valout, 4)

                      End If

                  Next i

                  End Sub

                    • Re: Find Replace Custom Property in all Configs
                      Paul Crotty

                      Hello Raghvendra,

                       

                      Thank you very much for the help.

                       

                      This code works very well and does find DE1234 in a part with any number of config specific and removes the DE. The only problem I am having so far is the last 4 digits "1234" can be any combination of 4 digits.

                       

                      Is there anyway to get it to read the property and use only the last 4 digits removing the first two (the DE) regardless of what is written in the property?

                       

                      Again thank you all for the help.

                    • Re: Find Replace Custom Property in all Configs
                      Peter Kennedy

                      Okay then this is all you need and it should work for you.

                       

                      Option Explicit

                      Dim swApp           As SldWorks.SldWorks

                      Dim swModel         As SldWorks.ModelDoc2

                      Dim swCustPropMgr   As SldWorks.CustomPropertyManager

                      Dim Prop1 As String

                      Sub main()

                      Set swApp = Application.SldWorks

                      Set swModel = swApp.ActiveDoc

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

                       

                      'Gets Prop 1 info, grabs the last for characters

                      Prop1 = swModel.CustomInfo("Number")

                      Prop1 = Right(Prop1, 4)

                       

                      'Deletes the Number custom property

                      swCustPropMgr.Delete ("Number")

                       

                      'Adds Number custom property back which is Prop1

                      swModel.AddCustomInfo3 "", "Number", swCustomInfoText, Prop1

                       

                      End Sub

                        • Re: Find Replace Custom Property in all Configs
                          Paul Crotty

                          Hello Peter,

                           

                          Thank you for the help, this code is a cleaned up version of what I originally posted and it is helping me understand VBA and how it works much better so thank you.

                           

                          Is there anyway to get the code you just posted to run on all configurations?

                           

                          In Raghvendra Bhargava's example he used this code to get it to read/write to all configurations

                           

                          vConfName = swModel.GetConfigurationNames

                          ReDim Preserve vConfName(UBound(vConfName) + 1)

                           

                          For i = 0 To UBound(vConfName)

                              Set swCustPropMgr = swModel.Extension.CustomPropertyManager(vConfName(i))

                           

                          bool = swCustPropMgr.Get4("Number", False, Val, valout)

                            

                              If valout <> "" And valout = "DE1234" Then

                                  'Deletes the Number custom property

                                  swCustPropMgr.Delete ("Number")

                                

                                  'Adds Number custom property

                                  swModel.AddCustomInfo3 vConfName(i), "Number", swCustomInfoText, Right(valout, 4)

                              End If

                          Next i

                           

                          But in this case I read it as it looks for property "Number" and if "Number" equals "DE1234" then run this code where as ideally it could read if there is a property called "Number" than run the code you just posted thus writing to all configurations.

                           

                          Again thank you all for the help and hanging in there with me on my learning.

                           

                          Paul

                            • Re: Find Replace Custom Property in all Configs
                              Peter Kennedy

                              Wow sorry I completely missed that, these early morning will get ya some times: as Deepak Gupta mentioned below this show work:

                               

                              Option Explicit

                              Dim swApp           As SldWorks.SldWorks

                              Dim swModel         As SldWorks.ModelDoc2

                              Dim swCustPropMgr   As SldWorks.CustomPropertyManager

                              Dim Val As String

                              Dim valout As String

                              Dim vConfName As Variant

                              Dim i As Integer

                              Dim bool As Boolean

                               

                              Sub main()

                              Set swApp = Application.SldWorks

                              Set swModel = swApp.ActiveDoc

                               

                              vConfName = swModel.GetConfigurationNames

                              ReDim Preserve vConfName(UBound(vConfName) + 1)

                               

                              'For loop to run through all configurations

                              For i = 0 To UBound(vConfName)

                                   'Setting properties manager

                                  Set swCustPropMgr = swModel.Extension.CustomPropertyManager(vConfName(i))

                                   'This will grab the data from the property table for number and store in valout

                                   bool = swCustPropMgr.Get4("Number", False, Val, valout)

                                   'As long as valout doesn't equal nothing it will run

                                  If valout <> "" Then

                                      'Deletes the Number custom property

                                      swCustPropMgr.Delete ("Number")

                                

                                      'Adds Number custom property

                                      swModel.AddCustomInfo3 vConfName(i), "Number", swCustomInfoText, Right(valout, 4)

                                  End If

                              Next i

                               

                              End Sub

                                • Re: Find Replace Custom Property in all Configs
                                  Paul Crotty

                                  Peter and Deepak,

                                   

                                  THANK YOU!

                                   

                                  This code works perfect on everything that I have tried so far but drawings but I currently not sure if it even needs to work on the drawings.

                                   

                                  Also the cleaned up code Peter posted does work on the drawings if needed so there is that. Also I thank you for the little descriptions you added on what does what. Very helpful for people like me learning this.

                                   

                                  Any suggestions for best practice for running this across a folder at a time?

                                    • Re: Find Replace Custom Property in all Configs
                                      Deepak Gupta

                                      Paul, try these combined codes. I've also simplified/removed few lines. And if you need to batch run it, then you can use it with #TASK or use/modify the codes form this macro https://forum.solidworks.com/message/718780#comment-718780 or this one https://forum.solidworks.com/message/711826#comment-711826

                                      Option Explicit
                                      Dim swApp           As SldWorks.SldWorks
                                      Dim swModel         As SldWorks.ModelDoc2
                                      Dim swCustPropMgr   As SldWorks.CustomPropertyManager
                                      Dim Val             As String
                                      Dim valout          As String
                                      Dim vConfName       As Variant
                                      Dim i               As Integer
                                      
                                      Sub main()
                                      Set swApp = Application.SldWorks
                                      Set swModel = swApp.ActiveDoc
                                      
                                      'For Drawings
                                      If swModel.GetType = 3 Then
                                      Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")
                                           swCustPropMgr.Get4 "Number", False, Val, valout
                                             If valout <> "" Then
                                                 swCustPropMgr.Add3 "Number", swCustomInfoText, Right(valout, 4), 1
                                          End If
                                      Else
                                      
                                      'For Part and Assembly
                                      vConfName = swModel.GetConfigurationNames
                                      For i = 0 To UBound(vConfName)
                                          Set swCustPropMgr = swModel.Extension.CustomPropertyManager(vConfName(i))
                                           swCustPropMgr.Get4 "Number", False, Val, valout
                                             If valout <> "" Then
                                                 swCustPropMgr.Add3 "Number", swCustomInfoText, Right(valout, 4), 1
                                          End If
                                      Next i
                                      End If
                                      End Sub
                                      
                                • Re: Find Replace Custom Property in all Configs
                                  Deepak Gupta

                                  Replace this line

                                   

                                  If valout <> "" And valout = "DE1234" Then

                                   

                                  with

                                   

                                   

                                  If valout <> "" Then