7 Replies Latest reply on Jun 14, 2018 1:37 PM by Grega Jerin

    Checking for BOM, and deleting the old one(s)

    Robert Voogt

      Hi all,

       

      I use this code to check if the sheet contains a BOM

       

              Set swView = swDraw.GetFirstView
              Set swTableAnn = swView.GetFirstTableAnnotation
              On Error GoTo Label1
              If swTableAnn.Type = swTableAnnotation_BillOfMaterials Then
              Debug.Print "There is a BOM"
              swBom = 1
              Else
      Label1:
              Debug.Print "NO BOM"
              swBom = 0
              End If
              
      

       

      But if there is no BOM,

      If swTableAnn.Type = swTableAnnotation_BillOfMaterials Then
      

      gives an error. (object variable or with block variable not set)

      I tried to circumvent it with an 'on error goto label1' code... but that doesn't work when a drawing has more than 1 sheet.

       

      ***

       

      I use this code to delete all BOMs, but I need it to do it only for the active sheet. (else it will also delete a bom on sheet 2 and then the code above thinks that that sheet never had a bom). It does need to delete all BOMs per sheet though because sometimes the engineers have multiple BOM's on a single sheet.

       

              While Not swFeat Is Nothing
              If "BomFeat" = swFeat.GetTypeName Then
              swFeat.Select2 True, -1
              End If
              Set swFeat = swFeat.GetNextFeature
              Wend
              swModel.Extension.DeleteSelection2 swDeleteSelectionOptions_e.swDelete_Absorbed
      
        • Re: Checking for BOM, and deleting the old one(s)
          Daniel Kuhn

          Hi Robert,

           

          try this code, maybe it helps.

          I run also in the same issue with "GetNext Method (ITableAnnotation)".

          Therefore, in this case, I only use "GetFirstTableAnnotation" to check if an object exists.

          Then I use "GetTableAnnotations" and the "For Each" loop.

          Maybe someone else has a better solution?

           

          Option Explicit
          
          Dim swApp As SldWorks.SldWorks
          Dim swModel As SldWorks.ModelDoc2
          Dim swDraw As SldWorks.DrawingDoc
          Dim swSheet As SldWorks.Sheet
          Dim swViews As Variant
          Dim swView As SldWorks.View
          Dim swTableAnn As SldWorks.TableAnnotation
          Dim swTableAnns As Variant
          Dim tableAnn As Variant
          Dim swAnn As SldWorks.Annotation
          
          Sub main()
          
          Set swApp = Application.SldWorks
          Set swModel = swApp.ActiveDoc
          
          If swModel.GetType <> swDocDRAWING Then Exit Sub
          
          Set swDraw = swModel
          Set swSheet = swDraw.GetCurrentSheet
          
          swViews = swSheet.GetViews
          
          Set swView = swDraw.GetFirstView
          ' This code removes all BOMs per view.
          While Not swView Is Nothing
              Set swTableAnn = swView.GetFirstTableAnnotation
              If Not swTableAnn Is Nothing Then
                  swTableAnns = swView.GetTableAnnotations
                  For Each tableAnn In swTableAnns
                      Set swTableAnn = tableAnn
                      If swTableAnn.Type = swTableAnnotation_BillOfMaterials Then
                          Set swAnn = swTableAnn.GetAnnotation
                          swAnn.Select False
                          swModel.Extension.DeleteSelection2 swDeleteSelectionOptions_e.swDelete_Absorbed
                      End If
                  Next
              End If
          
              Set swView = swView.GetNextView
          Wend
          
          End Sub
          

           

          Best regards

          Daniel

          • Re: Checking for BOM, and deleting the old one(s)
            Grega Jerin

            Is it possible to locate a BOM on a drawing and rename it to "BillOfMaterials1" or something that I will define? I usually have more than one BOM on drawing and one is located on a sheet and anchored (top level), the other one is of the sheet and is used for excel export (parts only).

            I want to run a macro that links ancored BOM to ballons on views and it needs to have a defined name so that this macro can find it. I'm usually running this macro for each sheet manually but now I want to make a macro that will call a few other submacros and make some tasks automatic.

            Problem is that when deleting and putting new BOMs on drawing, they are always named with one number higher and first BOM is not always named with number 1.

              • Re: Checking for BOM, and deleting the old one(s)
                Daniel Kuhn

                Hi Grega,

                 

                yes, you can. Here is some example code:

                 

                ' Insert BOM
                Set swBOMAnnotation = swView.InsertBomTable3(True, 0.4, 0.3, AnchorType, BomType, Configuration, TableTemplate, False)
                
                ' Show Name of BOM
                Debug.Print swBOMAnnotation.BomFeature.Name
                    
                ' Set Name of BOM
                swBOMAnnotation.BomFeature.Name = "TestBom"
                

                 

                hope it helps.

                 

                Best regards

                Daniel

                  • Re: Checking for BOM, and deleting the old one(s)
                    Grega Jerin

                    Nice. Thanks for answer. But does this macro recognize already inserted BOM that is on anchor point or does it insert new one and then renames it?

                      • Re: Checking for BOM, and deleting the old one(s)
                        Daniel Kuhn

                        No, this is only a code snippet for inserting and rename of the BOM.

                        But if you want to find the Anchored BOM and rename them, you can try this code:

                         

                        Option Explicit
                          
                        Dim swApp As SldWorks.SldWorks
                        Dim swModel As SldWorks.ModelDoc2
                        Dim swDraw As SldWorks.DrawingDoc
                        Dim swSheet As SldWorks.Sheet
                        Dim swViews As Variant
                        Dim swView As SldWorks.View
                        Dim swTableAnn As SldWorks.TableAnnotation
                        Dim swTableAnns As Variant
                        Dim tableAnn As Variant
                        Dim swAnn As SldWorks.Annotation
                          
                        Sub main()
                          
                        Set swApp = Application.SldWorks
                        Set swModel = swApp.ActiveDoc
                          
                        If swModel.GetType <> swDocDRAWING Then Exit Sub
                          
                        Set swDraw = swModel
                        Set swSheet = swDraw.GetCurrentSheet
                          
                        swViews = swSheet.GetViews
                          
                        Set swView = swDraw.GetFirstView
                        ' This code removes all BOMs per view.
                        While Not swView Is Nothing
                            Set swTableAnn = swView.GetFirstTableAnnotation
                            If Not swTableAnn Is Nothing Then
                                swTableAnns = swView.GetTableAnnotations
                                For Each tableAnn In swTableAnns
                                    Set swTableAnn = tableAnn
                                    
                                    ' Just for your information:
                                    Debug.Print "Table Anchored:    " & swTableAnn.Anchored
                                    Debug.Print "Table AnchorType:  " & swTableAnn.AnchorType
                                    
                                    If swTableAnn.Type = swTableAnnotation_BillOfMaterials Then
                        
                                        Dim swBOMAnnotation As SldWorks.BomTableAnnotation
                                        Set swBOMAnnotation = swTableAnn
                                        
                                        Debug.Print "Name of BOM:       " & swBOMAnnotation.BomFeature.Name
                                    
                                        If swTableAnn.Anchored = 1 Then
                                            swBOMAnnotation.BomFeature.Name = swBOMAnnotation.BomFeature.Name & " Anchored"
                                        Else
                                            swBOMAnnotation.BomFeature.Name = swBOMAnnotation.BomFeature.Name & " NotAnchored"
                                        End If
                                        
                                        '' If you want to delete
                                        'Set swAnn = swTableAnn.GetAnnotation
                                        'swAnn.Select False
                                        'swModel.Extension.DeleteSelection2 swDeleteSelectionOptions_e.swDelete_Absorbed
                                    End If
                                Next
                            End If
                          
                            Set swView = swView.GetNextView
                        Wend
                        
                        swModel.ForceRebuild3 True
                          
                        End Sub
                        

                         

                        Best regards

                        Daniel