Set different colour for each component of Assembly based on the PN.

Set different colour for each component of Assembly based on the PN.

aurel_e
Collaborator Collaborator
2,104 Views
6 Replies
Message 1 of 7

Set different colour for each component of Assembly based on the PN.

aurel_e
Collaborator
Collaborator

Hi 

I have found an Ilogic from Clint Brown website:

https://clintbrown.co.uk/2019/05/04/ilogic-set-every-part-to-a-different-colour/

 

I would like to have the same thing (random colors) but the same color for the same part or subassy in order to distinguish the components with the same Part Number.

 

Would it be possible?

 

Thank you.

0 Likes
Accepted solutions (1)
2,105 Views
6 Replies
Replies (6)
Message 2 of 7

JelteDeJong
Mentor
Mentor

try this iLogic rule:

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 occName As String = compOcc.ReferencedFileDescriptor.FullFileName
    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.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 7

johnsonshiue
Community Manager
Community Manager

Hi! If the color is assigned in the part (ipt) or subassembly (iam) file, the color should be on a per PN basis.

Many thanks!



Johnson Shiue (johnson.shiue@autodesk.com)
Software Test Engineer
0 Likes
Message 4 of 7

JelteDeJong
Mentor
Mentor
Accepted solution

@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.

EESignature


Blog: hjalte.nl - github.com

Message 5 of 7

aurel_e
Collaborator
Collaborator

Thank you very much @JelteDeJong 

 

It does what I asked, even when the Part Number is overwritten manually.

I wonder if it would be possible to change it to have the choice between setting a unique color to all the subassies or the single parts.

Before asking to this forum I was trying to change a rule that uses the BOM 

 

Dim oBOMView As BOMView = oBOM.BOMViews.Item("Parts Only")

Dim oBOMRow As BOMRow

For Each oBOMRow In oBOMView.BOMRows

 I wanted to have the choice between the "Parts only" or "Structured" depending on the need.

0 Likes
Message 6 of 7

cadEY2BL
Explorer
Explorer

@JelteDeJong 

 

Is it possible to change the random RGB value to a fixed RAL color chosen from a text parameter that is in the appearance RAL library? 

Message 7 of 7

b.kushaiynov
Contributor
Contributor

it gives error on component pattern. is it possible adjust it , so that patterns have same color ? 

0 Likes