Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations pierreick on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Layers macro 1

Status
Not open for further replies.

ctopher

Mechanical
Jan 9, 2003
17,485
Anyone have a macro that will delete unused layers on a drawing? I'm not into writing macros, but this one would be useful.

ctopher, CSWP
SolidWorks '19
ctophers home
SolidWorks Legion
 
Replies continue below

Recommended for you

Try this:

Code:
Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swLayerMgr As SldWorks.LayerMgr
    Dim vLayers As Variant
    Dim vLayer As Variant
    Dim swLayer As SldWorks.Layer
    Dim vItems As Variant
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swLayerMgr = swModel.GetLayerManager
    vLayers = swLayerMgr.GetLayerList
    For Each vLayer In vLayers
        Set swLayer = swLayerMgr.GetLayer(vLayer)
        vItems = swLayer.GetItems(swLayerItemsOption_e.swLayerItemsOption_Annotations + _
                                swLayerItemsOption_e.swLayerItemsOption_SketchBlockInstance + _
                                swLayerItemsOption_e.swLayerItemsOption_SketchPoint + _
                                swLayerItemsOption_e.swLayerItemsOption_SketchSegments)
        If IsEmpty(vItems) Then swLayerMgr.DeleteLayer (vLayer)
    Next
End Sub
 
Maybe I didn't understand your requirement correctly.
When you say "unused layers", I assumed it was "layers that don't have any items on them".
Is it "layers that are hidden" instead? Something else?

I also don't quite understand the problem with: "but also turns on items on layers".
 
I have some layers turned off. They have blocks and other items used in those layers. They are turned on when needed.
I don't want to delete those layers, I only want to delete layers are not used. For example, when I import an ACAD dwg and make a dwg, it brings a lot of layers. I only want to use the ones needed and delete the others.

ctopher, CSWP
SolidWorks '19
ctophers home
SolidWorks Legion
 
Thanks, it did.
But, like I said, if I have something on layers that are turned off, the macro turns them on and then deletes those layers.

ctopher, CSWP
SolidWorks '19
ctophers home
SolidWorks Legion
 
Ok.
I don't see it on my test file, maybe because it happens too quickly.
It might come from the GetItems function, but I don't see any other way to check if the layer is empty.
 
Does it matter if they briefly turn on? Since they will be turn off when deleted anyway?
 
Yes. I have blocks associated with layers that turn off or on depending the type of dwg. i.e., ESD symbol is on ESD layer, etc.
The macro turns it on (showing the block) if it's off, then deletes the layer. I want to keep the layer. I only want to delete layers that have nothing associated with them.

ctopher, CSWP
SolidWorks '19
ctophers home
SolidWorks Legion
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor