SW Macro / Check Layer Existence

Hi all

I'm looking for a programming synthaxis to check the existence or not of a layer.

For more details, what I want is:

- If the "foo" layer exists, then it must be hidden.

- If the "foo" layer does not exist, then it must be created.

I can already hide/show, create a macro layer but I can't check the existence or not.

Thank you in advance for your support

Cdlt

Julian

Hello

You have to search the SW APIs for help.

If you search for Layer , you'll find two very interesting examples in VBA:

  1. Layer search
  2. Creating a layer

Good code...

4 Likes

Can you show us a piece of code please =)

Dimitri

1 Like

Hello

To list the layers and hide the "foo" layer if it exists:

Sub main()
    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim swLayerMgr                  As SldWorks.LayerMgr
    Dim vLayerArr                   As Variant
    Dim vLayer                      As Variant
    Dim swLayer                     As SldWorks.Layer
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swLayerMgr = swModel.GetLayerManager
    vLayerArr = swLayerMgr.GetLayerList
    For Each vLayer In vLayerArr
        Set swLayer = swLayerMgr.GetLayer(vLayer)
        If swLayer.Name = "toto" Then
            swLayer.Visible = False
        End If
    Next
End Sub

Kind regards

2 Likes

Thank you d.roger

This is indeed the code I had arrived at by studying the examples given by remrem.

Thank you all for your contribution!