27 Replies Latest reply on Jun 16, 2016 11:30 AM by MATT AUSTIN

    Custom Properties - Part of a file name

    Sebastian Acosta

      The file naming convention that we use, has the part/drawing number first, followed by a description.

       

      For example:

       

      123456-001 - FRAME WELDMENT

      123456-002 - TANK

      123456-003 - SHAFT

      123456-102 - AXLE

       

      Custom properties can be linked to the file name, by typing something like:

      Custom Property                   Value / Text Expression          Evaluated Value

      Part No                                 $PRP:"SW-File Name"          123456-001 - FRAME WELDMENT

       

      What I would like, is an expression that selects only the first 10 characters or so, such that:

      Custom Property                   Value / Text Expression          Evaluated Value

      Part No                                 ?                                          123456-001

      Description                            ?                                          FRAME WELDMENT

       

      This would be useful because, during a Pack&Go we commonly rename the number using the "select/replace". But then the custom properties need tidying up manualy or with a macro.

       

      Any suggestions?

        • Re: Custom Properties - Part of a file name
          Deepak Gupta

          This can be done by using equations which you can embed into the file templates with the required properties. I'll try to set up an example.

          • Re: Custom Properties - Part of a file name
            Sebastian Acosta

            Deepak,

             

            Thanks for the response.

             

            In the following discussion, they used equations to run some VBA code, but I haven't (yet) been able to figure out how it is done.

            https://forum.solidworks.com/message/312634

              • Re: Custom Properties - Part of a file name
                Deepak Gupta

                Somehow the functionality is not working in SW2012/13 for me or at least I'm not  able to use it.

                 

                But In case you need a macro to accomplish what you looking for, I can make one quickly.

                • Re: Custom Properties - Part of a file name
                  Deepak Gupta

                  Here are the quick codes you can use in macro. Start a new macro (tools > macro > new) and copy paste these codes. Then run this macro ((tools > macro > run) and check file properties.

                   

                  Dim swApp As SldWorks.SldWorks

                  Dim swModel As ModelDoc2

                  Sub main()

                   

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                   

                  swModel.AddCustomInfo2 "Part No", swCustomInfoText, Left(swModel.GetTitle, Len(swModel.GetTitle) - 17)

                  swModel.AddCustomInfo2 "Description", swCustomInfoText, Right(swModel.GetTitle, Len(swModel.GetTitle) - 13)

                   

                  End Sub

                    • Re: Custom Properties - Part of a file name
                      Uwe Brunzel

                      Was looking for a solution for the exact same problem (splitting file name for custom properties) and found this thread. I hope somebody will read this an may have a solution.

                       

                      I have used the API code you provided, thanks very much. It is all working as I would like but it won't override the custom properties once thay have been filled in with the macro. I.E. I want to uprev the drawing and the filename changes.

                       

                      It all works fine if I delete the "old" custom property before running the macro. But I would like it to update on it's own as soon as I save a file. Do you think this is possible?

                       

                      I am very new to API, so any help is much appreciated.

                        • Re: Custom Properties - Part of a file name
                          Matt Martens

                          Uwe,

                           

                          Change these two lines from

                           

                          swModel.AddCustomInfo2 "Part No", swCustomInfoText, Left(swModel.GetTitle, Len(swModel.GetTitle) - 17)

                          swModel.AddCustomInfo2 "Description", swCustomInfoText, Right(swModel.GetTitle, Len(swModel.GetTitle) - 13)

                           

                          to

                           

                          swModel.CustomInfo2 "Part No", swCustomInfoText = Left(swModel.GetTitle, Len(swModel.GetTitle) - 17)

                          swModel.CustomInfo2 "Description", swCustomInfoText = Right(swModel.GetTitle, Len(swModel.GetTitle) - 13)

                           

                          Even though this method is superseded by another it should still work for updating a custom property.

                           

                          If this doesn't work delete the existing custom property by this method

                           

                          swModel.DeleteCustomInfo "Part No"

                          swModel.DeleterCustomInfo "Description"

                           

                          prior to using AddCustomInfo2 to add the property.

                           

                          Matt

                            • Re: Custom Properties - Part of a file name
                              Uwe Brunzel

                              Matt,

                               

                              thanks for your swift reply! I had a little play with it and I am almost there thanks to your help.

                               

                              Just need to find some more spare time and finish it all properly.

                               

                              One more question if you don't mind, I have deleted the string Len(swModel.GetTitle) an it works fine without it.

                               

                              That's what I came up with so fat: (with the below custom properties NOT present in the part file)

                               

                              File name is ABC-001234-A Cabinet Assembly

                               

                              -----------

                              Dim swApp As SldWorks.SldWorks

                               

                                  Dim swModel As ModelDoc2

                               

                                  Sub main()

                               

                                   

                                  Set swApp = Application.SldWorks

                               

                                  Set swModel = swApp.ActiveDoc

                               

                                   

                                  swModel.AddCustomInfo2 "PartNo", swCustomInfoText, Left(swModel.GetTitle, 10)

                                 

                                  swModel.AddCustomInfo2 "Revision", swCustomInfoText, Mid(swModel.GetTitle, 12, 1)

                               

                                  swModel.AddCustomInfo2 "Description", swCustomInfoText, Mid(swModel.GetTitle, 14, 50)

                                   

                               

                                  End Sub

                              -----------

                               

                              The "swModel.CustomInfo2" version didn't work at all.

                                • Re: Custom Properties - Part of a file name
                                  Deepak Gupta

                                  Uwe, try these codes. The macro will add the properties and their values if the custom properties doesn't exists. If they do exists then macro will update them.

                                   

                                  Dim swApp As SldWorks.SldWorks

                                  Dim swModel As ModelDoc2

                                   

                                  Sub main()

                                   

                                      Set swApp = Application.SldWorks

                                      Set swModel = swApp.ActiveDoc

                                   

                                      'Check and Add/Update PartNo Property

                                      If swModel.CustomInfo("PartNo") = "" Then

                                        swModel.AddCustomInfo2 "PartNo", swCustomInfoText, Left(swModel.GetTitle, 10)

                                      Else

                                          swModel.CustomInfo("PartNo") = Left(swModel.GetTitle, 10)

                                      End If

                                   

                                      'Check and Add/Update Revision Property

                                      If swModel.CustomInfo("Revision") = "" Then

                                        swModel.AddCustomInfo2 "Revision", swCustomInfoText, Mid(swModel.GetTitle, 12, 1)

                                      Else

                                          swModel.CustomInfo("Revision") = Mid(swModel.GetTitle, 12, 1)

                                      End If

                                   

                                      'Check and Add/Update Description Property

                                      If swModel.CustomInfo("Description") = "" Then

                                        swModel.AddCustomInfo2 "Description", swCustomInfoText, Mid(swModel.GetTitle, 14, 50)

                                      Else

                                          swModel.CustomInfo("Description") = Mid(swModel.GetTitle, 14, 50)

                                      End If

                                   

                                  End Sub

                                   

                                  P.S. swModel.CustomInfo2 is/was used for configuration properties.

                              • Re: Custom Properties - Part of a file name
                                Deepak Gupta

                                But I would like it to update on it's own as soon as I save a file. Do you think this is possible?

                                Do you want to macro to save and update the file properties on it own when you do a save or save as for any open file?

                                 

                                If yes then check and modify this macro here: https://forum.solidworks.com/message/387860#387860

                                 

                                This macro create a PDF (overwriting the old one) whenever an active file is saved. You'll have to run the macro after starting the SolidWorks so that it keeps on running in the background.

                                 

                                In case you want the macro run automatically once SolidWorks session has been started, then here is the way: https://forum.solidworks.com/message/387910#387910

                          • Re: Custom Properties - Part of a file name
                            Naga Pavan Dakineedi

                            Hi every one,

                            i dont know about this macro at all, but we can do that task by simple changing sheet format, and link the part number with the property in the model in view specified in the sheet formet, that means get that values from configuration specific.

                              • Re: Custom Properties - Part of a file name
                                Deepak Gupta

                                But again you'll have to somehow split the file name under part properties using macro, VBA script or manually type it.

                                  • Re: Custom Properties - Part of a file name
                                    Uwe Brunzel

                                    Had bit of time during lunch and can report the macro is working well except when the custom property is already present but empty.

                                    As soon as anything is there, it works fine and overwrites the contens.

                                     

                                    I made a few test file and assemblies with drawings and everything works as I wanted it. Great!!!

                                      • Re: Custom Properties - Part of a file name
                                        Deepak Gupta

                                        Uwe Brunzel wrote:

                                         

                                        Had bit of time during lunch and can report the macro is working well except when the custom property is already present but empty.

                                         

                                        Then for keeping it simple, use Matt's method:

                                         

                                        Dim swApp As SldWorks.SldWorks

                                        Dim swModel As ModelDoc2

                                         

                                        Sub main()

                                         

                                            Set swApp = Application.SldWorks

                                            Set swModel = swApp.ActiveDoc

                                         

                                             swModel.DeleteCustomInfo ("PartNo")

                                             swModel.AddCustomInfo2 "PartNo", swCustomInfoText, Left(swModel.GetTitle, 10)

                                         

                                             swModel.DeleteCustomInfo ("Revision")

                                             swModel.AddCustomInfo2 "Revision", swCustomInfoText, Mid(swModel.GetTitle, 12, 1)

                                         

                                             swModel.DeleteCustomInfo ("Description")

                                             swModel.AddCustomInfo2 "Description", swCustomInfoText, Mid(swModel.GetTitle, 14, 50)

                                         

                                        End Sub

                                          • Re: Custom Properties - Part of a file name
                                            Dirk Jan Schep

                                            Hi Deepak (and others!),

                                             

                                            Thanks for showing how to fix Sebastians problem, I have the exact same problem and now have some way of fixing it. Although with the following Macro I also get the .sdlprt at the end of my description. Do you happen to know a command to subtract this from the description after grabbing it from the filename? I have exactly zero experience with Macro's and programming, but with some common sense I was able to use your examples and edit them to my needs:

                                             

                                            Dim swApp As SldWorks.SldWorks

                                            Dim swModel As ModelDoc2

                                             

                                            Sub main()

                                             

                                                Set swApp = Application.SldWorks

                                                Set swModel = swApp.ActiveDoc

                                             

                                                'Check and Add/Update PartNo Property

                                                If swModel.CustomInfo("Part No") = "" Then

                                                  swModel.AddCustomInfo2 "PartNo", swCustomInfoText, Left(swModel.GetTitle, 9)

                                                Else

                                                    swModel.CustomInfo("Part No") = Left(swModel.GetTitle, 9)

                                                End If

                                             

                                                'Check and Add/Update Description Property

                                                If swModel.CustomInfo("Description") = "" Then

                                                  swModel.AddCustomInfo2 "Description", swCustomInfoText, Mid(swModel.GetTitle, 13, 50)

                                                Else

                                                    swModel.CustomInfo("Description") = Mid(swModel.GetTitle, 13, 50)

                                                End If

                                             

                                            End Sub

                                             

                                            All I still need to add is to remove the .sdlprt from the description property. Thanks in advance for your advice!

                                            • Re: Custom Properties - Part of a file name
                                              Adam Richendollar

                                              I am using this code, just slightly modified.  But I am having a hard time trying to extract a string of characters between two delimiters. Just know that I know next to nothing regarding VBA.

                                               

                                              We save our part numbers in this format: xxxxxxxx_REV XXX_MARK UP_IN.

                                              I need to extract the information between the first and second delimiter "_" for the revision information, while also adding it to the custom property manager.  Everything seems to work but I cannot figure out how to extract this information.  My company is using Solidworks 2013.

                                               

                                              Thanks.

                                              ---------------------------------------------------

                                              Dim swApp As SldWorks.SldWorks

                                              Dim swModel As ModelDoc2

                                               

                                               

                                               

                                              Sub main()

                                               

                                                  Set swApp = Application.SldWorks

                                                  Set swModel = swApp.ActiveDoc

                                                

                                                  swModel.DeleteCustomInfo ("PartNo")

                                                  swModel.AddCustomInfo2 "PartNo", swCustomInfoText, Replace(Replace(swModel.GetTitle, Mid(swModel.GetTitle, InStr(1, swModel.GetTitle, "_")), Empty), ".sldprt", Empty, , , vbTextCompare)

                                               

                                               

                                                   swModel.DeleteCustomInfo ("Revision")

                                                  swModel.AddCustomInfo2 "Revision", swCustomInfoText, Replace(Replace(swModel.GetTitle, Right(swModel.GetTitle, InStr(, swModel.GetTitle, "REV")), Empty), ".sldprt", Empty, , , vbTextCompare)

                                               

                                               

                                               

                                               

                                                  swModel.DeleteCustomInfo ("Description")

                                                  swModel.AddCustomInfo2 "Description", swCustomInfoText, Replace(Replace(swModel.GetTitle, Left(swModel.GetTitle, InStr(1, swModel.GetTitle, "_MARK")), Empty), ".sldprt", Empty, , , vbTextCompare)

                                               

                                               

                                              End Sub

                                                • Re: Custom Properties - Part of a file name
                                                  Deepak Gupta

                                                  I'm not sure how to do in one liner but try this

                                                      Dim value As String

                                                      value = Mid(swModel.GetTitle, InStrRev(swModel.GetTitle, "REV"))

                                                      value = Left(value, InStr(value, "_") - 1)

                                                    • Re: Custom Properties - Part of a file name
                                                      Adam Richendollar

                                                      I used your code as follows:

                                                       

                                                      Dim swApp As SldWorks.SldWorks

                                                      Dim swModel As ModelDoc2

                                                      Dim Value As String

                                                       

                                                      Sub main()

                                                       

                                                          Set swApp = Application.SldWorks

                                                          Set swModel = swApp.ActiveDoc

                                                          Value = Mid(swModel.GetTitle, InStrRev(swModel.GetTitle, "REV"))

                                                         

                                                           swModel.DeleteCustomInfo ("PartNo")

                                                           swModel.AddCustomInfo2 "PartNo", swCustomInfoText, Replace(Replace(swModel.GetTitle, Mid(swModel.GetTitle, InStr(2, swModel.GetTitle, "_")), Empty), ".sldprt", Empty, , , vbTextCompare)

                                                       

                                                       

                                                           swModel.DeleteCustomInfo ("Revision")

                                                           swModel.AddCustomInfo2 "Revision", swCustomInfoText, Left(Value, InStr(Value, "_") - 1)

                                                          

                                                           swModel.DeleteCustomInfo ("Description")

                                                           swModel.AddCustomInfo2 "Description", swCustomInfoText, Replace(Replace(swModel.GetTitle, Left(swModel.GetTitle, InStr(1, swModel.GetTitle, "_MARK")), Empty), ".sldprt", Empty, , , vbTextCompare)

                                                       

                                                       

                                                      End Sub

                                                      ----------------------------------------------------------------

                                                      This ended up working!!! Thank you so much!

                                                      • Re: Custom Properties - Part of a file name
                                                        Robbin Mennings

                                                        Hi Deepak,

                                                         

                                                        I have a question which is a bit simmilar as above. My programming skill are not that good.  I want to extract some more information about the file name and write it into the custom properties, I hope this is possible.

                                                         

                                                        My file name  for purchased parts will be:

                                                         

                                                        Part no - Description - Order no - Supplier

                                                         

                                                        Example:

                                                        16AB00-AB - Wheel - AXB38393929 - Misumi

                                                         

                                                        I have tried some code by myself but it doen`t work, this is because the length of the words will vary between the "-" signs.

                                                         

                                                        -------------------------------------------------------

                                                         

                                                        Dim swApp As SldWorks.SldWorks

                                                         

                                                            Dim swModel As ModelDoc2

                                                         

                                                            Sub main()

                                                            

                                                                Set swApp = Application.SldWorks

                                                         

                                                                Set swModel = swApp.ActiveDoc

                                                            

                                                                'Check and Add/Update PartNo Property

                                                         

                                                                If swModel.CustomInfo("PartNo") = "" Then

                                                         

                                                                  swModel.AddCustomInfo2 "PartNo", swCustomInfoText, Left(swModel.GetTitle, 9)

                                                         

                                                                Else

                                                         

                                                                    swModel.CustomInfo("PartNo") = Left(swModel.GetTitle, 9)

                                                         

                                                                End If

                                                         

                                                                           'Check and Add/Update Description Property

                                                         

                                                                If swModel.CustomInfo("Description") = "" Then

                                                         

                                                                  swModel.AddCustomInfo2 "Description", swCustomInfoText, Mid(swModel.GetTitle, 10, 50)

                                                         

                                                                Else

                                                         

                                                                    swModel.CustomInfo("Description") = Mid(swModel.GetTitle, 10, 50)

                                                         

                                                                End If

                                                         

                                                                        'Check and Add/Update Supplier Property

                                                         

                                                                If swModel.CustomInfo("Supplier") = "" Then

                                                         

                                                                  swModel.AddCustomInfo2 "Supplier", swCustomInfoText, Right(swModel.GetTitle, 10)

                                                         

                                                                Else

                                                         

                                                                    swModel.CustomInfo("Supplier") = Right(swModel.GetTitle, 10)

                                                         

                                                                End If

                                                            

                                                         

                                                            End Sub

                                                         

                                                        ----------------------------------------------------------------------------------

                                                         

                                                        Can you help me with this?

                                                         

                                                        Thanks in advance,

                                                         

                                                        Robbin Mennings

                                            • Re: Custom Properties - Part of a file name
                                              MATT AUSTIN

                                              Sebastian, I have a macro that pulls parts of our FileNames and extracts portions and places them in the File Properties.  I did not include the entire code because it is very long and does a lot more than what you are after. Using my code, you may have to recombine two strings to get your partnumber back together. Deepak may be able to help condense the code as well. I only know how to reverse engineer/combine coding to get what I need so some of the way I did things may be the long way to do it...

                                              Our Naming Convention is PN_Rev Letter_Title (with the Title broken into sections by more _s). eg. 315052_A_SPACER__PULLEY_BLK. We want to separate it out and add to Custom Properties as 315052, A, SPACER - PULLEY-BLK.

                                               

                                                  Option Explicit

                                               

                                                  Dim swApp           As SldWorks.SldWorks

                                               

                                                  Dim swModel         As SldWorks.ModelDoc2

                                               

                                                  Dim swConfigMgr     As SldWorks.ConfigurationManager

                                               

                                                  Dim swConfig        As SldWorks.Configuration

                                                

                                                 

                                              Dim boolstatus As Boolean

                                              Dim FILENAME        As String

                                              Dim TitleandRev As String

                                              Dim PartNum     As String

                                              Dim Title   As String

                                              Dim Revision1       As String

                                              Dim Retval      As String

                                              Public Signature As String

                                              Public DosTruename As String

                                              Dim i As Integer

                                              Dim Date1 As String

                                               

                                               

                                               

                                               

                                               

                                                  Sub main()

                                               

                                                  Set swApp = Application.SldWorks

                                               

                                                  Set swModel = swApp.ActiveDoc

                                               

                                                  Set swConfigMgr = swModel.ConfigurationManager

                                               

                                                  Set swConfig = swConfigMgr.ActiveConfiguration

                                               

                                                  Date1 = Format(Date, "M/D/YY")

                                               

                                                  FILENAME = swModel.GetTitle

                                               

                                                  FILENAME = Replace(Replace(FILENAME, ".sldprt", "", , , vbTextCompare), ".sldasm", "", , , vbTextCompare)

                                                

                                              'get needed info from model file name

                                              Dim PNlen As String

                                              Dim length As String

                                               

                                              FILENAME = swModel.GetTitle

                                               

                                              If InStr(1, FILENAME, ".SLDPRT") > 0 Then

                                                 FILENAME = Replace(FILENAME, ".SLDPRT", "")

                                                 ElseIf InStr(1, FILENAME, ".sldprt") > 0 Then

                                                  FILENAME = Replace(FILENAME, ".sldprt", "")

                                               

                                              End If

                                               

                                              If InStr(1, FILENAME, "_") > 0 Then

                                               

                                              PartNum = VBA.Left(FILENAME, InStr(FILENAME, "_") - 1)

                                               

                                              PNlen = Len(PartNum) + 1

                                               

                                              length = Len(PartNum) + 3

                                               

                                              If Len(FILENAME) > length Then

                                              Title = VBA.Right(FILENAME, Len(FILENAME) - length)

                                               

                                              End If

                                              If Len(FILENAME) > PNlen Then

                                              Revision1 = VBA.Right(FILENAME, Len(FILENAME) - PNlen)

                                               

                                              Revision1 = VBA.Left(Revision1, 1)

                                               

                                              Else

                                              Title = FILENAME

                                              Revision1 = 1

                                              End If