[英]Get value of the randomly chosen color
我有一個宏 colors 零件或組件。 它隨機選擇顏色,然后將其應用於組件。 我的問題是,我想獲得隨機選擇的顏色的值,因為我想要其他子中的值,但我不知道如何獲得它。 有人可以幫我解決這個問題嗎? 這是我的代碼。
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Public Sub ColorMacro1()
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
'Get all elements
If swModel.GetType = swDocPART Then
vElementArr = swModel.GetBodies2(swAllBodies, False)
For Each vElement In vElementArr
Set swElement = vElement
Randomize
vMatProp(0) = Rnd 'Red
vMatProp(1) = Rnd 'Green
vMatProp(2) = Rnd 'Blue
vMatProp(3) = Rnd / 2 + 0.5 'Ambient
vMatProp(4) = Rnd / 2 + 0.5 'Diffuse
vMatProp(5) = Rnd 'Specular
vMatProp(6) = Rnd * 0.9 + 0.1 'Shininess
swElement.MaterialPropertyValues2 = vMatProp
Next
ElseIf swModel.GetType = swDocASSEMBLY Then
vElementArr = swModel.GetComponents(False)
For Each vElement In vElementArr
Set swElement = vElement
Randomize
vMatProp(0) = Rnd 'Red
vMatProp(1) = Rnd 'Green
vMatProp(2) = Rnd 'Blue
vMatProp(3) = Rnd / 2 + 0.5 'Ambient
vMatProp(4) = Rnd / 2 + 0.5 'Diffuse
vMatProp(5) = Rnd 'Specular
vMatProp(6) = Rnd * 0.9 + 0.1 'Shininess
swElement.MaterialPropertyValues = vMatProp
Next
ElseIf swModel.GetType = swDocDRAWING Then
MsgBox ("You can only apply random colors to part bodies or assembly components.")
Exit Sub
End If
'Redraw to see new color
swModel.GraphicsRedraw2
End Sub
請嘗試下一個方法:
vMatProp
聲明為標准模塊頂部的公共變量(在聲明區域中):Public vMatProp as Variant
按原樣運行您使用的代碼。 不要忘記從中刪除聲明,否則它不會返回錯誤,而是僅將其值用於此(現有)過程。
在任何其他Sub
中使用它,因為它是......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.