27 Replies Latest reply on Sep 4, 2018 3:11 AM by Federico Borghi

    API/Macro for updating a BOM

    Erik Rotolo

      You have an asm file that has a few configuration and you make a drawing with a drawing view showing config1 of the asm and you insert a BOM referencing config1, everything is good.

       

      You then change the config in the drawing view to config2, but the BOM doesn't update to match.

       

      This is annoying, so you have to select the BOM and match the config that is in the drawing view.  This is OK if its a small list of configs, but is terrible if you have a large amount of asm configs.

       

      Other method of updating BOM after changing configs of asm in drawing view, is to delete the BOM and reinsert the BOM, this also is not the best solution.

       

      I have looked into if you can always keep the BOM linked to the drawing view, but have found that this is not possible.

       

      So, does any one already have a solution to this using a macro?

       

      Or can someone make a macro to do this?

       

      The macro should look to see if there is already a BOM and if it sees there is one, then delete it and insert new BOM based on drawing view specified in sheet properties.  If no BOM is present then create one BOM based on drawing view specified in sheet properties.

      In the macro you would need to specify where and what BOM template to use and also to be able to  pick Top Asm BOM or Part BOM or Indented BOM.

      But not have the selection as a user inputs every time you run it, just as code line in the body of the macro, that could be edited later if things change.

       

      Thanks in advanced to everyone that shares on this forum.  All you contributors are amazing.

       

        • Re: API/Macro for updating a BOM
          Elmar Klammer

          Hi Erik,

           

          Goto Find which view is bringing custom property values that will give you the view specified in the drawing document settings.

          Then run a loop to get the configuration name of the referenced component and use it to set it as the BOM configuration value. Or use "Default" in your case.

           

          set View = Drawingdoc.getfirstview

          set View = View.Getnextview

          If view = "Name of drawing doc view"

          debug.print view.referencedconfiguration

           

          Elmar

            • Re: API/Macro for updating a BOM
              Erik Rotolo

              Thanks for the response, I want to use the the view specified in the drawing document settings and not "Default"

              So, how would you put the code together?  I don't know what to do with this code you have provided...i'm still a newbie

                • Re: API/Macro for updating a BOM
                  Deepak Gupta

                  Erik Rotolo wrote:

                   

                  Thanks for the response, I want to use the the view specified in the drawing document settings and not "Default"

                  So, how would you put the code together?

                  The macro Artem should work with BOMs already there in the drawing and they would change to the model configuration based on the view specified in the sheet.

                   

                  I don't know what to do with this code you have provided...i'm still a newbie

                  Copy and paste the codes from that macro into a new macro. Open a drawing with some sheets. BOM(s) and views. Change the model configuration referenced by the BOM to a different one. And now run the macro and check if sheet view config and BOM config matches for each sheet or not.

                    • Re: API/Macro for updating a BOM
                      Deepak Gupta

                      You need to make sure that there is a BOM table on the drawing. Check attached video showing that macro works as expected.

                        • Re: API/Macro for updating a BOM
                          Erik Rotolo

                          the macro is working for part level BOM and indented BOM but when i select Top Level BOM  and run the macro it deletes the QTY column, and doesnt change the BOM config to match the View used for drawing properties

                            • Re: API/Macro for updating a BOM
                              Deepak Gupta

                              The video I have posted above is showing BOM as top level BOM. So something not correct on your end as macro doesn't have lines/codes to delete the column.

                               

                              Can you share some test files having the issue you're stating.

                                • Re: API/Macro for updating a BOM
                                  Erik Rotolo

                                  please see attached files

                                    • Re: API/Macro for updating a BOM
                                      Erik Rotolo

                                      I think the error of deleting QTY column has to do with the "Restrict top-level only BOMs to one configuration"...

                                       

                                      Sorry, Its not an error nor is it really deleting.  It's disappearing, because there are no config check boxes that are checked, so no qty column results.  "Restrict top-level only BOMs to one configuration" option results in the BOM config selection to change to a drop down list, just like in the indent BOM or part BOM selection is done.

                                       

                                      So does the code need to be a little different to fix this?????

                                       

                                       

                                      "Restrict top-level only BOMs to one configuration" checked

                                      "Restrict top-level only BOMs to one configuration" unchecked

                                    • Re: API/Macro for updating a BOM
                                      Erik Rotolo

                                      Deepak Gupta is there a fix for my situation regarding "Restrict top-level only BOMs to one configuration" checked and this BOM macro?

                                        • Re: API/Macro for updating a BOM
                                          Deepak Gupta

                                          Erik, I've not got the chance to check this out (had been occupied with some personal stuff). Will try to peek in tomorrow night.

                                          • Re: API/Macro for updating a BOM
                                            Deepak Gupta

                                            Erik/ Grant, add these two line at the specified location/position and check. I had tested it out and it seems to be working.

                                             

                                              • Re: API/Macro for updating a BOM
                                                Erik Rotolo

                                                This does make the macro work, but the QTY column is being renamed, so this defeats the purpose of checking "Restrict top-level only BOMs to one configuration" , I want to use this option so the QTY column doesn't rename to the "configured name"/QTY. , when the BOM table changes. 

                                                 

                                                Is there another way to make this work?

                                                I really appreciate your time, I don't mean to be difficult. 

                                                  • Re: API/Macro for updating a BOM
                                                    Deepak Gupta

                                                    The only way I can think is to run another code of lines to reset the column heading to QTY.

                                                      • Re: API/Macro for updating a BOM
                                                        Erik Rotolo

                                                        what lines would need to be added for renaming to "QTY"  (not "QTY.")?

                                                         

                                                        Thanks

                                                          • Re: API/Macro for updating a BOM
                                                            Deepak Gupta

                                                            Look for SetColumnTitle method.

                                                            • Re: API/Macro for updating a BOM
                                                              Deepak Gupta

                                                              Add these lines 

                                                                              Dim SwBOMTab As BomTableAnnotation

                                                                              Dim SwTabAnn As TableAnnotation

                                                                              Dim k As Integer

                                                                              Dim ColumnTitle As String

                                                                             

                                                                              Set SwBOMTab = SwBOMFeat.GetTableAnnotations(0)

                                                                              Set SwTabAnn = SwBOMTab

                                                                             

                                                                              For k = 0 To SwTabAnn.ColumnCount - 1

                                                                                  ColumnTitle = SwTabAnn.GetColumnTitle2(k, False)

                                                                                 

                                                                                      If UCase(ColumnTitle) Like "*QTY*" Then

                                                                                      SwTabAnn.SetColumnTitle2 k, "QTY", False

                                                                                      End If

                                                                              Next k

                                                              below this line

                                                              SwBOMFeat.SetConfigurations False, vConfVis, vConfNames

                                                              in the ProcessView sub.

                                                                • Re: API/Macro for updating a BOM
                                                                  Federico Borghi

                                                                  Hi Deepak Gupta and thanks for your macro!

                                                                   

                                                                  In my case the Column "QTY" was called as "configuration name/Qty", so I added the tic in the box "Do not add QTY to the configuration name" and changed your code as follows

                                                                   

                                                                   

                                                                  ColumnTitle = SwTabAnn.GetColumnTitle2(k, False)

                                                                                   

                                                                                          If UCase(ColumnTitle) Like swView.ReferencedConfiguration Then

                                                                                          SwTabAnn.SetColumnTitle2 k, "Q.tà", False

                                                                   

                                                                                          End If

                                                                                  Next k

                                                                              Next

                                                                      End If

                                                                   

                                                                   

                                                                  Since I'm not using the standard font of the drawing for the first raw of the BOM the new text is not formatted as others (Verdana, 2.5mm, bold), there is any way to set it? I checked into SolidWorks library without any result.

                                                                  In add I would love to be able to:

                                                                  • Rename all the BOM of a drawing as ("Dinstinta " + SheetName) (who's set as configuration name by another macro that I already have)
                                                                  • Export all the BOM of a drawing in txt format as with right click "save as/.txt", calling it as SheetName.txt
                                                                  • link every view of a sheet to the BOM related to the sheet (SheetName=Distinta SheetName=active configuration) - This because if I copy a drawing to make the drawing of a different configuration the upper macro doesn't work as view is not connected to the right BOM but to the old one, I don't know if it can be done in a better way-
                                                                  • Set all the view of a sheet to point at  the same configuration (sometimes I have 6 views in a sheet and I manually set them to another configuration)

                                                                   

                                                                  Is that all possible?

                                                                  In the case you'll be too busy to write the code I can try to make it but I would need the command as for example "swSheet.SetName" to rename the configuration since I can't found them :/

                                                                    • Re: API/Macro for updating a BOM
                                                                      Deepak Gupta

                                                                      Federico Borghi wrote:

                                                                       

                                                                      Since I'm not using the standard font of the drawing for the first raw of the BOM the new text is not formatted as others (Verdana, 2.5mm, bold), there is any way to set it? I checked into SolidWorks library without any result.

                                                                      Not in front of SW, so will check and let you know.

                                                                      Rename all the BOM of a drawing as ("Dinstinta " + SheetName) (who's set as configuration name by another macro that I already have)

                                                                       

                                                                      link every view of a sheet to the BOM related to the sheet (SheetName=Distinta SheetName=active configuration) - This because if I copy a drawing to make the drawing of a different configuration the upper macro doesn't work as view is not connected to the right BOM but to the old one, I don't know if it can be done in a better way-

                                                                       

                                                                      You'll have to obtain the feature object and BOM on each sheet and rename the feature. Refer to the macro in this post:Macro To link all views to BOM on Sheet 1

                                                                       

                                                                      Export all the BOM of a drawing in txt format as with right click "save as/.txt", calling it as SheetName.txt

                                                                      Macro to export BOM into an Excel file

                                                                       

                                                                      Set all the view of a sheet to point at the same configuration (sometimes I have 6 views in a sheet and I manually set them to another configuration)

                                                                      Drawing API Change all view configurations

                                              • Re: API/Macro for updating a BOM
                                                Erik Rotolo

                                                The code from that link is not working, if no BOM is present, it doesnt insert one, if there is a BOM present it just deletes the QTY column, and doesnt change the BOM config to match the View used for drawing properties.

                                                 

                                                Is this not current code for SW2017?  Is there something not quite right?

                                                 

                                                 

                                                Dim swApp As SldWorks.SldWorks

                                                Dim swDraw As SldWorks.DrawingDoc

                                                 

                                                 

                                                Sub main()

                                                 

                                                 

                                                    Set swApp = Application.SldWorks

                                                 

                                                    Set swDraw = swApp.ActiveDoc

                                                 

                                                    Dim vSheetNames As Variant

                                                 

                                                    vSheetNames = swDraw.GetSheetNames

                                                 

                                                    Dim i As Integer

                                                     

                                                    For i = 0 To UBound(vSheetNames)

                                                     

                                                        Dim swSheet As SldWorks.Sheet

                                                        Set swSheet = swDraw.Sheet(vSheetNames(i))

                                                     

                                                        Dim swView As SldWorks.View

                                                        Set swView = GetPropertiesView(swSheet)

                                                     

                                                        Dim vBomFeatures As Variant

                                                        vBomFeatures = GetBomFeatures(swSheet)

                                                        ProcessView swView, vBomFeatures

                                                     

                                                    Next

                                                 

                                                End Sub

                                                 

                                                 

                                                Sub ProcessView(swView As SldWorks.View, vBomFeatures As Variant)

                                                 

                                                    If Not IsEmpty(vBomFeatures) Then

                                                     

                                                        Dim i As Integer

                                                     

                                                        For i = 0 To UBound(vBomFeatures)

                                                         

                                                            Dim swBomFeat As SldWorks.BomFeature

                                                         

                                                            Set swBomFeat = vBomFeatures(i)

                                                         

                                                            Dim vConfVis As Variant

                                                            Dim vConfNames As Variant

                                                            vConfNames = swBomFeat.GetConfigurations(False, vConfVis)

                                                         

                                                            Dim visConfIndex As Integer

                                                         

                                                            Dim j As Integer

                                                         

                                                            For j = 0 To UBound(vConfNames)

                                                             

                                                                vConfVis(j) = UCase(vConfNames(j)) = UCase(swView.ReferencedConfiguration)

                                                                             

                                                            Next

                                                         

                                                            swBomFeat.SetConfigurations False, vConfVis, vConfNames

                                                         

                                                        Next

                                                     

                                                    End If

                                                 

                                                End Sub

                                                 

                                                 

                                                Function GetBomFeatures(swSheet As SldWorks.Sheet) As Variant

                                                 

                                                    Dim vSheets As Variant

                                                    vSheets = swDraw.GetViews()

                                                 

                                                    Dim i As Integer

                                                 

                                                    For i = 0 To UBound(vSheets)

                                                 

                                                        Dim vViews As Variant

                                                        vViews = vSheets(i)

                                                     

                                                        Dim swSheetView As SldWorks.View

                                                        Set swSheetView = vViews(0)

                                                     

                                                        If UCase(swSheetView.Name) = UCase(swSheet.GetName()) Then

                                                         

                                                            Dim swBomFeatures() As SldWorks.BomFeature

                                                         

                                                            Dim vTables As Variant

                                                            vTables = swSheetView.GetTableAnnotations()

                                                         

                                                            Dim j As Integer

                                                            Dim isArrInit As Boolean

                                                         

                                                            For j = 0 To UBound(vTables)

                                                             

                                                                Dim swTableAnn As SldWorks.TableAnnotation

                                                                Set swTableAnn = vTables(j)

                                                             

                                                                If swTableAnn.Type = swTableAnnotationType_e.swTableAnnotation_BillOfMaterials Then

                                                                    If False = isArrInit Then

                                                                        isArrInit = True

                                                                        ReDim swBomFeatures(0)

                                                                    Else

                                                                        ReDim Preserve swBomFeatures(UBound(swBomFeatures) + 1)

                                                                    End If

                                                                 

                                                                    Dim swBomTableAnn As SldWorks.BomTableAnnotation

                                                                    Set swBomTableAnn = swTableAnn

                                                                 

                                                                    Set swBomFeatures(UBound(swBomFeatures)) = swBomTableAnn.BomFeature

                                                                 

                                                                End If

                                                             

                                                            Next

                                                         

                                                            GetBomFeatures = swBomFeatures

                                                            Exit Function

                                                        End If

                                                     

                                                    Next

                                                 

                                                End Function

                                                 

                                                 

                                                Function GetPropertiesView(swSheet As SldWorks.Sheet) As SldWorks.View

                                                 

                                                    Dim vViews As Variant

                                                 

                                                    vViews = swSheet.GetViews

                                                 

                                                    If Not IsEmpty(vViews) Then

                                                     

                                                        Dim i As Integer

                                                     

                                                        For i = 0 To UBound(vViews)

                                                         

                                                            Dim swView As SldWorks.View

                                                            Set swView = vViews(i)

                                                         

                                                            If UCase(swView.Name) = UCase(swSheet.CustomPropertyView) Then

                                                                Set GetPropertiesView = swView

                                                                Exit Function

                                                            End If

                                                         

                                                        Next

                                                     

                                                        Set GetPropertiesView = vViews(0) 'use first one

                                                     

                                                    End If

                                                 

                                                End Function

                                                • Re: API/Macro for updating a BOM
                                                  Erik Rotolo

                                                  Artem Taturevych is there a fix for my situation regarding "Restrict top-level only BOMs to one configuration" checked and this BOM macro?

                                                    • Re: API/Macro for updating a BOM
                                                      Grant Kirkland

                                                      I'm having the same issue. If "Restrict top-level..." is selected my QTY column is deleted.

                                                       

                                                      EDIT: This line placed in the beginning of the code turns of "restrict top-level only" which solves the problem:

                                                       

                                                      boolstatus = swDraw.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swOneConfigOnlyTopLevelBom, 0, False)

                                                       

                                                      However, if I put

                                                       

                                                      boolstatus = swDraw.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swOneConfigOnlyTopLevelBom, 0, True)

                                                       

                                                      at the end of the code all BOMs are then relinked to the BOM referenced by the active view. I don't know how to fix that.

                                                      • Re: API/Macro for updating a BOM
                                                        Artem Taturevych

                                                        Hi Erik,

                                                         

                                                        I can confirm the behavior and looks like this is a bug. I have tried to find the workaround - with no success for now. I will try to research a bit more.

                                                         

                                                        Thanks,

                                                        Artem