Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Macro for layer color change? 2

Status
Not open for further replies.

pdybeck

Mechanical
May 14, 2003
599
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
 
Replies continue below

Recommended for you

Are the layer names whose color needs changing the same for every drawing?
 
Yes, they are the same. For every drawing they are the same and have been since we implemented SolidWorks here. That makes it easier... I would think.
 
Here you go. To find the number corresponding to the color you want, set the value of the constant "COLORQUERY" to true, set the active layer to the color you want to find the number for, and run the macro. After you know all the color numbers, change "COLORQUERY" back to false and copy/paste the block at the end as many times as you have layers to change. Change layername to the names of the layers you want to change (leaving the quotation marks) and change the number 255 to whatever color you got from the color query.

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
 
Thanks handleman,

I'll give it a try and let you know. Much appreciated.

Pete Yodis
 
Works great handleman. Many Thanks.
 
Handleman,

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.
 
Are you all on the same version of SW? I'm running 2006 myself. SW adds functionality to the API with each release, so if they're using an older version there may be some function call that is not supported.

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?
 
It works for me using SolidWorks 2006 and 2007. The macro is located and run from a network drive. The other users don't get an error message, the layer colors just don't change. The other users are running SolidWorks 2006. The macro is run from the same location that I run it from, a network drive...

Pete
 
I had something similar before. Make sure the other systems have the correct "Type Library" set.
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?

[cheers]
Helpful SW websites faq559-520​
How to find answers ... faq559-1091​
SW2006-SP5 Basic ... No PDM​
 
CBL,

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor