以下代码将随机颜色应用于装配中的所有零部件,而与每个零部件的名称相同或不同无关。装配件可能具有包装在一起的相同名称的零部件。我只想在tempComp.DisplayName
相同时应用相同的颜色。我不知道如何在循环运行期间检查名称或以某种方式存储名称并进行比较。
任何帮助将不胜感激。
Dim i as integer = 0
For Each tempComp As Assemblies.Component In myAsmInfo.AllComponents
lw.WriteLine(tempComp.DisplayName & " | Color ID:" & i)
''' Select random color between 1 to 216
i = CInt(Math.Ceiling(Rnd() * 216)) + 1
Dim markId1 As NXOpen.Session.UndoMarkId = Nothing
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Edit Object Display")
Dim displayModification1 As NXOpen.DisplayModification = Nothing
displayModification1 = theSession.DisplayManager.NewDisplayModification()
displayModification1.ApplyToAllFaces = True
displayModification1.ApplyToOwningParts = False
'lw.WriteLine("Color Before " & i)
displayModification1.NewColor = i
Dim objects1(0) As NXOpen.DisplayableObject
objects1(0) = tempComp
displayModification1.Apply(objects1)
Dim nErrs1 As Integer = Nothing
nErrs1 = theSession.UpdateManager.DoUpdate(markId1)
' lw.WriteLine("Color After " & i)
displayModification1.Dispose()
Next
答案 0 :(得分:1)
鉴于您需要在名称旁边存储tempComp.DisplayName
和一个数字值,因此需要使用Dictionary (Of String, Integer)
来保存值。
在开始For
循环之前,添加以下行:
Dim coloursDictionary As New Dictionary(Of String, Integer)
而且,在For
循环中,当确定要分配的颜色时,使用:
If coloursDictionary.ContainsKey(tempComp.DisplayName) Then
i = coloursDictionary(tempComp.DisplayName)
Else
i = CInt(Math.Ceiling(Rnd() * 216)) + 1
coloursDictionary(tempComp.DisplayName) = i
End If