7 Replies Latest reply on Jul 2, 2016 8:29 AM by Solid Air

    API to get specific digit from file name.

    jason kooken

      I have a part numbering system that has the drawing rev built into the filename for example: 11111234A-PM.slddrw

      Previously the Macro used to save this drawing would get the revision from custom properties.

      This field is no longer updated and there is now a need to retrieve the revision from the filename instead.

       

      Below is a clip of the current macro, in the "get revision" section is what was used to get the rev, I want to get the revision from the 9th digit of the file name as "rev"

      any help would be greatly appreciated.

       

      Dim longstatus As Long, longwarnings As Long

      Dim swModel As SldWorks.ModelDoc2

      Dim swApp As SldWorks.SldWorks

       

       

      Sub main()

          Set swApp = Application.SldWorks

          Set Part = swApp.ActiveDoc

          Set swModel = swApp.ActiveDoc

          Set SelMgr = Part.SelectionManager

          Dim proj As String

          Dim dwg As String

          Dim filename As String

          Dim saveLoc As String

          Dim lastTwoChar As String

          Dim rev As String

          Dim trueRev As String

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

       

       

          filename = swModel.GetPathName

          If filename = "" Then

              MsgBox "Please save the file first and try again", vbCritical

              End

          End If

         

       

          'Get Revision

          retval = swCusPropMgr.Get3("Revision", False, rev, trueRev)

          If rev = "-" Then

              rev = "-"

          End If

        • Re: API to get specific digit from file name.
          Deepak Gupta

          Use these codes for revision

          filename = Mid(filename, InStrRev(filename, "\") + 1)

          filename = Left(filename, InStrRev(filename, ".") - 1)

          rev = Left(Mid(filename, 9), 1)

          • Re: API to get specific digit from file name.
            Solid Air

            Another solution

             

            rev = Mid(swModel.GetTitle, 9, 1)

             

            If you would rather use the previous code supplied, it can be simplified to

             

            rev = Mid(filename, InStrRev(filename, "\") + 9 , 1)

              • Re: API to get specific digit from file name.
                Aj Bosma

                Deepak Gupta

                 

                In what way would i have to modify the code above to use it to find within the example filepaths the numeric value xx.xxx (numbers are subject to change, but are always suplied as seen in example below)

                 

                x:\12.001\Section\110

                x:\something\15.001\Section\110

                x:\something\something\55.001-ProjectName\Section\110

                  • Re: API to get specific digit from file name.
                    Deepak Gupta

                    You might have to split them into various string and then check each string for numeric.

                    • Re: API to get specific digit from file name.
                      Solid Air

                      If you can guarantee that the file paths will always be as shown in your post (ie only the numbers will contain a period {decimal point})? and that only the number will have a dash (minus sign) after it?  If yes, then I might try something like (there are probably a lot of programmers out there that will not like my variable names):

                       

                      Option Explicit

                      Dim FilePath As String
                      Dim DecimalPoint As Long
                      Dim FirstBackslash As Long
                      Dim Dash As Long
                      Dim SecondBackslash As Long
                      Dim NumericValue As Double

                      Sub main()

                          'initilize variables
                          DecimalPoint = 0
                          FirstBackslash = 0
                          Dash = 0
                          SecondBackslash = 0
                          NumericValue = 0
                         
                          FilePath = {your code to get file path}
                         
                          'find decimal point
                          DecimalPoint = InStr(FilePath, ".")
                         
                          'if point found find backslash preceding it
                          If DecimalPoint Then
                         
                              'find backslash
                              FirstBackslash = InStrRev(FilePath, "\", DecimalPoint)
                             
                              'look for dash after decimal point
                              Dash = InStr(DecimalPoint, FilePath, "-")
                             
                              'check if dash not found, look for following backslash
                              If Dash = 0 Then
                             
                                  SecondBackslash = InStr(DecimalPoint, FilePath, "\")
                                 
                              End If
                             
                              'check if dash or secondbackslash found
                              If Dash <> 0 Or SecondBackslash <> 0 Then
                             
                                  'set SecondBackslash equal to end of number (one of these values will be zero)
                                  SecondBackslash = Dash + SecondBackslash
                             
                                  'get value
                                  NumericValue = CDbl(Mid$(FilePath, FirstBackslash + 1, SecondBackslash - FirstBackslash - 1))
                                 
                              End If
                             
                          End If
                         
                          'display value
                          If NumericValue Then
                         
                              Debug.Print NumericValue
                             
                          Else
                         
                              Debug.Print "No value found"
                             
                          End If
                                 
                      End Sub