Can somebody fix my Macro

first of all, thanks to Josh Brady and Jacob Cordingley who tried to help me whit this

this is the latest attempt, visual basic doesn't find anything wrong but the macro isn't doing a thing on my drawings.

I put some test sheets and if somebody can fix my macro (solidworks 2007) I'd be real happy

as a reminder, want I want to do is to meke a layer (PROTO) visible/invisible to all opened drawing files in solidworks.

I do have a macro that do it for active file and I'm sure we can do it for the opened ones.

thanks a lot....

  Dim swApp As Object
'------------------------------------
' Preconditions:
'          (1) Drawing document is open.
'          (2) Drawing document contains a layer named PROTO.
'

' Postconditions:
'          If PROTO layer is visible, then it becomes not visible.
'             - or -
'          If PROTO layer is not visible, then it becomes visible.
'
'------------------------------------------------------------------
Option Explicit

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.GetFirstDocument
    While Not swModel Is Nothing

     ' check if file is drawing
    If (swModel.GetType = swDocumentTypes_e.swDocDRAWING) Then
   
         Set swLayerMgr = swModel.GetLayerManager
         Set swLayer = swLayerMgr.GetLayer("PROTO")
        
        
        
    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 If
        
         Set swModel = swModel.GetNext
    Wend
End Sub

SolidworksApi macros
Answers 5
SF
SF 2010-03-09 Best Answer

I did not test your macro since I do not have 2007 anymore, but looking at your macro, you have swApp declared twice. Delete the "Dim swApp As Object" at the top of your macro.

It could blowup if a drawing does not have a layer called "PROTO". Do all of these test drawings have this layer?

SF
SF 2010-03-09

Yes they all have it.  you can test it in 2010 or erlier version I also have a 2010 version...

I copy you the original macro to.

thanks for your reply

Dim swApp As Object
'------------------------------------
' Preconditions:
'          (1) Drawing document is open.
'          (2) Drawing document contains a layer named PROTO.
'

' Postconditions:
'          If PROTO layer is visible, then it becomes not visible.
'             - or -
'          If PROTO layer is not visible, then it becomes visible.
'
'------------------------------------------------------------------
Option Explicit

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("PROTO")

      

    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

JC
JC 2010-03-09

It look like you went away from all opend drawings in your marco

also you still have the swAPP declared twice  like stated above by you should fix it

FR
FR 2010-03-09

Frederic ;

I've load your drawings and execute your macro without change (on SolidWorks 2008 SP5) and it seems to work for me.

I didn't read your previous topics but I've seen nothing that can make your macro fail.

Just find a little error : the first line : "Dim swApp As Object" must be suppress because an another one exist : "Dim swApp As SldWorks.SldWorks"

I re-work the code to display some informations and help you to find what fails :

<p>'------------------------------------<br>' Preconditions:<br>'          (1) Drawing document is open.<br>'          (2) Drawing document contains a layer named PROTO.<br>'<br><br>' Postconditions:<br>'          If PROTO layer is visible, then it becomes not visible.<br>'             - or -<br>'          If PROTO layer is not visible, then it becomes visible.<br>'<br>'------------------------------------------------------------------<br>Option Explicit<br><br>Sub main()<br>    Dim swApp As SldWorks.SldWorks<br>    Dim swModel As SldWorks.ModelDoc2<br>    Dim swLayerMgr As SldWorks.LayerMgr<br>    Dim swLayer As SldWorks.Layer<br><br><br>    Set swApp = Application.SldWorks<br>    Set swModel = swApp.GetFirstDocument<br>    While Not swModel Is Nothing<br><br>        Debug.Print swModel.GetPathName<br><br>        ' check if file is drawing<br>        If (swModel.GetType = swDocumentTypes_e.swDocDRAWING) Then<br><br>            Set swLayerMgr = swModel.GetLayerManager<br>            Set swLayer = swLayerMgr.GetLayer("PROTO")<br><br>            If Not swLayer Is Nothing Then<br>                If swLayer.Visible = False Then<br>                    ' Toggle layer on<br>                    Debug.Print "Hides the layer ""PROTO"""<br>                    swLayer.Visible = True<br>                    Debug.Assert True = swLayer.Visible<br><br>                Else<br>                    Debug.Print "Shows the layer ""PROTO"""<br>                    ' Toggle layer off<br>                    swLayer.Visible = False<br>                    Debug.Assert False = swLayer.Visible<br><br>                End If<br>            Else<br>                MsgBox "Unable to get the layer ""PROTO"" in : " & vbCrLf & swModel.GetPathName<br>            End If<br>        End If<br><br>        Set swModel = swModel.GetNext<br>    Wend<br>End Sub<br></p>

Try to use this one and see the output in execution window.

I added a test on the exisiting layer proto.

Note : I'm available by mail/phone if you want (I'm french).

SF
SF 2010-03-09

Thank you so much, it now works perfectly

Good job

Salutations...