15 Replies Latest reply on Apr 29, 2015 2:15 PM by David Dewey

    Partnumber-configuation Macro

    Thomas Alberts

      I need to update the configuration properties for a large batch of parts.  I specifically need the part number to show up as "document name" - "Configuration Name"

       

      Does anyone have a maro that updates the part number displayed in the BOM?

       

      Thanks

        • Re: Partnumber-configuation Macro
          Deepak Gupta

          Welcome to SOLIDWORKS forums Thomas.

           

          Do you need the macro to run on active part file OR bunch of files in a selected folder.

           

          You might also find this post helpful for your future reference.

            • Re: Partnumber-configuation Macro
              Thomas Alberts

              Thanks Deepak,  I was actually using your "export Flat Pattern View (configurations) as a template.

               

              Ultimately I would want both.. But given either, I could product the other. 

               

              I've been trying to use: EditConfiguration3() without success.

               

               

              When I record a marco of the manual process it looks like:

               

              Sub main()

              Set swApp = _
              Application.SldWorks

              Set Part = swApp.ActiveDoc
              boolstatus = Part.Extension.SelectByID2("-01", "CONFIGURATIONS", 0, 0, 0, False, 0, Nothing, 0)
              Part.ClearSelection2 True
              Part.SetMaterialPropertyName2 "-01", "C:/Program Files/SolidWorks Corp/SolidWorks/lang/english/sldmaterials/ch.sldmat", "20 GA SS"
              boolstatus = Part.EditConfiguration3("-01", "-01", "", "NEWNAME-01", 36)
              End Sub

               

              Thanks

                • Re: Partnumber-configuation Macro
                  Deepak Gupta

                  Thomas, here are the two macros; one for active file and second one for the batch files.

                    • Re: Partnumber-configuation Macro
                      Yong Ning

                      Qeustion:

                       

                      Set Configuration BOM Property to Configuration Name.swp

                      How to  set Configuration Bill Of Materials options to Configuration Name?

                       

                      key code →

                              Set sConfig = swModel.GetConfigurationByName(sConfigName) →  sConfig name

                              

                              what mean for BOMPartNoSource?

                       

                              sConfig.BOMPartNoSource = swBOMPartNumber_ConfigurationName

                      • Re: Partnumber-configuation Macro
                        Thomas Alberts

                        Deepak,

                         

                        Finally getting back to this.. It works as it should, I just need help with the "File Name"  I would like to replace the user specified Bom part number with the current part number + configuration.

                         

                        Sub main()

                            Dim swApp                       As SldWorks.SldWorks
                            Dim swModel                     As SldWorks.ModelDoc2
                            Dim sConfigNameArr              As Variant
                            Dim sConfigName                 As Variant
                            Dim sConfig                     As SldWorks.Configuration
                            Dim nErrors                     As Long
                            Dim nWarnings                   As Long
                           
                            Set swApp = Application.SldWorks
                            Set swModel = swApp.ActiveDoc
                            sConfigNameArr = swModel.GetConfigurationNames

                            For Each sConfigName In sConfigNameArr
                                Set sConfig = swModel.GetConfigurationByName(sConfigName)
                                sConfig.BOMPartNoSource = swBOMPartNumber_UserSpecified
                               
                                sConfig.AlternateName = sConfigName
                                sConfig.UseAlternateNameInBOM = True
                                sConfig.AlternateName = "FileName" + sConfigName

                            Next
                           
                            swModel.ForceRebuild3 False
                            swModel.ShowNamedView2 "*Isometric", 7
                            swModel.ViewZoomtofit2
                            swModel.Save3 swSaveAsOptions_Silent, nErrors, nWarnings

                        End Sub

                         

                        Thanks again!!

                          • Re: Partnumber-configuation Macro
                            Deepak Gupta

                            Here are the updated codes:

                             

                            Option Explicit

                                Dim swApp                       As SldWorks.SldWorks

                                Dim swModel                     As SldWorks.ModelDoc2

                                Dim sConfig                     As SldWorks.Configuration

                                Dim sConfigNameArr              As Variant

                                Dim sConfigName                 As Variant

                                Dim FileName                    As String

                                Dim nErrors                     As Long

                                Dim nWarnings                   As Long

                             

                             

                            Sub main()

                               

                                Set swApp = Application.SldWorks

                                Set swModel = swApp.ActiveDoc

                                sConfigNameArr = swModel.GetConfigurationNames

                             

                             

                                For Each sConfigName In sConfigNameArr

                                    Set sConfig = swModel.GetConfigurationByName(sConfigName)

                                    sConfig.BOMPartNoSource = swBOMPartNumber_UserSpecified

                                   

                                    If InStr(UCase(swModel.GetTitle), ".SLD") > 0 Then

                                        FileName = Left(swModel.GetTitle, Len(swModel.GetTitle) - 7)

                                    Else

                                        FileName = swModel.GetTitle

                                    End If

                             

                             

                                    sConfig.AlternateName = sConfigName

                                    sConfig.UseAlternateNameInBOM = True

                                    sConfig.AlternateName = FileName + " " + sConfigName

                                Next

                               

                                swModel.ForceRebuild3 False

                                swModel.ShowNamedView2 "*Isometric", 7

                                swModel.ViewZoomtofit2

                                swModel.Save3 swSaveAsOptions_Silent, nErrors, nWarnings

                            End

                              • Re: Partnumber-configuation Macro
                                David Dewey

                                Deepak,

                                Thank you for sharing so much great information!

                                 

                                I am trying to use the above script and change the name to the following:

                                 

                                        sConfig.AlternateName = "1/4T201"

                                 

                                However... whenever I use the "/" character, it reverts the name to "default". Are you aware of any work arounds that could be used for this? Unfortunately changing to a "-" works but our ERP system is setup to only accept a "/" in the name currently.

                                  • Re: Partnumber-configuation Macro
                                    Deepak Gupta

                                    David, this is a current limitation. Please vote for SPR #: 174971

                                      • Re: Partnumber-configuation Macro
                                        David Dewey

                                        Hello Deepak. I was informed by our VAR today that SPR#'s are up to 800,000 and this will likely never get addressed.

                                         

                                        There is no way we will be able to change away from using this field for our part numbers. There is also no way we would be able to change our ERP system to not use the "/" character.

                                         

                                        Are you aware of any other workarounds that could be done? Screen recording typing characters in? Time isn't necessarily a huge factor, but making sure this gets changed in an automated fashion consistently is the main end goal.

                                          • Re: Partnumber-configuation Macro
                                            Deepak Gupta

                                            David, I would still advise you to ask your VAR to connect you that SPR.

                                             

                                            For your issue, you could set up a design table and add in that required property with / character. Later you may delete the design table if not required.

                                              • Re: Partnumber-configuation Macro
                                                David Dewey

                                                They did connect me to the SPR, but apparently this is somehow looked at as a filename? That was according to our rep... not sure how accurate that is or not.

                                                 

                                                I do like your idea of the design table, it would solve the same issues without using a macro, however this would only work on next or modified parts. We have literally tens of thousands of models that already have parts that we pack and go from. This would be easier handled with an API... maybe it will surprise us and get solved in SP3.0!

                                    • Re: Partnumber-configuation Macro
                                      James Harvey

                                      Deepak,

                                       

                                      I am imploring your help. You seem to be the resident expert on API's and many other things SW. I am very reluctant to ask for help because I want to (and will) learn these things on my own. For now I am trying to relieve a nuisance and I am under a time pressure. I am 100% new to tinkering with API's and find the logic & terminology quite confusing. I need a lot of practice. As you read my questions below, I'm sure to reveal my ignorance in creating design tables, configurations, and how SolidWorks properties (custom or otherwise) are created & connected. I am hopeful that you can help erase some of this ignorance.

                                       

                                      By the description above, your attached macro "Set Configuration BOM Property to Configuration Name" is almost what I am trying to do, the difference being that I need to create an API that will "Set Configuration BOM Property to Document Name". I suspect accomplishing this would be a matter of changing a variable name in the code?

                                      Note: Our file-naming convention is the part number of the component. The configurations in question are part of a large design table, each configuration based on & named by length. I would be very grateful if you could provide me a macro to accomplish my goal. I hope I'm not asking too much.

                                      1. When the design table was created, it apparently defaulted to Configuration Properties>Bill of Materials Options>Configuration Name. Is there a setting where in creating design tables they would have chosen Document Name?
                                      2. My workaround has been to expand the component column in the BOM (almost always in drawing environment), RMB the component to access Component Options and then modifying Part Number via the dropdown for Document Name/Configuration Name/User Specified Name. Furthermore:
                                        • When I choose Document Name from the dropdown, it has no effect. Can you explain why?
                                          • This is also true if I open the part document, change the properties of the specific configuration used in the BOM, and save. No effect- config name still displays in part number column. Again, why is this?
                                        • My workaround continued is to dropdown to Document Name, select the text, copy, dropdown again to User Specified Name, paste. Only this sequence will successfully display the correct info in the part number column. Furthermore again:
                                          • This work is consistently undone somewhere in the rebuild/save/file-close/file-open process (not sure) requiring me to scan all BOM tables for errors & redo before converting to pdf for release.

                                       

                                      I learned the above ritual through persistent trial & error, and though there is not a huge amount of this to contend with you can probably imagine how I'm weary of it. And apologies for the very long post, these things are a bit difficult to verbalize with sure clarity. I hope what I've written above is easy to follow.

                                       

                                      In all honesty I'm not even sure how to implement the macros attached to your post above but I will be experimenting with that while waiting hopefully for a positive reply. Any help would be greatly appreciated.

                                       

                                      SW2015, SP1.1

                                       

                                      Best Regards,

                                      James