7 Replies Latest reply on Nov 21, 2018 1:19 PM by Don Carter

    Macro to Hide/Show Tables

    Don Carter

      Is there a macro or set of macros that could be combined that could:

      1. Hide all tables in a drawing on all sheets;
      2. Show tables having specific names on the first sheet of a drawing, i.e. these three tables "BOM Table" "Revision Table" "TB" (no quotes).
          • Sheet1 may not be named Sheet1.

       

      Based on how I've seen other macros work, I would think it easier to hide all tables before showing specific tables, but that is a presumption on my part, not a requirement.

       

      I have no ability to write a macro, so any and all assistance is greatly appreciated !!!

      SWx 2017 SP3.0; WIndows 7 and 10

        • Re: Macro to Hide/Show Tables
          Fifi Riri

          Hello, try this:

           

          Option Explicit

          Dim swApp       As SldWorks.SldWorks

          Dim swDraw      As SldWorks.DrawingDoc

          Dim swView      As SldWorks.View

          Dim swAnn       As SldWorks.Annotation

          Dim swTableAnn  As SldWorks.TableAnnotation

          Dim swFeat      As SldWorks.Feature

          Dim swGeneralTableFeature As SldWorks.GeneralTableFeature

          Dim swBomTableFeature As SldWorks.BomFeature

          Dim swRevTableFeature As SldWorks.RevisionTableFeature

          Dim swBlockTableFeature As SldWorks.TitleBlockTableFeature

          Dim vSheetNames As Variant

          Dim swTables    As Variant

          Dim swTable     As Variant

          Dim i           As Integer

          Dim boolstatus  As Boolean

          Sub main()

          Set swApp = Application.SldWorks

          Set swDraw = swApp.ActiveDoc

          vSheetNames = swDraw.GetSheetNames

          For i = UBound(vSheetNames) To 0 Step -1

              boolstatus = swDraw.ActivateSheet(vSheetNames(i))

              Set swView = swDraw.GetFirstView

              If swView.GetTableAnnotationCount <> 0 Then

                  swTables = swView.GetTableAnnotations

                  For Each swTable In swTables

                      Set swTableAnn = swTable

                      Set swAnn = swTableAnn.GetAnnotation

                      Select Case swTableAnn.Type

                      Case swTableAnnotationType_e.swTableAnnotation_General

                          Set swGeneralTableFeature = swTableAnn.GeneralTableFeature

                          Set swFeat = swGeneralTableFeature.GetFeature

                      Case swTableAnnotationType_e.swTableAnnotation_RevisionBlock

                          Set swRevTableFeature = swTableAnn.RevisionTableFeature

                          Set swFeat = swRevTableFeature.GetFeature

                      Case swTableAnnotationType_e.swTableAnnotation_BillOfMaterials

                          Set swBomTableFeature = swTableAnn.BomFeature

                          Set swFeat = swBomTableFeature.GetFeature

                      Case swTableAnnotationType_e.swTableAnnotation_TitleBlock

                          Set swBlockTableFeature = swTableAnn.TitleBlockTableFeature

                          Set swFeat = swBlockTableFeature.GetFeature

                      Case Else

                          Set swFeat = Nothing

                      End Select

                      If swFeat Is Nothing Then

                          swAnn.Visible = swAnnotationVisibilityState_e.swAnnotationHidden

                          Debug.Print "Hide   " & swTableAnn.Title

                      Else

                          'Debug.Print swFeat.Name

                          If i <> 0 Or (i = 0 And (swFeat.Name <> "BOM Table" And swFeat.Name <> "Revision Table" And swFeat.Name <> "TB")) Then

                              swAnn.Visible = swAnnotationVisibilityState_e.swAnnotationHidden

                              Debug.Print "Hide   " & swFeat.Name

                          Else

                              swAnn.Visible = swAnnotationVisibilityState_e.swAnnotationVisible

                              Debug.Print "Show   " & swFeat.Name

                          End If

                      End If

                      Debug.Print

                  Next

              End If

          Next

          swDraw.ClearSelection2 True

          End Sub

            • Re: Macro to Hide/Show Tables
              Don Carter

              Fifi Riri,

              Thanx for your work.  Much appreciated .... however, it only hid all tables on the drawing.  It did not cause the 3 tables (that I need) to show.   The drawing that I tested on is 1 sheet.

               

              With all tables hidden, I ran it again.  Nothing happened --- which I expected.

                • Re: Macro to Hide/Show Tables
                  Fifi Riri

                  Could you post your files

                    • Re: Macro to Hide/Show Tables
                      Don Carter

                      Standby .... I'm doing my tests on our proprietary files ..... I'll create a dummy set.

                       

                      Thanx

                      • Re: Macro to Hide/Show Tables
                        Don Carter

                        Sample asm / drawing attached.

                        I've used the motion path tutorial that comes with SWx 2017. None of the Custom Properties exist or match what we use, so the drawing  Title Block, BOM, etc contain a lot of blank fields.  However, the BOM recognizes the number of items, so I know that part of SWx is functioning normally.

                        What you see is an asm drawing with 2 tables visible - a Revision Table and a BOM.  A production drawing would have these plus perhaps a hole chart, or some inspection dimensions in a General Table, a CutList, etc.

                        If you expand the Feature Manager, there is a second BOM and another table called "Title Block Table", both of which are hidden.

                        You will notice that the Header of each table is hidden/collapsed.  The Header of every table has a unique Display Title.

                         

                        What I want this macro to do is to show or leave visible the 3 tables having the Display Headers of "BOM Table", "Revision Table" and "TB."

                         

                        Right now, it hides all tables.   I've attached the macro also.  Did I do that part right?

                         

                        (FYI, when those 3 tables are visible, we run another macro that exports them as xls files, from which we build our production BOM.)

                          • Re: Macro to Hide/Show Tables
                            Fifi Riri

                            Here you go

                             

                            Option Explicit

                            Dim swApp       As SldWorks.SldWorks

                            Dim swDraw      As SldWorks.DrawingDoc

                            Dim swView      As SldWorks.View

                            Dim swAnn       As SldWorks.Annotation

                            Dim swTableAnn  As SldWorks.TableAnnotation

                            Dim vSheetNames As Variant

                            Dim swTables    As Variant

                            Dim swTable     As Variant

                            Dim i           As Integer

                            Dim boolstatus  As Boolean

                            Sub main()

                            Set swApp = Application.SldWorks

                            Set swDraw = swApp.ActiveDoc

                            vSheetNames = swDraw.GetSheetNames

                            For i = UBound(vSheetNames) To 0 Step -1

                                boolstatus = swDraw.ActivateSheet(vSheetNames(i))

                                Set swView = swDraw.GetFirstView

                                If swView.GetTableAnnotationCount <> 0 Then

                                    swTables = swView.GetTableAnnotations

                                    For Each swTable In swTables

                                        Set swTableAnn = swTable

                                        Set swAnn = swTableAnn.GetAnnotation

                                        'Debug.Print swTableAnn.Title

                                        If i <> 0 Or (i = 0 And (swTableAnn.Title <> "BOM Table" And swTableAnn.Title <> "Revision Table" And swTableAnn.Title <> "TB")) Then

                                            swAnn.Visible = swAnnotationVisibilityState_e.swAnnotationHidden

                                            Debug.Print "Hide   " & swTableAnn.Title

                                        Else

                                            swAnn.Visible = swAnnotationVisibilityState_e.swAnnotationVisible

                                            Debug.Print "Show   " & swTableAnn.Title

                                        End If

                                        Debug.Print

                                    Next

                                End If

                            Next

                            swDraw.ClearSelection2 True

                            End Sub

                              • Re: Macro to Hide/Show Tables
                                Don Carter

                                FiFi Riri,

                                Thanx so much for this.  I've tested it on single sheets, multi-sheets, read-only, and it works great on all !!

                                 

                                Let me buy you a cup of coffee.  Better yet, take the rest of the day off.  You have my permission !! hahaha..

                                 

                                Would you be willing to perform an encore?  As a separate macro, would it be possible to change the name of one table that the user can select?  In my sample, you see 2 BOMs, each named differently.  Sometimes, both BOMs get named the same because they were created from the same template.  But our export process only wants one BOM, named "BOM Table".  Could a macro be used that would allow the user to pick one of the BOMs and rename it, to "Drawing BOM" is a good choice.

                                 

                                I'm thinking the process would be to run the BOM rename macro only when needed, followed by the Hide Tables macro that you just wrote.

                                 

                                Thanks again !!