Calling all iLogic Gurus

Calling all iLogic Gurus

lancermandc_co_za
Enthusiast Enthusiast
486 Views
6 Replies
Message 1 of 7

Calling all iLogic Gurus

lancermandc_co_za
Enthusiast
Enthusiast

Hi everyone,

I am seeking assistance with recoding my iLogic script. To simplify the testing process, I have reduced my assembly to just two .ipt files. However, I continue to encounter the same errors repeatedly, regardless of the changes I make to the code.

My goal is to change the color appearance of multiple part files within an assembly without having to modify each part individually.

Thank you in advance for your help.

Best regards,

 

' iLogic rule to color parts in an assembly
Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument

Dim oCompOccs As ComponentOccurrences
oCompOccs = oAssyDoc.ComponentDefinition.Occurrences

' Define colors
Dim colorList As String() = {"Slate"}
Dim colorIndex As Integer = 0

For Each oOcc As ComponentOccurrence In oCompOccs
    Try
        ' Get the appearance style from the document's appearance library
        Dim oAppearance As Asset
        oAppearance = oAssyDoc.Assets.Item(colorList(colorIndex))
        
        ' Apply the appearance
        oOcc.Appearance = oAppearance
        colorIndex = (colorIndex + 1) Mod colorList.Length
    Catch ex As Exception
        MessageBox.Show("Error setting color for component: " & oOcc.Name & vbCrLf & "Error: " & ex.Message)
    End Try
Next

 

My code:

' iLogic rule to color parts in an assembly
Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument

Dim oCompOccs As ComponentOccurrences
oCompOccs = oAssyDoc.ComponentDefinition.Occurrences

' Define colors
Dim colorList As String() = {"Slate"}
Dim colorIndex As Integer = 0

For Each oOcc As ComponentOccurrence In oCompOccs
    Try
        ' Get the appearance style from the document's appearance library
        Dim oAppearance As Asset
        oAppearance = oAssyDoc.Assets.Item(colorList(colorIndex))
        
        ' Apply the appearance
        oOcc.Appearance = oAppearance
        colorIndex = (colorIndex + 1) Mod colorList.Length
    Catch ex As Exception
        MessageBox.Show("Error setting color for component: " & oOcc.Name & vbCrLf & "Error: " & ex.Message)
    End Try
Next

 

0 Likes
487 Views
6 Replies
Replies (6)
Message 2 of 7

Michael.Navara
Advisor
Advisor

The first issue without testing is the collection of oAssyDoc.Assets has the first element at index 1 not 0

Message 3 of 7

youann21700
Advocate
Advocate

I havent checked your code but this is a script we use for the same purpose. I will get you to choose a colour/appearance that is in your active appearance library. You can then chose specfic parts in the assembly to change to your selected colour. I should be plug and play from what i can see.

 

 

Sub Main
	Dim oAsset_Array As New ArrayList
	For Each oAsset_Array_X In ThisApplication.ActiveAppearanceLibrary.AppearanceAssets
		oAsset_Array.Add(oAsset_Array_X.DisplayName)
		oAsset_Array.Sort()
	Next
	oAsset_Array_Selected = InputListBox("CHOOSE TEXTURE FROM ABOVE LIST", oAsset_Array, oAsset_Array.Item(0), "TEXTURE SELECTION", "LIST OF TEXTURES")
	If oAsset_Array_Selected = "" Then
		Return
	End If

	Dim oSelect As New clsSelect
	oSelect.WindowSelect(ThisApplication)
	comps = oSelect
	For Each comp As ComponentOccurrence In oSelect.SelectedObjects
		Dim oDef As PartDocument
		oDef = comp.Definition.Document

		Dim i As Asset
		Dim j As Asset
		Dim status As String = "No"
		Dim oAppearance As Asset

		For Each j In ThisApplication.ActiveAppearanceLibrary.AppearanceAssets
			If j.DisplayName = oAsset_Array_Selected
			oAppearance = j
			Else
			End If 
		Next

			For Each i In oDef.AppearanceAssets
				If i.DisplayName = oAsset_Array_Selected Then
					oDef.ActiveAppearance = i
					status = "Yes"
				End If
			Next

			If status = "No"
				localappearance = oAppearance.CopyTo(oDef)
				oDef.ActiveAppearance = localappearance
				Else
			End If
		Next
End Sub

