@johnsonshiue i missed the part about the partnumber but here is the code based on the part number.
Dim topAsm As AssemblyDocument = ThisDoc.Document
Dim trans As Transaction = ThisApplication.TransactionManager.StartTransaction(topAsm, "Unique Colors")
Dim ucRep As DesignViewRepresentation
Try
ucRep = topAsm.ComponentDefinition.RepresentationsManager.DesignViewRepresentations("Unique Colors")
Catch ex As Exception
ucRep = topAsm.ComponentDefinition.RepresentationsManager.DesignViewRepresentations.Add("Unique Colors")
End Try
ucRep.Activate()
Dim knowOcc As Dictionary(Of String, Asset) = New Dictionary(Of String, Asset)
Dim compOcc As ComponentOccurrence
For Each compOcc In topAsm.ComponentDefinition.Occurrences
Dim occDoc As Document = compOcc.Definition.Document
Dim occPropSet As PropertySet = occDoc.PropertySets.Item("Design Tracking Properties")
Dim propert As [Property] = occPropSet.Item("Part Number")
Dim occName As String = propert.Value
If (knowOcc.ContainsKey(occName)) Then
compOcc.Appearance = knowOcc.Item(occName)
Else
Dim uAppearance As Asset = topAsm.Assets.Add(AssetTypeEnum.kAssetTypeAppearance, "Generic", "appearances")
Dim uColor As ColorAssetValue = uAppearance.Item("generic_diffuse")
Dim RNG = Math.Round(Rnd() * 255)
Dim RNG1 = Math.Round(Rnd() * 255)
Dim RNG2 = Math.Round(Rnd() * 255)
uColor.Value = ThisApplication.TransientObjects.CreateColor(RNG, RNG1, RNG2)
compOcc.Appearance = uAppearance
knowOcc.Add(occName, uAppearance)
End If
Next
trans.End()
Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Blog: hjalte.nl - github.com