iLogic – Set Every Part to A Different Colour

 

iLogic – Set Every Part to A Different Colour

I ran across a really interesting thread on the Autodesk Inventor ideas station. The feature request is to have a visual style in Inventor that sets every component in an assembly to a unique colour.

A comment by Tanner Dant included some VBA code, which creates a new view representation called “Unique Colors” and sets each component to it’s own colour. The Assembly can be set back to “Default”, restoring the model. Running the rule again cycles the model through a different set of unique colours and updates the view rep.

I thought this was really useful, so I adapted it to work with iLogic.

The animated GIF below shows the code in action.

random colours

The only tricky bit of the code was generating the random colours. In Tanners VBA code, he uses a Function to randomise the colours. I was able to achieve the same thing, by creating a random value for each or the RGB values required.

RNG = Round(Rnd * 255)
RNG1 = Round(Rnd * 255)
RNG2 = Round(Rnd * 255)

Here is the iLogic Code:

'Code based on an Inventor Ideas forum post by Tanner Dant
'https://forums.autodesk.com/t5/Inventor-ideas/visual-style-every-part-With-different-color/idc-p/8713666#M34691
'Adapted for iLogic by @ClintBrown3D, originally posted at https://clintbrown.co.uk/ilogic-set-every-part-to-a-different-colour

Sub Main()
    Dim topAsm As AssemblyDocument
    topAsm = ThisApplication.ActiveDocument
    
    Dim trans As Transaction
    trans = ThisApplication.TransactionManager.StartTransaction(topAsm, "Unique Colors")
    
    Dim ucRep As DesignViewRepresentation
    
    On Error GoTo CreateDV
    ucRep = topAsm.ComponentDefinition.RepresentationsManager.DesignViewRepresentations("Unique Colors")
    On Error GoTo 0
    
    ucRep.Activate
	
    Dim compOcc As ComponentOccurrence
    For Each compOcc In topAsm.ComponentDefinition.Occurrences
        Dim uAppearance As Asset
        uAppearance = topAsm.Assets.Add(kAssetTypeAppearance, "Generic", "appearances")
		        
        Dim uColor As ColorAssetValue
        uColor = uAppearance.Item("generic_diffuse")
		RNG = Round(Rnd * 255)
		RNG1 = Round(Rnd * 255)
		RNG2 = Round(Rnd * 255)

        uColor.Value = ThisApplication.TransientObjects.CreateColor(RNG, RNG1, RNG2)
        
        compOcc.Appearance = uAppearance
    Next
    
    trans.End
    Exit Sub
    
CreateDV:
    ucRep = topAsm.ComponentDefinition.RepresentationsManager.DesignViewRepresentations.Add("Unique Colors")
    Resume Next
End Sub

One thought on “iLogic – Set Every Part to A Different Colour

Comments are closed.