Edit Color of ActiveAppearance

Edit Color of ActiveAppearance

mgaunt3DSG
Enthusiast Enthusiast
718 Views
8 Replies
Message 1 of 9

Edit Color of ActiveAppearance

mgaunt3DSG
Enthusiast
Enthusiast

I often get step files that have all parts grey. I have written an iLogic Rule that loops through my selected parts and applies predefined appearances to those parts. I would like to expand this to include applying materials and use the default material appearance as a base to apply a color to.

 

Here's how I do it manually.

Start with a part:

mgaunt3DSG_0-1728653498740.png

Apply Material:

mgaunt3DSG_1-1728653540886.png

Change Appearance Color:

mgaunt3DSG_2-1728653645116.png

 

The specific colors will either come from a predefined list or read from Excel. Other materials (Steel) will need an image to be removed from their default appearance (Plate) and for others (Steel, Galvinized) I would like to move the image to be a bump instead.

 

For reference, this is the iLogic rule I currently have:

 

'Make a ref to active doc
Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument

'selected components collection
Dim oSelected As ObjectCollection
oSelected = ThisApplication.TransientObjects.CreateObjectCollection

Dim oCount As Integer
oCount = oDoc.SelectSet.Count

'Check that at least 1 is selected
If oCount = 0 Then
	MessageBox.Show("Please select a component.", "iLogic")
	Exit Sub 'bail out
End If

Dim oList As New List(Of String)

oList.Add("MyColor")
oList.Add("Do Not Use")

myAppearance = InputListBox("Prompt", oList, "", Title:= "Select Appearnce Type", ListName := "Appearances")

Dim i As Integer
'add to Object Collection
For i = 1 To oCount
	If oDoc.SelectSet.Item(i).Type = ObjectTypeEnum.kComponentOccurrenceObject Then
		oSelected.Add(oDoc.SelectSet.Item(i))
	End If
Next

Dim k As Integer = 1
' If there are selected components we can do something
For Each comp In oSelected
	Dim oDef As PartDocument
    oDef = comp.Definition.Document
	oDef.ComponentDefinition.ClearAppearanceOverrides
	
    Dim oRenderStyle As RenderStyle
	Try
    	oRenderStyle = oDef.RenderStyles.Item(myAppearance & "(" & k & ")")
	Catch
		k = 1
		oRenderStyle = oDef.RenderStyles.Item(myAppearance & "(" & k & ")")
	End Try
    oDef.ActiveRenderStyle = oRenderStyle
	k = k + 1
	
	' There can be references between assets,
	' so keep doing it until only used things remain
	Dim foundUnused As Boolean
	Do
		foundUnused = False
		Dim a As Asset
		For Each a In oDef.Assets

			ThisApplication.StatusBarText = "Working with...... " & a.DisplayName & "......" & oDoc.FullFileName
			If Not a.IsUsed Then
				a.Delete
				foundUnused = True
			End If
		Next
	Loop While foundUnused
    iLogicVb.UpdateWhenDone = True
Next

 

 

0 Likes
Accepted solutions (3)
719 Views
8 Replies
Replies (8)
Message 2 of 9

Curtis_Waguespack
Consultant
Consultant
Accepted solution

@mgaunt3DSG, this example will create and modify a color and set it active

 

Imports System.ComponentModel
AddReference "System.drawing"
Imports System.Windows.Forms
Imports System.Drawing

Sub Main

	Dim oClDlg As New System.Windows.Forms.ColorDialog
	Dim oColor As System.drawing.Color
	If oClDlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
		oColor = oClDlg.Color
	End If

	Dim oDoc As PartDocument
	oDoc = ThisApplication.ActiveDocument

	Dim oAppearance As Asset
	'get active appearance
	oAppearance = oDoc.ActiveAppearance

	oName = "New Color"
	'create new appearance named Test
	If Not oDoc.ActiveAppearance.DisplayName = oName Then
		Try
		oAppearance = oDoc.Assets.Add(AssetTypeEnum.kAssetTypeAppearance, "Generic", "Appearances", oName)
		Catch
			oAppearance =  oDoc.Assets.Item(oName)
		End Try
		'set new appearance active
		oDoc.ActiveAppearance = oAppearance
	End If

	'set colors
	Dim oNewColor As ColorAssetValue
	oNewColor = oAppearance.Item("generic_diffuse")
	oNewColor.Value = ThisApplication.TransientObjects.CreateColor(oColor.R, oColor.G, oColor.B)

End Sub

 

EESignature

Message 3 of 9

Curtis_Waguespack
Consultant
Consultant

Also, in case it's of interest, here is an example to clear overrides from the assembly

 

Dim oComponent As ComponentOccurrence
Dim oDoc As Document

While True
	oComponent = ThisApplication.CommandManager.Pick(
	SelectionFilterEnum.kAssemblyOccurrenceFilter,
	"Select a component")

	' If nothing gets selected then we're done	
	If IsNothing(oComponent) Then Exit While
	
	oDoc = oComponent.Definition.Document
	oDoc.ComponentDefinition.ClearAppearanceOverrides
	oComponent.AppearanceSourceType = AppearanceSourceTypeEnum.kPartAppearance
