Edit: Attached a VBA macro and re-wrote the explanation.
See attached for a VBA macro that will change newly added hole wizard features to red. You will see in the commenting where I show you how to use Case statements to modify the color depending on the type of hole wizard hole. There's a tremendous number of hole types, so please look in the swWzdHoleTypes_e enumeration to find exactly what you want.
Explanation: To get the feature colors to change automatically upon feature creation then you need to use event notifications. As Artem points out below, it is not enough to use AddItemNotify, since this fires before the rebuild, but also OnIdleNotify, which fires once SolidWorks is "idle" again. The itemName argument in AddItemNotify is used to get the name of the newly added feature. Finally, IFeature::SetMaterialPropertyValues2 is used to actually change the color. Note that the first three arguments of the array in correspond to RGB (on a scale of 0-1) so you need only change them to change the color.
The disadvantage of event-triggered macros is that you already need the macro to be running. Consequently, to ensure that its always on, you can change your SolidWorks shortcut to run the macro when SolidWorks is launched. I show you how to do that in this blog post: 4 Ways to Run A Macro Without the Run Button. To see more examples of notification macros, check out our Macro Library.
Alternatively, you could just write a macro that you run manually, which searches the feature tree for hole wizard features, determines their hole wizard type, and colors them accordingly.
I tried your version and it works fine. When I try to make cases though it gets too complicated for me. I would like to have all tapped bores in red, all the others in pink. Could you please take a look at my code and tell me whats wrong? What do the 9 stand for? I thought the first 3 where enough, but maybe they aren´t? ^^
I was able to make one bore green, but unfortunately dont remember how, or maybe I am doing something wrong now.
Anyways, have a good weekend!
Private Function swApp_OnIdleNotify() As Long
Dim swFeat As SldWorks.Feature
Dim swHoleWizFeatData As SldWorks.WizardHoleFeatureData2
Dim vProps As Variant
If blnFeatAdded = True Then
Set swFeat = swPart.FeatureByName(strNewItemName)
If swFeat.GetTypeName2 = "HoleWzd" Then
Set swHoleWizFeatData = swFeat.GetDefinition
vProps = swFeat.GetMaterialPropertyValues2(swAllConfiguration, Empty)
'Modify / add to the following case statements if you want to control the colors _
for various types of hole wizard holes
Select Case swHoleWizFeatData.Type
vProps(0) = 1: vProps(1) = 0: vProps(2) = 0
vProps(3) = 1: vProps(4) = 0: vProps(5) = 0
vProps(6) = 1: vProps(7) = 0: vProps(8) = 0
vProps(0) = 0: vProps(1) = 0.752941: vProps(2) = 0.796078
'Delete this next line if you decide to use the case logic above
'vProps(0) = 1: vProps(1) = 0: vProps(2) = 0
'vProps(3) = 1: vProps(4) = 1: vProps(5) = 0.8
'vProps(6) = 0.3125: vProps(7) = 0: vProps(8) = 0
swFeat.SetMaterialPropertyValues2 vProps, swAllConfiguration, Empty
strNewItemName = Empty
blnFeatAdded = False
Are you adding a blind hole? Because that's the only type of hole that's affected by the highlighted code.
I would recommend that you put a breakpoint on IFeature::SetMaterialPropertyValues2 and see what vProps is by the time you reach that line. Look at the locals window to see the values in vProps, make sure they're correct.
Does the macro work with its original code?
I am not an expert in VB or macros. I was trying to modify your code to work with "case logic" conditions
I don't know how and where to place the breakpoint.
Can you please modify the code to work with
Case swCounterBoreThru and
Google "vba breakpoints" or "debug VBA" and I'm sure you'll come up with a lot of free resources. These are fundamental skills to VBA programming so I would encourage you to learn them if you want to delve further into macros.
I cover that material as well in the premium content at CADSharp.com. Lesson 1.9 to be specific.
Alternatively, if you want to hire me to show you how to do this over Skype, that would great as well. I can create these modifications for you live. Unfortunately they aren't so trivial that I have time to do it for free.
I subscribe to Keith's website and I can vouch for his ability. Well done website and very much knows his stuff!
I can second that
I'm afraid the AddItemNotify is not enough in this case. At this moment feature is not completely created (this is not post notification). Changing the feature in this state may be risky. For example on my end it changes the whole body because separated entities are not yet generated for feature.
It is better to use some post notification.
Furthermore I would not suggest to use VBA macros because you need to have some User Form or something like that to keep them 'alive' after it ends. The best choice is to use add-in however the VSTA macro should also work because you can toggle the SolidWorks settings to not close the debugger when macro ends (added to macro directly).
You also need to keep track of the documents change and attach to active document every time.
I have implemented this in C# VSTA macro, please find this in the attachment.
Try to run it and to create a "Through all counterbore" in hole wizard and it will be colored to red. You may change the list of colors and features in the block in 'main' procedure:
//TODO: add your types here
You may also change the features itself's slightly modifying the macro. but I would finally suggest to switch to add-in and forget about running the macro at all because you can run the add-in on startup automatically,
Hope it helps.
Application Engineer at Intercad
Tel: +61 2 9454 4444
ColorFeaturesAfterCreate.zip 12.6 K
Thanks for those insights, Artem. It's too bad that AddItemNotify isn't a post-event notification. What is interesting is that in my testing, RegenPostNotifty2 actually fires before AddItemNotify. Doesn't make much sense.
I completely understand the elegance of addins but here are some points I want to make:
1. A class module can be used in place of a user form, and these are very easy to implement.
2. Macros can run on startup as well as long as the user is launching from a shortcut. An addin seems like overkill just to make up for that small lack of flexibility.
Nevertheless if you think I am overlooking something, please share
Yes, you are right about the macros. Just checked and the behaviour is changed in new versions of SolidWorks. Previously the macro terminates once the main function ends so it was only possible to keep it run via DoEvents in infinite loop or dummy form display. Now it seems to be controlled by 'Stop VSTA Debugger on macro exit' toggle despite it says that this is only for VSTA.
Anyways both VBA and VSTA macros may be run via /m switch. Anyways all it look like a workaround because this toggle will 'hang' all macros run on this session in memory. Sure it is not a case if no macros are usually run by user.
Application Engineer at Intercad
Tel: +61 2 9454 4444
Thanks very much guys. I will look at both solutions today.
Artem .. I'm not sure how to activate your solution?
Also ... I do have another thought .... TAS has helped me out with a custom properties macro that traverses all of the parts in my assembly and assigns custom properties based on inputs from a dialog box.
Would it be a better idea to have the macro also look at each of the hole wizard features, and assign the hole color based on this while it is already traversing the assembly for the custom properties?
Please share your thoughts.
The solution I have attached is VSTA macro - just open it in edit mode in VSTA Editor (Tools->Macro->Edit. Specify the filter for VSTA macros) and run it.
If you like to color all existing features than it makes sense to combine the macros. Sample I have created monitors the hole wizard add notification and colors it in 'real' time.
Application Engineer at Intercad
Tel: +61 2 9454 4444
Excellent. I can see that it works. I have only ever seen the VBA macros. The VSTA has a very clean interface.
I love that this happens upon creation. Just makes sense. Not sure why Solidworks doesn't already include this as an option.
Looking thru the Hole Wizard features ... I don't see what the "swWzdHoleTypes_e" would be for the new hole "dowel" feature.
Also is there a way to specify RGB value for the assigned colors?
Ideally, I would use the middle row of the Solidworks feature pallette, but not sure how to specify this.
IE. regardless of size, and depth, and based only by feature name
All dowel holes = Red
All tapped holes = Orange
All screw clearance holes = Green
All Counterbored holes = Blue
All Countersunk holes = Yellow
Does this make sense?
Thanks for your help with this. I appreciate it
Found this in another post from Keith. That takes care of my RGB question. Thanks Keith.
vProps(0) = 0
vProps(1) = 1
vProps(2) = 0
If you would prefer to use a 0-255 scale then you will need to do some math. For example, if you know you need RGB colors = 182, 4, 67 then you would need to do this:
vProps(0) = 182/255
vProps(1) = 4/255
vProps(2) = 67/255
Since Solidworks API does not allow for Hole Colors based on Feature name. ie .. dowel holes = red
is there a way to set hole color based on hole favorites? Where the hole color is assigned based on the Hole favorite name?
edit: I got it to open with "SW Visual studio tools for applications". But how do I run it?^^
I am having trouble opening this macro as well. When I click Tools->Macro->Edit there are two specifuv filters for VSTA macros: SW VSTA VB MACRO (*.vbproj) and SW VSTA C# MACRO (*.csproj). But both don´t seem to include your macro.
Any idea why this doesnt work or any suggestion for a workaround?
Please find the 'csproj' file in the zip (may be in sub folder). You need to unzip first. Your steps are correct. Please let me know if you cannot find it.
Thanks for your quick reply Artem! :-)
I found the csproj, when I try to edit it says something like: "ColorFeaturesAfterCreate.csproj.user is being copied to ColorFeaturesAfterCreate.csproj.swp".
Then I click ok, and up comes: "Couldnt open Macro file U:\...\ColorFeaturesAfterCreate.csproj.user"
Looks like something wrong with your installation of VSTA. You may need to repair it. Now you can use Keith's VBA version.
This post has been quite helpful as I am trying to manage hole wizard face colors as well. I have utilized the information provided and have the following:
A macro with a listener that sets the color of the hole according to the hole type on wizard create. (Thanks to this forum)
An Addin which launches the macro on Solidworks launch. (Thanks to several forums including this one)
Everything is great except I need the macro to update the colors on edit. I have read in this forum there was some challenge in this. What is the final verdict on feature change / edit and the colors update?
In reviewing options it seems there is the possibility to detect a part rebuild and run the macro on all features at that point. This is more intriging because it would take care of even older holes created without the macro available at the time. If someone could point me in the correct direction I would greatly appreciate it.
Once I am done I can post all of my code here for the world to use. At this time it is incomplete due to the lack of ability to keep up with feature edits. Not that my code is new but all of it brought together in one place could be of help to some.
Message was edited by: Danny Bradford