16 Replies Latest reply on Jun 22, 2018 9:25 AM by Robert Voogt

    Retrieving Existing BOM Configuration

    Robert Voogt

      Hi all,

       

      I use this code to find the BOM name.
      But I need its configuration. Keep in mind that it is a top level only BOM

       

      Dim swApp As SldWorks.SldWorks
      Private Const strApp As String = "Link2BOM v1.00"
      Public Sub subMain()
          Dim iCnt, iTotal As Integer
          Dim retVal As Variant
          Dim sBOMname As String
          Dim vSheetNames As Variant
          
          Dim swView As SldWorks.View
          Dim swSheetView As SldWorks.View
          Dim swModel As SldWorks.ModelDoc2
          Dim swDrawing As SldWorks.DrawingDoc
          Dim swFeat As SldWorks.Feature
          Dim swBOM As SldWorks.Feature
          
          Set swApp = Application.SldWorks
          Set swModel = swApp.ActiveDoc
          Set swDrawing = swModel
          Set swFeat = swModel.FirstFeature
          
          'Get BOM name
          Do While Not swFeat Is Nothing
              If "BomFeat" = swFeat.GetTypeName Then
                  Set swBomFeat = swFeat.GetSpecificFeature2
                  Set swBOM = swBomFeat.GetFeature
                  sBOMname = swBOM.Name
                  Exit Do
              End If
              Set swFeat = swFeat.GetNextFeature
          Loop
          
          vSheetNames = swDrawing.GetSheetNames
          iTotal = UBound(vSheetNames) + 1
          iCnt = 0
          Do While iCnt < iTotal
              'Set view linked to BOM
              retVal = swDrawing.ActivateSheet(vSheetNames(iCnt))
              Set swSheetView = swDrawing.GetFirstView
              Set swView = swSheetView.GetNextView
              Do While Not swView Is Nothing
                  retVal = swView.SetKeepLinkedToBOM(True, sBOMname)
                  Set swView = swView.GetNextView
              Loop
              iCnt = iCnt + 1
          Loop
          retVal = swDrawing.ActivateSheet(vSheetNames(0))
          
          swApp.SendMsgToUser2 "       Linking is complete...       ", swMbInformation, swMbOk
          Set swApp = Nothing
          Set swModel = Nothing
          Set swDrawing = Nothing
          Set swFeat = Nothing
          Set swBomFeat = Nothing
          Set swBOM = Nothing
          Set swSheetView = Nothing
          Set swView = Nothing
          
          End
      End Sub
      

       

      Whatever I try

       

      Dim sBOMconfig As Variant

      Dim swBomFeat As SldWorks.BomFeature

      Dim swFeat As SldWorks.Feature

       

      Set swFeat = swModel.FirstFeature

      Set swBomFeat = swFeat.BomFeature (or Set swBomFeat = swFeat.GetSpecificFeature2)

       

      sBOMconfig = swBomFeat.GetConfigurations(True, Visible)

       

      I cannot seem to get the configuration being used.

       

      Further I would also like to see it only look for the configuration of the visible BOM. All other BOM's in the sheet that are hidden should be ignored.

        • Re: Retrieving Existing BOM Configuration
          Alex Burnett

          I don't have time to test it out but the approach that comes to mind is to get a reference to the view that the BOM is linked to. From there, get the referenced model from the view. At that point, you should be able to get the shown configuration of the model.

            • Re: Retrieving Existing BOM Configuration
              Robert Voogt

              I got it to work by changing this in my code (another macro, the one posted above was just a test-case to find out how to retrieve the BOM)

               

              from (gets wrong configuration):

                      Set swView = swDraw.ActiveDrawingView

                      Views = swDraw.GetCurrentSheet.GetViews

                      Set swView = Views(0)

                      configuration = swView.ReferencedConfiguration

               

              to (gets correct configuration)

                      Set swSheetView = swDraw.GetFirstView

                      Set swView = swSheetView.GetNextView

                      configuration = swView.ReferencedConfiguration

               

              Now off to check if it holds up to a drawing with multiple sheets!

            • Re: Retrieving Existing BOM Configuration
              Josh Brady
                  'Get BOM name
                  Dim bomAnnots As Variant
                  Dim Cfgs As Variant
                  Dim Viz As Variant
                  Do While Not swFeat Is Nothing
                      If "BomFeat" = swFeat.GetTypeName Then
                          Set swbomfeat = swFeat.GetSpecificFeature2
                          bomAnnots = swbomfeat.GetTableAnnotations
                          If bomAnnots(0).GetAnnotation.Visible = 1 Then
                              Set swBOM = swbomfeat.GetFeature 
                              sBOMname = swBOM.Name
                              Cfgs = swbomfeat.GetConfigurations(True, Viz)
                              MsgBox Cfgs(0)
                              Exit Do
                          End If
                      End If
                      Set swFeat = swFeat.GetNextFeature
                  Loop
              

               

               

              This code assumes:

              (1) your BOM is only showing one configuration.  If it's showing multiple configs, who knows which one will be returned by this code.

              (2) Your BOM feature only has one BOM table annotation attached to it.  A BOM feature can be split into multiple table annotations.  Based on your post I assume this has not been done.

                • Re: Retrieving Existing BOM Configuration
                  Robert Voogt

                  1) our boms are always just one configuration

                  2) I think so yes.

                   

                  Can you explain to me why it needs to loop? is it for checking every view if it is the BOM linked view?

                    • Re: Retrieving Existing BOM Configuration
                      Robert Voogt

                      it works perfectly btw

                      • Re: Retrieving Existing BOM Configuration
                        Josh Brady

                        Robert Voogt wrote:

                         

                        1) our boms are always just one configuration

                        2) I think so yes.

                         

                        Can you explain to me why it needs to loop? is it for checking every view if it is the BOM linked view?

                         

                        I just took your loop and modified it...

                         

                        The code is pretty straightforward.   You need to read every line and understand what it's doing.  Otherwise you're just pasting snippets of things you don't understand, and you'll end up with unexpected results.

                         

                        The bit that I modified from your posted code starts at the top of the feature tree.  It goes feature by feature, looking for BOMs in the tree.  Once it finds a BOM, it checks to see if the corresponding table is visible or not.  If it's visible, it gets the BOM name and the linked configuration.  It then EXITS THE LOOP.  It stops looking for BOMs in this entire document.

                         

                        This means that the loop I posted will only ever get the first BOM in the tree that is visible. (the loop you posted will only get the first BOM period, whether it's visible or not).  That "exit do" statement makes it jump out of the loop.

                         

                        That loop doesn't know what sheet the BOM it found is on.  It just knows it found a BOM and it told you the name.

                         

                        The rest of your code in your original post goes sheet-by-sheet, linking every view on that sheet to the BOM found IN THE ORIGINAL LOOP.  If you want this code link all views on a sheet to the BOM ON THAT SHEET then you'll have to rearrange/rewrite your code significantly.

                          • Re: Retrieving Existing BOM Configuration
                            Robert Voogt

                            Well, I tried it out now on a drawing with multiple sheets, each having a different configuration in the BOM. It found the correct configuration for each sheet. (and all configurations are available on each sheet)

                              • Re: Retrieving Existing BOM Configuration
                                Josh Brady

                                Robert Voogt wrote:

                                 

                                Well, I tried it out now on a drawing with multiple sheets, each having a different configuration in the BOM. It found the correct configuration for each sheet. (and all configurations are available on each sheet)

                                OK... What do you mean by this?  Do you mean the loop I posted?  Or the one where you're looking at the view's configuration?  Because the loop I posted will only ever find one BOM.

                                  • Re: Retrieving Existing BOM Configuration
                                    Robert Voogt

                                    Hi Josh,

                                     

                                    I use your loop, but I start your loop for each sheet in the drawing again. This appears to find the correct configuration of the old BOM (/view) and inputs it as the configuration of the new BOM. (even though some sheets have multiple hidden BOMs besides the visible one)

                                      • Re: Retrieving Existing BOM Configuration
                                        Josh Brady

                                        OK... That loop will never find any BOM except the first visible one on the tree, no matter what sheet is currently active.  Please verify that you're finding the BOM that you really want by checking its name...

                                          • Re: Retrieving Existing BOM Configuration
                                            Robert Voogt

                                            Hi Josh, I will try to explain.

                                             

                                            I have a drawing of a machine. This machine has multiple configurations: CW-in, CW-out, CCW-in and CCW-out. There is for each of those configurations a separate sheet in the drawing. Some of these sheet have more than 1 BOM (But only 1 is visible).

                                             

                                            Your code placed in my code looks in each sheet for the visible BOM, therefor on each sheet it shows the correct configuration in the new BOM.

                                             

                                            Every sheet in our drawings will always have only 1 visible BOM.

                                              • Re: Retrieving Existing BOM Configuration
                                                Josh Brady

                                                Robert, I will try to explain.

                                                 

                                                No matter which sheet of the drawing is active, no matter how many times you run it, the loop of code that I posted WILL ONLY EVER FIND THE FIRST BOM IN THE FEATURE TREE THAT IS VISIBLE.  That BOM may or may not be on the currently active sheet.

                                                 

                                                Below I have modified your code to run my loop multiple times, once before switching sheets and then with every sheet.  Every single time that loop runs, it finds the first BOM.

                                                 

                                                I've attached a simplified test drawing that represents (as I understand) your situation.  Running this code against this drawing always returns the same BOM.

                                                 

                                                Dim swApp As SldWorks.SldWorks
                                                Private Const strApp As String = "Link2BOM v1.00"
                                                Public Sub subMain()
                                                    Dim iCnt, iTotal As Integer
                                                    Dim retVal As Variant
                                                    Dim sBOMname As String
                                                    Dim vSheetNames As Variant
                                                      
                                                    Dim swView As SldWorks.View
                                                    Dim swSheetView As SldWorks.View
                                                    Dim swModel As SldWorks.ModelDoc2
                                                    Dim swDrawing As SldWorks.DrawingDoc
                                                    Dim swFeat As SldWorks.Feature
                                                    Dim swBOM As SldWorks.Feature
                                                      
                                                    Set swApp = Application.SldWorks
                                                    Set swModel = swApp.ActiveDoc
                                                    Set swDrawing = swModel
                                                    
                                                      
                                                    'Get BOM name
                                                    Dim bomAnnots As Variant
                                                    Dim Cfgs As Variant
                                                    Dim Viz As Variant
                                                    
                                                    MsgBox "OK, let's look for BOMs"
                                                    Set swFeat = swModel.FirstFeature
                                                    Do While Not swFeat Is Nothing
                                                        If "BomFeat" = swFeat.GetTypeName Then
                                                            Set swbomfeat = swFeat.GetSpecificFeature2
                                                            bomAnnots = swbomfeat.GetTableAnnotations
                                                            If bomAnnots(0).GetAnnotation.Visible = 1 Then
                                                                Set swBOM = swbomfeat.GetFeature
                                                                sBOMname = swBOM.Name
                                                                Cfgs = swbomfeat.GetConfigurations(True, Viz)
                                                                Exit Do
                                                            End If
                                                        End If
                                                        Set swFeat = swFeat.GetNextFeature
                                                    Loop
                                                    MsgBox "First time running my loop.  We found " & sBOMname & " which references config " & Cfgs(0)
                                                    
                                                    
                                                    vSheetNames = swDrawing.GetSheetNames
                                                    iTotal = UBound(vSheetNames) + 1
                                                    iCnt = 0
                                                    Do While iCnt < iTotal
                                                        'Set view linked to BOM
                                                        retVal = swDrawing.ActivateSheet(vSheetNames(iCnt))
                                                        MsgBox "OK, we activated sheet """ & vSheetNames(iCnt) & """. Let's run my loop again:"
                                                        '''''''''''
                                                        Set swFeat = swModel.FirstFeature
                                                        Do While Not swFeat Is Nothing
                                                            If "BomFeat" = swFeat.GetTypeName Then
                                                                Set swbomfeat = swFeat.GetSpecificFeature2
                                                                bomAnnots = swbomfeat.GetTableAnnotations
                                                                If bomAnnots(0).GetAnnotation.Visible = 1 Then
                                                                    Set swBOM = swbomfeat.GetFeature
                                                                    sBOMname = swBOM.Name
                                                                    Cfgs = swbomfeat.GetConfigurations(True, Viz)
                                                                    Exit Do
                                                                End If
                                                            End If
                                                            Set swFeat = swFeat.GetNextFeature
                                                        Loop
                                                        MsgBox "This time we found " & sBOMname & " which references config " & Cfgs(0)
                                                        '''''''''''
                                                        Set swSheetView = swDrawing.GetFirstView
                                                        Set swView = swSheetView.GetNextView
                                                        Do While Not swView Is Nothing
                                                            'retVal = swView.SetKeepLinkedToBOM(True, sBOMname)
                                                            Set swView = swView.GetNextView
                                                        Loop
                                                        iCnt = iCnt + 1
                                                    Loop
                                                    retVal = swDrawing.ActivateSheet(vSheetNames(0))
                                                      
                                                    'swApp.SendMsgToUser2 "       Linking is complete...       ", swMbInformation, swMbOk
                                                    Set swApp = Nothing
                                                    Set swModel = Nothing
                                                    Set swDrawing = Nothing
                                                    Set swFeat = Nothing
                                                    Set swbomfeat = Nothing
                                                    Set swBOM = Nothing
                                                    Set swSheetView = Nothing
                                                    Set swView = Nothing
                                                      
                                                    End
                                                End Sub
                                                
                                                  • Re: Retrieving Existing BOM Configuration
                                                    Robert Voogt

                                                    I will try out your code next week (my pc is back to the shop for repairs).

                                                     

                                                    But your other code did find the correct BOM on each sheet of the same drawing and didn't keep the first one found. I have tried it on a couple of drawings.

                                                     

                                                    My code processes each sheet separatly and in that segment of code I added your bit (slightly altered of course).