Class clsSelect
	Private WithEvents oInteractEvents As InteractionEvents
	Private WithEvents oSelectEvents As SelectEvents
	Private bTooltipEnabled As Boolean
	Private ThisApplication As Inventor.Application
	Public SelectedObjects As ObjectsEnumerator
	Private stillSelecting As Boolean = True

	Public Sub WindowSelect(oApp As Inventor.Application)
	ThisApplication = oApp
	oInteractEvents = ThisApplication.CommandManager.CreateInteractionEvents
	oInteractEvents.InteractionDisabled = False
	oSelectEvents = oInteractEvents.SelectEvents
	oSelectEvents.AddSelectionFilter(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter)
	oSelectEvents.WindowSelectEnabled = True
	bTooltipEnabled = ThisApplication.GeneralOptions.ShowCommandPromptTooltips
	ThisApplication.GeneralOptions.ShowCommandPromptTooltips = True
	oInteractEvents.StatusBarText = "Select components. Shift+Click to unselect. Esc to finish."
	oInteractEvents.Start()
	While stillSelecting
		ThisApplication.UserInterfaceManager.DoEvents()
	End While
	End Sub
	Private Sub oInteractEvents_OnTerminate() Handles oInteractEvents.OnTerminate
	ThisApplication.GeneralOptions.ShowCommandPromptTooltips = bTooltipEnabled
	oSelectEvents = Nothing
	oInteractEvents = Nothing
	stillSelecting = False
	End Sub
	Private Sub oSelectEvents_OnSelect(ByVal JustSelectedEntities As ObjectsEnumerator, ByVal SelectionDevice As SelectionDeviceEnum, ByVal ModelPosition As Point, ByVal ViewPosition As Point2d, ByVal View As View) Handles oSelectEvents.OnSelect
	SelectedObjects = oSelectEvents.SelectedEntities
	End Sub
	Private Sub oSelectEvents_OnUnSelect(UnSelectedEntities As ObjectsEnumerator, SelectionDevice As SelectionDeviceEnum, ModelPosition As Point, ViewPosition As Point2d, View As View) Handles oSelectEvents.OnUnSelect
	SelectedObjects = oSelectEvents.SelectedEntities
	End Sub
End Class


'		oDef = comp.Definition.Document
'		Dim oRenderStyle As renderstyle
'		oRenderStyle = oDef.renderstyles.item(oAsset_Array_Selected)
'		oDef.Activerenderstyle = oRenderStyle
'		iLogicVb.UpdateWhenDone = True

 

 

0 Likes
Message 4 of 7

lancermandc_co_za
Enthusiast
Enthusiast

Hello!

 

Many thanks for the script!

 

I did work; however, I'm looking to change the colour of each individual part different to the next just like the function "Display Separate Colors" Inventor has built in.

 

I need to display my assembly in a .idw drawing that will distinctly show all parts in various colours.

0 Likes
Message 5 of 7

youann21700
Advocate
Advocate

Ah I see, my bad.

Have a look at this link, I used it for a similar tool a while back. Think its what you are wanting.

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

0 Likes
Message 6 of 7

lancermandc_co_za
Enthusiast
Enthusiast

Hi...

Many thanks for the link. It had some coding errors in the script which I re-wrote and got Inventor to stop shouting errors at me 🙂

 

Here's my code below and once again, thanks for your help!

 

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
    Dim uAppearance As Asset
    Dim uColor As ColorAssetValue
    Dim RNG As Integer
    Dim RNG1 As Integer
    Dim RNG2 As Integer
    
    For Each compOcc In topAsm.ComponentDefinition.Occurrences
        On Error GoTo AssetError
        uAppearance = topAsm.Assets.Add(kAssetTypeAppearance, "Generic", "appearances")
        On Error GoTo 0
        
        If Not uAppearance Is Nothing Then
            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)
            
            On Error GoTo AppearanceError
            compOcc.Appearance = uAppearance
            On Error GoTo 0
        End If
    Next
    
    trans.End
    Exit Sub
    
CreateDV:
    ucRep = topAsm.ComponentDefinition.RepresentationsManager.DesignViewRepresentations.Add("Unique Colors")
    Resume Next
    
AssetError:
    MsgBox("Error creating asset for component: " & compOcc.Name)
    Resume Next
    
AppearanceError:
    MsgBox("Error setting appearance for component: " & compOcc.Name)
    Resume Next
End Sub

 

Message 7 of 7

dave_taylor
Advocate
Advocate

Your code works for me in Inventor 2025 without errors.

 

Every time I run the code, it is creating a new "Generic" appearance with a (1) or (2), etc. suffix if the appearance name already exists. I would probably have it named something unique, then go through the appearances and clean them up/remove them that have that name with the code before recreating others. You could wind up with many appearances of a similar name if you keep running it.

 

dave_taylor_0-1730308262672.png

 

Can you elaborate on what the errors say when you run it?

 

You can even use a Try/Catch instead of an OnError Goto to get the exception if you want...I find it easier than trying to handle the errors in an OnError...

Like this:

 

 

Try
	uAppearance = topAsm.Assets.Add(kAssetTypeAppearance, "Generic", "appearances")

    If Not uAppearance Is Nothing Then
        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
    End If
Catch ex As Exception
	MessageBox.Show(ex.Message, "Error")
End Try

That "ex" is the exception thrown on an error and you can send the message to a messagebox to show you.  You can also get rid of the error handling altogether and let the application throw its errors directly on screen.

 


Dave Taylor
MCAD Solutions Engineer
Hagerman & Co.


Autodesk Certified Instructor - Platinum



*Likes to this post are appreciated if the information I have shared is helpful to you and/or others...
**Did this resolve your issue? Please accept it "As a Solution" so others may benefit from it.
0 Likes