6 Replies Latest reply on Sep 9, 2014 8:05 AM by Peter Larsen

    Macro - toggle both layer + document setting on/off

    Peter Larsen

      Hello everyone,

       

      I am trying to create a macro that toggles 2 things at the same time on a SW drawing:

      1. Turn on/off a specific layer
      2. Turn on/off the document setting shown below.

       

      I found a macro here on the forum that toggles the layer (shown below) but how do I add the on/off toggle of the document setting?

      I am not familar with macros but hoping to learn more about later on.

       

      Thank you in advance.

       

      Regards

      Peter

       

       

      Dim swApp As Object

      Sub main()

      Dim swApp                       As SldWorks.SldWorks

          Dim swModel                     As SldWorks.ModelDoc2

          Dim swLayerMgr                  As SldWorks.LayerMgr

          Dim swLayer                     As SldWorks.Layer

       

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swLayerMgr = swModel.GetLayerManager

          Set swLayer = swLayerMgr.GetLayer("Variant_Controlled")

        

          If swLayer.Visible = False Then

              ' Toggle layer on

              swLayer.Visible = True

              Debug.Assert True = swLayer.Visible

          Else

              ' Toggle layer off

              swLayer.Visible = False

              Debug.Assert False = swLayer.Visible

          End If

      Set swApp = Application.SldWorks

      End Sub

        • Re: Macro - toggle both layer + document setting on/off
          Deepak Gupta

          Peter use these codes for toggling that BOM Table option:

           

          Dim swApp           As SldWorks.SldWorks

          Dim Part            As SldWorks.ModelDoc2

          Dim bRet            As Boolean

           

          Sub main()

          Set swApp = Application.SldWorks

          Set Part = swApp.ActiveDoc

           

          bRet = Part.Extension.GetUserPreferenceToggle(swUserPreferenceToggle_e.swBomTableDontAddQTYNextToConfigName, 0) 'Get the option

          If bRet = True Then

          Part.Extension.SetUserPreferenceToggle swUserPreferenceToggle_e.swBomTableDontAddQTYNextToConfigName, 0, False 'Uncheck the option

          Else

          Part.Extension.SetUserPreferenceToggle swUserPreferenceToggle_e.swBomTableDontAddQTYNextToConfigName, 0, True 'Check the option

          End If

          End Sub

            • Re: Macro - toggle both layer + document setting on/off
              Peter Larsen

              Thank you very much for the quick reply, Deepak.

               

              When I run the macro it adds the check mark in the doc.setting but when I run it again it does not remove the check mark. What could be wrong?

               

              And is it possible to combine it with this macro that turns on/off a layer:

               

              Dim swApp As Object

              Sub main()

              Dim swApp                       As SldWorks.SldWorks

                  Dim swModel                     As SldWorks.ModelDoc2

                  Dim swLayerMgr                  As SldWorks.LayerMgr

                  Dim swLayer                     As SldWorks.Layer

               

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                  Set swLayerMgr = swModel.GetLayerManager

                  Set swLayer = swLayerMgr.GetLayer("Variant_Controlled")

                

                  If swLayer.Visible = False Then

                      ' Toggle layer on

                      swLayer.Visible = True

                      Debug.Assert True = swLayer.Visible

                  Else

                      ' Toggle layer off

                      swLayer.Visible = False

                      Debug.Assert False = swLayer.Visible

                  End If

              Set swApp = Application.SldWorks

              End Sub

                • Re: Macro - toggle both layer + document setting on/off
                  Deepak Gupta

                  Hi Peter, not sure why they didn't worked (was working for me when I tested but now they are also not working). So I swapped the condition and this seems to be working. Try the combined codes below:

                   

                  Dim swApp                       As SldWorks.SldWorks

                  Dim swModel                     As SldWorks.ModelDoc2

                  Dim swLayerMgr                  As SldWorks.LayerMgr

                  Dim swLayer                     As SldWorks.Layer

                  Dim bRet                        As Boolean

                  Sub main()

                   

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                   

                  bRet = swModel.Extension.GetUserPreferenceToggle(swUserPreferenceToggle_e.swBomTableDontAddQTYNextToConfigName, 0) 'Get the option

                  If bRet = False Then

                  swModel.Extension.SetUserPreferenceToggle swUserPreferenceToggle_e.swBomTableDontAddQTYNextToConfigName, 0, True 'Uncheck the option

                  Else

                  swModel.Extension.SetUserPreferenceToggle swUserPreferenceToggle_e.swBomTableDontAddQTYNextToConfigName, 0, False 'Check the option

                  End If

                   

                  Set swLayerMgr = swModel.GetLayerManager

                      Set swLayer = swLayerMgr.GetLayer("Layer1") 'Change Layer Name Here

                   

                      If swLayer.Visible = False Then

                          ' Toggle layer on

                          swLayer.Visible = True

                          Debug.Assert True = swLayer.Visible

                      Else

                          ' Toggle layer off

                          swLayer.Visible = False

                          Debug.Assert False = swLayer.Visible

                      End If

                   

                  End Sub