End While

 

EESignature

Message 4 of 9

mgaunt3DSG
Enthusiast
Enthusiast

Thanks Curtis, after just a little work I got this working in my code.

 

Do you have a solution for removing/changing the texture/bump of the appearance?

Message 5 of 9

Curtis_Waguespack
Consultant
Consultant
Accepted solution

@mgaunt3DSG, I looked but was not seeing how to remove the image, although I'm sure it can be done.

Imight have updated the example after you copied it, but look at the one I posted earlier again. It creates a new color rather than modifying the active. I think that will be easier than removing the image and bump, etc. And might be safer since it is not modifying an existing color.

 

Here is another version that copies an existing color, and modifies the copy. Note that the internal property for the color changed since this example is copying a metallic paint. So the line that sets oNewColor might need to be changed if you were to use this and set it to copy a different color type.

 

I think I have this version setting the color for any color you might choose to copy.

 

 

 

Imports System.ComponentModel
AddReference "System.drawing"
Imports System.Windows.Forms
Imports System.Drawing

Dim oClDlg As New System.Windows.Forms.ColorDialog
Dim oColor As System.drawing.Color
If oClDlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
	oColor = oClDlg.Color
End If

Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument

Dim oAppearance As Asset
sName = "New Color"
AppearanceTopCopy = "Glossy - Black"

'get an appearance to copy
oLib = ThisApplication.ActiveAppearanceLibrary
Try
	oAppearance = oLib.AppearanceAssets.Item(AppearanceTopCopy).CopyTo(ThisDoc.Document)
Catch
	oAppearance = oDoc.Assets.Item(AppearanceTopCopy)
End Try

oAppearance = oAppearance.Duplicate 'copy it
oDoc.ActiveAppearance = oAppearance 'set it active
Try : oDoc.Assets.Item(sName).Delete : Catch : End Try ' try to delete an existing one with this name
oAppearance.DisplayName = sName ' rename the copied 

'set colors
Dim oNewColor As ColorAssetValue

'find the color property and set it to use the RGB
For i As Integer = 1 To oAppearance.Count
	If oAppearance.Item(i).DisplayName = "Color" Then
		oNewColor = oAppearance.Item(i)
		oNewColor.Value = ThisApplication.TransientObjects.CreateColor(oColor.R, oColor.G, oColor.B)
	End If
Next

EESignature

Message 6 of 9

Curtis_Waguespack
Consultant
Consultant

Also this rule might be helpful for working with the appearances:

'show ilogic logger
ThisApplication.UserInterfaceManager.DockableWindows.Item("ilogic.logwindow").Visible = True

Dim oDoc As PartDocument = ThisApplication.ActiveDocument
oAsset = oDoc.ActiveAppearance

Logger.Info("")
Logger.Info("oAsset.Name = " & oAsset.Name)
Logger.Info("oAsset.DisplayName = " & oAsset.DisplayName)
Logger.Info("oAsset.CategoryName = " & oAsset.CategoryName)
Logger.Info("oAsset.HasTexture = " & oAsset.HasTexture)
Logger.Info("oAsset.IsReadOnly = " & oAsset.IsReadOnly)
Logger.Info("")
If oAsset.Count > 0 Then
	For i As Integer = 1 To oAsset.Count
		Logger.Info("oAsset.Item(" & i & ").Name = " & oAsset.Item(i).Name)
		Logger.Info("oAsset.Item(" & i & ").DisplayName = " & oAsset.Item(i).DisplayName)
		Logger.Info("oAsset.Item(" & i & ").IsReadOnly = " & oAsset.Item(i).IsReadOnly)
		Logger.Info("")
	Next
End If

 

EESignature

Message 7 of 9

mgaunt3DSG
Enthusiast
Enthusiast

Thank you for your continued support Curtis.

 

Here is what I have now:

'Make a ref to active doc
Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument

'selected components collection
Dim oSelected As ObjectCollection
oSelected = ThisApplication.TransientObjects.CreateObjectCollection

Dim oCount As Integer
oCount = oDoc.SelectSet.Count
'Check that at least 1 is selected
If oCount = 0 Then
	MessageBox.Show("Please select a component.", "iLogic")
	Exit Sub 'bail out
End If

'Get Desired Material
Dim MaterialList As New List(Of String)

MaterialList.Add("Aluminum 6061")
MaterialList.Add("Steel")

myMaterial = InputListBox("Select Material", MaterialList, "", Title:= "Select Material", ListName := "Materials")

'Get the Appearance to be modified based on the Material Selected
Dim baseAppearance As String

Select Case myMaterial
	Case "Aluminum 6061"
		baseAppearance = "Aluminum - Flat"
	Case "Steel"
		baseAppearance = "Plate Fixed" 'This is a modified "Plate" Appearance with no Surface Texture but the Bumpmap Remains
End Select

'Get Color scheme
Dim AppearanceList As New List(Of String)

AppearanceList.Add("Inventor")
AppearanceList.Add("Office")
AppearanceList.Add("Office 2013-2022")
AppearanceList.Add("Office 2007-2010")
AppearanceList.Add("Edge")

