Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
aurel_e
1291 Views, 5 Replies

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

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.

5 REPLIES 5
Message 2 of 6
JelteDeJong
in reply to: aurel_e

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 6
johnsonshiue
in reply to: aurel_e

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
Message 4 of 6
JelteDeJong
in reply to: johnsonshiue

@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 6
aurel_e
in reply to: JelteDeJong

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.

Message 6 of 6
cadEY2BL
in reply to: JelteDeJong

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report