Hello, I'm starting to learn API coding and for my current project I am trying to assign part colours at the component level in an assembly so as not to overwrite the original part colour. The script would be simple, simply select the component to be coloured then run the macro and it will update the colour.
From the start, how would I assign the currently selected part in the api code?
Cheers
-Robin
SolidworksApi macros
Check ModelColors macro by Leonard Kikstra
ModelColors is a nice tool to dynamically change the base color of SolidWorks part models. This macro incorporates a user customizable external data file to allow the user to add their own custom model colors to the selection buttons. This macro can also manage multiple color lists if needed. User also has option to apply color to shaded, wireframe or both.
Also check this: http://solidsmack.com/cad/multi-colored-macro-paint-rainbow-on-solidworks/
Thank you Deepak, I managed to modify that example to manipulate the colour of just the first document. See code below.
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swElement As Object
Dim vElementArr As Variant
Dim vElement As Variant
Dim vMatProp As Variant
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
vMatProp = swModel.MaterialPropertyValues
vElementArr = swModel.GetComponents(False)
Set swElement = vElementArr(0)
vMatProp(0) = 1 'Red
vMatProp(1) = 0.5 'Green
vMatProp(2) = 1 'Blue
vMatProp(3) = 1 'Ambient
vMatProp(4) = 1 'Diffuse
vMatProp(5) = 0.8 'Specular
vMatProp(6) = 0 'Shininess
vMatProp(7) = 0 'Transparency
vMatProp(8) = 0 'Emission
swElement.MaterialPropertyValues = vMatProp
Exit Sub
'Redraw to see new color
swModel.GraphicsRedraw2
End Sub
Each of the components exists in the vElementArr, how do I sense which component is selected in solidworks? Also, I couldn't find any commands that would change the Specular Spread or Reflection Amount of a part.
Any hints?
(edit)
So apparently it doesn't necessarily apply it to the first part in the assembly list. I really need to get only the selection changed.
Robin,
You need to use ISelectionManager in order to work with pre-selected objects. This should do the trick. Note that you need to have the component selected in the FeatureManager tree, however. You can't simply select the component's face in the display area.
======
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Dim vMatProps As Variant
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swComp = swSelMgr.GetSelectedObject6(1, -1)
vMatProps = swComp.GetMaterialPropertyValues2(swAllConfiguration, Nothing)
vMatProps(0) = 1
vMatProps(1) = 0
vMatProps(2) = 0
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
End Sub
====
Let me know if this helps.
Keith
Video Tutorials for the SolidWorks API
This works perfectly. Thank you!