myAppearance = InputListBox("Select Color Scheme", AppearanceList, "", Title:= "Select Color Scheme", ListName := "Appearances")

'Get the Excel Workbook information and Scheme limits
Dim ExcelSheet As String
Dim startRow, kLimit As Integer

Select Case myAppearance
	Case "Inventor"
		ExcelSheet = "Inventor"
		startRow = 51
		kLimit = 6
	Case "Office"
		ExcelSheet = "Excel"
		startRow = 37
		kLimit = 6
	Case "Office 2013-2022"
		ExcelSheet = "Excel"
		startRow = 47
		kLimit = 6
	Case "Office 2007-2010"
		ExcelSheet = "Excel"
		startRow = 57
		kLimit = 6
	Case "Edge"
		ExcelSheet = "Edge"
		startRow = 2
		kLimit = 9
End Select

Dim i As Integer
'add to Object Collection
For i = 1 To oCount
	If oDoc.SelectSet.Item(i).Type = ObjectTypeEnum.kComponentOccurrenceObject Then
		oSelected.Add(oDoc.SelectSet.Item(i))
	End If
Next

Dim k As Integer = 1
Dim ExcelFile As String = "C:\Users\mgaunt\Documents\CC\PowerColor.xlsm"
' If there are selected components we can do something
For Each comp In oSelected
	Dim oDef As PartDocument
    oDef = comp.Definition.Document
	oDef.ComponentDefinition.ClearAppearanceOverrides
	
	iProperties.MaterialOfComponent(comp.Name) = myMaterial
	
	'Setup the base appearance of the component
	Dim oAppearance As Asset
	Dim oRenderStyle As RenderStyle
    oRenderStyle = oDef.RenderStyles.Item(baseAppearance)
	oDef.ActiveRenderStyle = oRenderStyle
	oAppearance = oDef.ActiveAppearance
	oAppearance = oAppearance.Duplicate
	oDef.ActiveAppearance = oAppearance
	
	'set colors
	oRed = GoExcel.CellValue(ExcelFile, ExcelSheet, "B" & startRow + k)
	oGreen = GoExcel.CellValue(ExcelFile, ExcelSheet, "C" & startRow + k)
	oBlue = GoExcel.CellValue(ExcelFile, ExcelSheet, "D" & startRow + k)
	Dim oNewColor As ColorAssetValue
	oNewColor = oAppearance.Item("generic_diffuse")
	oNewColor.Value = ThisApplication.TransientObjects.CreateColor(oRed, oGreen, oBlue)
	oAppearance.DisplayName = baseAppearance & " (" & oRed & "," & oGreen & "," & oBlue & ")"
		
	k = k + 1
	If k > kLimit Then k = 1
	
	' There can be references between assets,
	' so keep doing it until only used things remain
	Dim foundUnused As Boolean
	Do
		foundUnused = False
		Dim a As Asset
		For Each a In oDef.Assets

			ThisApplication.StatusBarText = "Working with...... " & a.DisplayName & "......" & oDoc.FullFileName
			If Not a.IsUsed Then
				a.Delete
				foundUnused = True
			End If
		Next
	Loop While foundUnused
    iLogicVb.UpdateWhenDone = True
Next

For any material that I want a bumpmap on, I will just create a custom Appearance and leave it in one of my Appearance Libraries before adding the material to my list and case statement. Each part will be left with its assigned material and associated appearance as well as the custom appearance this code creates.

Message 8 of 9

Curtis_Waguespack
Consultant
Consultant
Accepted solution

@mgaunt3DSG , I was able to find a way to turn the bump and texture on/off, see these links and examples below.

 

https://forums.autodesk.com/t5/inventor-programming-ilogic/change-an-image-for-an-coppied-appearance...

 

https://adndevblog.typepad.com/manufacturing/2018/01/vba-code-to-create-new-appearance-in-a-document...

 

Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument

oAppearance = oDoc.ActiveAppearance

Dim generic_color As ColorAssetValue
generic_color = oAppearance.Item("generic_diffuse")
generic_color.HasConnectedTexture = False

Dim generic_bump As ColorAssetValue
generic_bump = oAppearance.Item("generic_bump_map")
generic_bump.HasConnectedTexture = False

 

Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument

oAppearance = oDoc.ActiveAppearance

Dim generic_color As ColorAssetValue
generic_color = oAppearance.Item("generic_diffuse")
generic_color.HasConnectedTexture = True

Dim generic_bump As ColorAssetValue
generic_bump = oAppearance.Item("generic_bump_map")
generic_bump.HasConnectedTexture = True

oImage = "C:\Temp\Test.png"

generic_color.ConnectedTexture.Item("unifiedbitmap_Bitmap").Value = oImage
generic_bump.ConnectedTexture.Item("unifiedbitmap_Bitmap").Value = oImage

 

EESignature

Message 9 of 9

mgaunt3DSG
Enthusiast
Enthusiast

Thanks again Curtis. This is very helpful.

 

I have found, however, that the code only works on generic type appearances. If I am using an edited appearance, I can use the "Duplicate as Generic" option while making the edits, is there a way to do that in iLogic?

0 Likes