6 Replies Latest reply on Oct 9, 2018 4:15 PM by Matt Peneguy

    Macro for hiding dimensions

    Joel McDevitt

      Hi there,

      I am needing some help developing a macro to hide the dimensions on drawings as well as pull up the print command to print to PDF.

      I can get the print to pull up but I can not get the dimensions to become hidden. Here is my code :

       

      Dim swApp As Object

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

      Sub main()

      Dim Toggle As Boolean

      Set swApp = Application.SldWorks

      Set Part = swApp.ActiveDoc

      Toggle = Part.Extension.SelectByID2("Annotations", "Details", 0, 0, 0, 0, 0, Nothing, 0)

      Set swApp = Nothing

      Set swDoc = Nothing

      Part.PrintDirect

      End Sub

       

      I used the record function and went through Annotations, Details, and unchecked the "Reference Dimensions" check box.

       

       

      Any help would be greatly appreciated.

      Thank you.

        • Re: Macro for hiding dimensions
          Nilesh Patel

          Have a look at IModelDoc2::HideDimension method.

          • Re: Macro for hiding dimensions
            Joel McDevitt

            How would I implement this in the above code?

            • Re: Macro for hiding dimensions
              Alex Burnett

              It's not pretty but see if this cobbled together code does what you need.

              Dim swApp As SldWorks.SldWorks
              Dim swModel As SldWorks.ModelDoc2
              Dim swDraw As DrawingDoc
              Dim swView As SldWorks.View
              Dim lerr As Long
              Dim bstatus As Boolean
              Dim allSheetViewArrays As Variant
              Dim sheetViews As Variant
              Dim swDispDim As DisplayDimension
              Dim swAnno As Annotation
              
              
              Sub main()
              
              
                  Set swApp = Application.SldWorks
                  Set swModel = swApp.ActiveDoc
                  
                  If swModel.GetType <> swDocDRAWING Then
                      Msg = "Only Allowed on Drawings" ' Define message
                      Style = vbOKOnly ' OK Button only
                      Title = "Error" ' Define title
                      Call MsgBox(Msg, Style, Title) ' Display error message
                      Exit Sub ' Exit this program
                  End If
                      
                  Set swDraw = swModel
                  allSheetViewArrays = swDraw.GetViews
                  For i = 0 To UBound(allSheetViewArrays)
                      sheetViews = allSheetViewArrays(i)
                      For j = 0 To UBound(sheetViews)
                          Set swView = sheetViews(j)
                          
                              Set swAnno = swView.GetFirstAnnotation
                              Do While Not swAnno Is Nothing
                                  If swAnno.GetType = SwConst.swDisplayDimension Then
                                      Set swDispDim = swAnno.GetSpecificAnnotation
                                      Debug.Print swDispDim.GetNameForSelection
                                      swAnno.Select3 True, Nothing
                                  End If
                                  Set swAnno = swAnno.GetNext
                              Loop
                          
                          Set swView = swView.GetNextView
                      Next j
                      
                  Next i
                  swModel.HideDimension
              End Sub
              
                • Re: Macro for hiding dimensions
                  Joel McDevitt

                  That worked perfectly. Thank you very much.

                    • Re: Macro for hiding dimensions
                      Alex Burnett

                      Glad it worked, unhiding them is another story. I've got it mostly working but for whatever reason it isn't picking up the last dimension on each page when I run it. Perhaps this is a glitch with the API but I'll post that below too.

                       

                      Dim swApp As SldWorks.SldWorks
                      Dim swModel As SldWorks.ModelDoc2
                      Dim swDraw As DrawingDoc
                      Dim swView As SldWorks.View
                      Dim lerr As Long
                      Dim bstatus As Boolean
                      Dim allSheetViewArrays As Variant
                      Dim sheetViews As Variant
                      Dim swDispDim As DisplayDimension
                      Dim swAnno As Annotation
                      
                      
                      Sub main()
                      
                      
                          Set swApp = Application.SldWorks
                          Set swModel = swApp.ActiveDoc
                          
                          If swModel.GetType <> swDocDRAWING Then
                              Msg = "Only Allowed on Drawings" ' Define message
                              Style = vbOKOnly ' OK Button only
                              Title = "Error" ' Define title
                              Call MsgBox(Msg, Style, Title) ' Display error message
                              Exit Sub ' Exit this program
                          End If
                              
                          Set swDraw = swModel
                          swDraw.HideShowDimensions
                          allSheetViewArrays = swDraw.GetViews
                          For i = 0 To UBound(allSheetViewArrays)
                              sheetViews = allSheetViewArrays(i)
                              For j = 0 To UBound(sheetViews)
                                  Set swView = sheetViews(j)
                                  
                                      Set swAnno = swView.GetFirstAnnotation2
                                      swAnno.Select3 True, Nothing
                                      Do While Not swAnno Is Nothing
                                          If swAnno.GetType = swDisplayDimension Then
                                              Set swDispDim = swAnno.GetSpecificAnnotation
                                              swAnno.Select3 True, Nothing
                                              Debug.Print swDispDim.GetNameForSelection
                                              swAnno.Visible = swAnnotationVisibilityState_e.swAnnotationHidden
                                              'swAnno.Visible = swAnnotationVisibilityState_e.swAnnotationVisible
                                          End If
                                          Set swAnno = swAnno.GetNext
                                      Loop
                                  Set swView = swView.GetNextView
                              Next j
                          Next i
                          swModel.SetPickMode
                      End Sub
                      
                      
                  • Re: Macro for hiding dimensions
                    Matt Peneguy

                    Joel McDevitt,

                    For new projects, I'd suggest changing the layer for the dimensions to a new layer you create called "Dimensions".  Then when you want you can just hide the "Dimension" layer.  If you save this into your document template, it'll be available for all new drawings.