Did anyone ever find a solution to this?
Jason, see this page:
Per the above link:
To see a color change, you must:
- Specify R, G, and B, each with a value between 0.0 and 1.0, inclusive. (These values are internally multiplied by 255.0 to determine the RGB color.)
- Specify the reflectivity properties (Diffuse, Specular, Shininess (1.0 - Specular spread/Blurriness)), each with a value greater than 0.0 and less than or equal to 1.0.
- Specify Ambient, Transparency and Emission, each with a value between 0.0 and 1.0, inclusive.
- Refresh the graphics area after calling this method.
The code posted in the original post above only sets 3 of the 9 values. So you must determine appropriate values for each other parameter. Otherwise, they could default to -1 (as documented in GetMaterialPropertyValues2) if they were never set (which is not in the range 0 to 1).
The easiest thing, if you're not sure, would be just to set the other values to 0.5 and see if that suits your needs
I have been unsuccessful in implementing this method. The color just does not change. I had limited success with ModelDoc2.MaterialPropertyValues but that is only working on part files. Here's my bit of code (in vb.net), any thoughts?
Dim swModelDocext As ModelDocExtension = SwModel.Extension
Dim swSelMgr As SelectionMgr = SwModel.SelectionManager
Dim swConfigMgr As ConfigurationManager = SwModel.ConfigurationManager
Dim swConfig As Configuration = swConfigMgr.ActiveConfiguration
Dim currComponent As Component2
Dim matValues As Object
Dim matValueString As String
currComponent = swConfig.GetRootComponent3(True)
matValues = SwModel.MaterialPropertyValues
matValueString = "1, 0.752941176470588, 0.752941176470588, 1, 1, 0.5, 0.3125, 0, 0"
Dim tempString() As String = matValueString.Split(","c)
matValues(0) = Convert.ToDouble(tempString(0))
matValues(1) = Convert.ToDouble(tempString(1))
matValues(2) = Convert.ToDouble(tempString(2))
matValues(3) = Convert.ToDouble(tempString(3))
matValues(4) = Convert.ToDouble(tempString(4))
matValues(5) = Convert.ToDouble(tempString(5))
matValues(6) = Convert.ToDouble(tempString(6))
matValues(7) = Convert.ToDouble(tempString(7))
matValues(8) = Convert.ToDouble(tempString(8))
' SwModel.MaterialPropertyValues = matValues
currComponent.SetMaterialPropertyValues2(matValues, 2, Nothing)
I think 256 is invalid number?
ex. if you want to change to RGB (128,134,200)
then your code should be
dMatProps(0) = 128 / 255
dMatProps(1) = 134 / 255
dMatProps(2) = 200 / 255
I've had similar experience changing colors and getting an all black appearance, and I believe it's a bug in the API. At the end of my macro I do a save and reload, and that gets the colors to display correctly. That first save actually leaves the model preview with an all black appearance, so I do another save after the reload to get the preview to save with proper colors. (I'm on SW2016 sp4.)
I wish the save/reload option would work for me but I am trying to do this on brand new, unsaved documents
So just prompt the user for a path and file name, and then save as and reload.
Would a force rebuild be able to fix that?
That was the first thing I tried, naturally.
dMatProps(0) = 0 dMatProps(1) = 255 / 256 dMatProps(2) = 255 / 256 dMatProps(3) = Abs(dMatProps(3)) dMatProps(4) = Abs(dMatProps(4)) dMatProps(5) = Abs(dMatProps(5)) dMatProps(6) = Abs(dMatProps(6)) dMatProps(7) = Abs(dMatProps(7)) dMatProps(8) = Abs(dMatProps(8))