In other words run whatever code this button does.
Everything I can find is changing the actual color of the components not the component colors at the assembly level.
In other words run whatever code this button does.
Everything I can find is changing the actual color of the components not the component colors at the assembly level.
Take a look at RemoveMaterialProperty2 method.
You will find a VBA example at the end of the documentation.
That works for removing the color from a selected component. I want to remove all component colors driven at the assembly level.
Sub main() Dim app As SldWorks.SldWorks Dim doc As SldWorks.ModelDoc2 Dim asm As SldWorks.AssemblyDoc Dim vcpns As Variant Dim vcpn As Variant Dim cpn As SldWorks.Component2 Set app = Application.SldWorks Set doc = app.ActiveDoc If doc.GetType <> SwConst.swDocumentTypes_e.swDocASSEMBLY Then Exit Sub Set asm = doc vcpns = asm.GetComponents(False) If IsEmpty(vcpns) Then Exit Sub For Each vcpn In vcpns Set cpn = vcpn cpn.RemoveMaterialProperty2 SwConst.swInConfigurationOpts_e.swAllConfiguration, Nothing Next vcpn doc.GraphicsRedraw2 End Sub
I'm using this macro to add color at the assembly along with some other properties. Your code seems to work fine if i change the components and run your code but it doesn't remove the color my macro is adding.
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Dim vMatProps As Variant
Dim swMgr As CustomPropertyManager
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swComp = swSelMgr.GetSelectedObject6(1, -1)
Set swMgr = swModel.Extension.CustomPropertyManager("")
vMatProps = swComp.GetMaterialPropertyValues2(swAllConfiguration, Nothing)
vMatProps(0) = 1
vMatProps(1) = 1
vMatProps(2) = 121
vMatProps(3) = 1
vMatProps(4) = 1
vMatProps(5) = 0.8
vMatProps(6) = 0.3125
vMatProps(7) = 0
vMatProps(8) = 0
swComp.SetMaterialPropertyValues2 vMatProps, swAllConfiguration, Nothing
swModel.GraphicsRedraw2
swMgr.Set "Finish", ("CLEAR ZINC")
swMgr.Set "Finish1", ("0.0005 MIN SC3")
swMgr.Set "Finish2", ("ASTM B-633-07")
swMgr.Set "Finish Code", ("ZN")
End Sub
I have no problem with this code:
Function material() As Double() Dim arr(8) As Double arr(0) = 1 arr(1) = 1 arr(2) = 1 arr(3) = 1 arr(4) = 1 arr(5) = 0.8 arr(6) = 0.3125 arr(7) = 0 arr(8) = 0 material = arr End Function Sub setColor() Dim app As SldWorks.SldWorks Dim doc As SldWorks.ModelDoc2 Dim asm As SldWorks.AssemblyDoc Dim vcpns As Variant Dim vcpn As Variant Dim cpn As SldWorks.Component2 Set app = Application.SldWorks Set doc = app.ActiveDoc If doc.GetType <> SwConst.swDocumentTypes_e.swDocASSEMBLY Then Exit Sub Set asm = doc vcpns = asm.GetComponents(False) If IsEmpty(vcpns) Then Exit Sub For Each vcpn In vcpns Set cpn = vcpn cpn.SetMaterialPropertyValues2 material, SwConst.swInConfigurationOpts_e.swAllConfiguration, Nothing Next vcpn doc.GraphicsRedraw2 End Sub Sub unsetColor() Dim app As SldWorks.SldWorks Dim doc As SldWorks.ModelDoc2 Dim asm As SldWorks.AssemblyDoc Dim vcpns As Variant Dim vcpn As Variant Dim cpn As SldWorks.Component2 Set app = Application.SldWorks Set doc = app.ActiveDoc If doc.GetType <> SwConst.swDocumentTypes_e.swDocASSEMBLY Then Exit Sub Set asm = doc vcpns = asm.GetComponents(False) If IsEmpty(vcpns) Then Exit Sub For Each vcpn In vcpns Set cpn = vcpn cpn.RemoveMaterialProperty2 SwConst.swInConfigurationOpts_e.swAllConfiguration, Nothing Next vcpn doc.GraphicsRedraw2 End Sub
Sub setColorRootCpn() Dim app As SldWorks.SldWorks Dim doc As SldWorks.ModelDoc2 Dim cpn As SldWorks.Component2 Set app = Application.SldWorks Set doc = app.ActiveDoc If doc.GetType <> SwConst.swDocumentTypes_e.swDocASSEMBLY Then Exit Sub Set cpn = doc.ConfigurationManager.ActiveConfiguration.GetRootComponent3(True) cpn.SetMaterialPropertyValues2 material, SwConst.swInConfigurationOpts_e.swAllConfiguration, Nothing doc.GraphicsRedraw2 End Sub Sub unsetColorRootCpn() Dim app As SldWorks.SldWorks Dim doc As SldWorks.ModelDoc2 Dim cpn As SldWorks.Component2 Set app = Application.SldWorks Set doc = app.ActiveDoc If doc.GetType <> SwConst.swDocumentTypes_e.swDocASSEMBLY Then Exit Sub Set cpn = doc.ConfigurationManager.ActiveConfiguration.GetRootComponent3(True) cpn.RemoveMaterialProperty2 SwConst.swInConfigurationOpts_e.swAllConfiguration, Nothing doc.GraphicsRedraw2 End Sub Function material() As Double() Dim arr(8) As Double arr(0) = 1 arr(1) = 1 arr(2) = 1 arr(3) = 1 arr(4) = 1 arr(5) = 0.8 arr(6) = 0.3125 arr(7) = 0 arr(8) = 0 material = arr End Function
You're welcome, to not confuse talk about "root component".
Just a warning on your vMatProps(2) value equals to 121!
Alexandre,
I've noticed that when I run this macro sometimes it removes the color like I want it too other times it turns the assembly white. Any ideas?
I suppose it depending where you place your text cursor before to click "Run". If your cursor is in the scope of "setColorRootCpn" Sub, it will lauch "setColorRootCpn" Sub. If your cursor is in the scope of "unsetColorRootCpn", it will launch "unsetColorRootCpn" Sub. If your cursor is outside both, VBA will ask you which Sub you want to launch.