14 Replies Latest reply on Nov 4, 2014 7:57 AM by Mike Helsinger

    Extract my part number from my file name (macro?)

    Mike Helsinger

      I have my drawing template set up to receive title block entries from my part file's custom properties.  I'd like to go a step farther and extract my part number and revision from the file name without explicitly entering the info in custom properties / title blocks.  I'm seeing similar queries in other threads but I don't know enough about macros to successfully patch one of my own together for my scenario.  Here's some specifics of my file structures:

       

      • part file & drawing file are always named identical to each other
      • Part number can be any length of alphanumeric characters and often includes a "-" (dash)
      • Revision may be one, two, or more characters, may include be a "-" (dash) or " " (space)
      • file name format is "(part number) Rev (revision)
      • example
        • part number is 101010-201
        • revision is C SUB B
        • file name is 101010-201 Rev C SUB B.sldprt

       

      Is it possible to get there from here?

        • Re: Extract my part number from my file name (macro?)
          Paul Vandenberk

          This is possible. The macro you make can simply add or modify the revision and part number properties of your part in order for them to auto populate to your drawing. You can get the file name and then use standard vba to sort through the title and pick the information you want. It should be easy to determine what is what due to the spaces existing between each of the categories of information.

          • Re: Extract my part number from my file name (macro?)
            Jamil Snead

            See if this works for you. It automatically converts the rev text to all caps, I'm not sure if you want that or not.

            • Re: Extract my part number from my file name (macro?)
              Mike Helsinger

              Got it - mission accomplished.  And for those of you that find this thread useful, see Deepak's macro.  It's enough of a start that I was able to successfully adapt to my needs.

               

              https://forum.solidworks.com/message/434651?sr=search&searchId=4fed8b4a-6668-4e7c-ae74-d1b2ce4f61bd&searchIndex=0#434651

                • Re: Extract my part number from my file name (macro?)
                  Deepak Gupta

                  Mike glad you find that macro helpful. I would advise you to move this thread to API section since it is related to macros. Please use the move discussion option on right side.

                    • Re: Extract my part number from my file name (macro?)
                      Mike Helsinger

                      I'm having issues with my code, posted earlier today.

                        • Re: Extract my part number from my file name (macro?)
                          Deepak Gupta

                          Mike what is REV in your codes and from where it gets the value.

                           

                          I've fixed some issue in the macro (highlighted in bold)

                           

                          Dim swApp As SldWorks.SldWorks

                          Dim swModel As SldWorks.ModelDoc2

                          Dim swCustPropMgr As SldWorks.CustomPropertyManager

                          Dim vConfs As Variant

                          Dim i As Integer

                          Dim retval As Variant

                          Dim FileName As String

                          Dim FileNameSize As Integer

                          Dim PartNumber As String

                          Dim Revision As String

                          Dim Description As String

                          Dim Rev As Integer

                          Dim Material As String

                          Dim Thickness As String

                          Dim KFactor As String

                          Dim valout As String

                          '***********************************    ABSTRACT   ***********************************

                          'This macro was started by extracting code samples from the SolidWorks Forum API section

                          '   Writing credits to Deepak Gupta & Artem Taturevich

                          '

                          'This macro is intended to restructure a part's custom properties.

                          'First it stores the existing descrtiption value (if any).

                          'Then delete all existing custom properties

                          'Then define and populate new custom properties

                          'Part file nomenclature follows the following file naming convention:

                          '   (part number) Rev (part revision).sldprt

                          'This macro does not yet work properly, it's buggy and doesn't do anything.

                           

                          Sub main()

                           

                          Set swApp = Application.SldWorks

                          Set swModel = swApp.ActiveDoc

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

                           

                          'Read current custom properties and extract Description if there is one, else prompt for new Description

                           

                          retval = swCustProp.Get4("Description", False, Description, valout)

                          If Len(Description) < 2 Then

                              Description = InputBox("Enter Description", "What is this thing?")

                              End If

                             

                          'Delete current custom properties, call subroutine

                              ClearCustPrps ("")

                              vConfs = swModel.GetConfigurationNames

                             

                              For i = 0 To UBound(vConfs)

                                  ClearCustPrps (vConfs(i))

                             

                              Next

                            

                          'Define and insert new custom properties

                           

                              'Import File Name & partition

                                  FileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)

                                  FileNameSize = Len(FileName)

                                  Rev = InStr(FileName, " Rev")

                                  

                              'Define Part Number

                                  PartNumber = Left(FileName, Rev)

                                  retval = Part.AddCustomInfo3("", "Part Number", 30, PartNumber)

                           

                              'Define Revision

                                  Revision = Right(FileName, FileNameSize - Rev - 4)

                                  retval = Part.AddCustomInfo3("", "Revision", 30, Revision)

                           

                              retval = Part.AddCustomInfo3("", "Description", 30, Description)

                           

                              Material = Chr(34) & "SW-Material@Part1.SLDPRT" & Chr(34)

                              retval = Part.AddCustomInfo3("", "Material", 30, Material)

                           

                              Thickness = Chr(34) & "Thickness@Part1.SLDPRT" & Chr(34)

                              retval = Part.AddCustomInfo3("", "Thickness", 30, Thickness)

                           

                              KFactor = Chr(34) & "D2@Sheet-Metal2" & Chr(34)

                              retval = Part.AddCustomInfo3("", "K Factor", 30, KFactor)

                           

                          End Sub

                           

                          'Subroutine called to delete existing custom properties

                           

                          Sub ClearCustPrps(conf As String)

                              Dim j As Integer

                             

                              Set swCustPropMgr = swModel.Extension.CustomPropertyManager(conf)

                                 

                              If Not swCustPropMgr Is Nothing Then

                                  swCustPropMgr.GetAll vPropNames, Empty, Empty

                                     

                                  If Not IsEmpty(vPropNames) Then

                                      For j = 0 To UBound(vPropNames)

                                          swCustPropMgr.Delete vPropNames(j)

                                      Next

                                  End If

                                     

                              End If

                                 

                          End Sub

                            • Re: Extract my part number from my file name (macro?)
                              Mike Helsinger

                              Deepak, thank you for taking your time to look at my code.  I applied your edits but I am still getting a Run-time error '91': Object variable or With block variable not set on your line:

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

                              I'll continue to research this occurrence but I'm still happy to receive assistance.

                               

                              The REV variable is a place counter.  My file name structure is (part number) Rev (revision) where the part number can be any number of characters as determined by our customers.  My goal here is to partition the file name into it's two pieces and use them to define the custom properties, so the REV variable locates where in the file name string the text " Rev" appears, space included.  I suppose I could give the variable a more descriptive variable name.

                        • Re: Extract my part number from my file name (macro?)
                          Mike Helsinger

                          Darn, I lost it.  Some time late last week or so my macro disappeared, no idea why.  So I thought I'd take the opportunity to practice what I've learned.  I'm now failing to rewrite the macro that worked great before.  I believe my error is somewhere between how or what I'm declaring / or how to use the swCustProp tools - I'm starting to drown in lack of understanding of the mechanics of what I'm doing and I'm just stuck.

                           

                          I'm running SW Premium 2014 x64 SP 4.0 on Windows 7 Professional SP 1

                          Here's my code, who can help me?

                           

                          Dim swApp As SldWorks.SldWorks
                          Dim swModel As SldWorks.ModelDoc2
                          Dim swCustPropMgr As SldWorks.CustomPropertyManager

                          Dim vConfs As Variant
                          Dim i As Integer

                          Dim retval As Variant
                          Dim FileName As String
                          Dim FileNameSize As Integer
                          Dim PartNumber As String
                          Dim Revision As String
                          Dim Description As String

                          Dim Rev As Integer
                          Dim Material As String
                          Dim Thickness As String
                          Dim KFactor As String

                          '***********************************    ABSTRACT   ***********************************
                          'This macro was started by extracting code samples from the SolidWorks Forum API section
                          '   Writing credits to Deepak Gupta & Artem Taturevich
                          '
                          'This macro is intended to restructure a part's custom properties.
                          'First it stores the existing descrtiption value (if any).
                          'Then delete all existing custom properties
                          'Then define and populate new custom properties
                          'Part file nomenclature follows the following file naming convention:
                          '   (part number) Rev (part revision).sldprt
                          'This macro does not yet work properly, it's buggy and doesn't do anything.

                           

                          Sub main()

                           

                          Set swApp = Application.SldWorks
                          Set swModel = swApp.ActiveDoc
                          swCustProp = swModelDocExt.get_CustomPropertyManager("")

                           

                          'Read current custom properties and extract Description if there is one, else prompt for new Description

                           

                          retval = swCustPropMgr.Get4("Description", False, Description, "")
                          If Len(Description) < 2 Then
                              Description = InputBox("Enter Description", "What is this thing?")
                              End If
                             
                          'Delete current custom properties, call subroutine

                              ClearCustPrps ("")
                              vConfs = swModel.GetConfigurationNames
                             
                              For i = 0 To UBound(vConfs)
                                  ClearCustPrps (vConfs(i))
                             
                              Next
                             

                          'Define and insert new custom properties

                           

                              'Import File Name & partition
                                  FileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
                                  FileNameSize = Len(FileName)
                                  Rev = InStr(FileName, " Rev")

                           

                              'Define Part Number
                                  PartNumber = Left(FileName, Rev)
                                  retval = Part.AddCustomInfo3("", "Part Number", 30, PartNumber)

                           

                              'Define Revision
                                  Revision = Right(FileName, FileNameSize - Rev - 4)
                                  retval = Part.AddCustomInfo3("", "Revision", 30, Revision)

                           

                              retval = Part.AddCustomInfo3("", "Description", 30, Description)

                           

                              Material = Chr(34) & "SW-Material@Part1.SLDPRT" & Chr(34)

                              retval = Part.AddCustomInfo3("", "Material", 30, Material)

                           

                              Thickness = Chr(34) & "Thickness@Part1.SLDPRT" & Chr(34)

                              retval = Part.AddCustomInfo3("", "Thickness", 30, Thickness)

                           

                              KFactor = Chr(34) & "D2@Sheet-Metal2" & Chr(34)
                              retval = Part.AddCustomInfo3("", "K Factor", 30, KFactor)

                           

                          End Sub

                           

                          'Subroutine called to delete existing custom properties

                           

                          Sub ClearCustPrps(conf As String)

                              Dim j As Integer
                             
                              Set swCustPropMgr = swModel.Extension.CustomPropertyManager(conf)
                                 
                              If Not swCustPropMgr Is Nothing Then
                                  swCustPropMgr.GetAll vPropNames, Empty, Empty
                                     
                                  If Not IsEmpty(vPropNames) Then
                                      For j = 0 To UBound(vPropNames)
                                          swCustPropMgr.Delete vPropNames(j)
                                      Next
                                  End If
                                     
                              End If
                                 
                          End Sub