Macro for layer color change?
Macro for layer color change?
(OP)
Does anyone have code, or could supply code, or point me in the right direction for code, which would change layer colors for the current drawing opened in SolidWorks. I desire to create a macro to do this. I do not have VB experience and have found that the macro recorder in SolidWorks does not record any actions in the layer properties dialogue box (I think if it did I could muddle through and not have to ask this favor). Basically, we have changed what colors we want to use for layers on our drawings. I would like to create a macro so that as people open up older drawings they can hit the macro and it automatically sets/changes roughly 5 or six layer colors. The layers would not need to be created, as they would already exist on the drawing, just the colors would change. Any help would be appreciated.
Pete Yodis
Pete Yodis






RE: Macro for layer color change?
RE: Macro for layer color change?
RE: Macro for layer color change?
Enjoy!
CODE
Const COLORQUERY As Boolean = False
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim swDwg As SldWorks.DrawingDoc
Dim swLyrMgr As SldWorks.LayerMgr
Dim swLayer As SldWorks.Layer
Sub LayerColorChange()
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
If swDocDRAWING <> swDoc.GetType Then
MsgBox "This only works for drawings"
Exit Sub
End If
Set swDwg = swDoc
Set swLyrMgr = swDoc.GetLayerManager
If COLORQUERY Then
Set swLayer = swLyrMgr.GetLayer(swLyrMgr.GetCurrentLayer)
MsgBox "The color of layer " & swLayer.Name & " is " & swLayer.Color
Exit Sub
End If
''''''''''''
'copy and paste this block until you have as many as you want
Set swLayer = swLyrMgr.GetLayer("layername") 'Type the layer name here. Leave the quotes.
swLayer.Color = 255 'Type the number for the desired color here.
''''''''''''
End Sub
RE: Macro for layer color change?
I'll give it a try and let you know. Much appreciated.
Pete Yodis
RE: Macro for layer color change?
RE: Macro for layer color change?
Any idea why this macro would work on my local pc, but not on any other coworkers of mine? Can't seem to figure it out. The macro is sitting in a network folder that all of us have access to.
RE: Macro for layer color change?
What sort of error message do your other users get?
Also, just to confirm... you say the macro works on your "local pc". Does that mean it works when you have it on your hard drive, or it works whether it's on your hard drive or the network folder?
RE: Macro for layer color change?
Pete
RE: Macro for layer color change?
Edit Macro > Tools > References and make sure the Solidworks 2006 Constant Type Library is loaded.
Handleman ... can you confirm if this and/or others may be needed?
RE: Macro for layer color change?
That was the trick. I actually created the macro while working in SolidWorks 2007. The other users have not had 2007 installed yet (I'm still testing it before it gets rolled out), and therefore do not have the 2007 constant type library installed. I simply re-created the macro while in SolidWorks 2006 and saved and it then used the 2006 constant type library and the other users were then able to use the macro. Thanks CBL.
Pete Yodis