×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Macro for layer color change?
2

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

RE: Macro for layer color change?

Are the layer names whose color needs changing the same for every drawing?

RE: Macro for layer color change?

(OP)
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.

RE: Macro for layer color change?

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

RE: Macro for layer color change?

(OP)
Thanks handleman,

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

Pete Yodis

RE: Macro for layer color change?

(OP)
Works great handleman.  Many Thanks.

RE: Macro for layer color change?

(OP)
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.

RE: Macro for layer color change?

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?

RE: Macro for layer color change?

(OP)
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

RE: Macro for layer color change?

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

RE: Macro for layer color change?

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources