I apologize for the confusion, I am not being clear. The snippet I have provided is from me debugging my larger rule, the message box in the snippet is just letting me know it is working right. The larger rule loops through all the features in a part file and swaps the appearance of the same to a color coordinated version of the body appearance. I am using the Pick Method to toggle the selected face's appearance between 'Precision Machined' and feature appearance.
For clarity, here is the larger rule as I have it now:
Option Explicit On
Sub main
' Get the active document
Dim oDoc As PartDocument = ThisApplication.ActiveEditDocument
' Get the component definition
Dim oCompDef As PartComponentDefinition = oDoc.ComponentDefinition
' Define colors
Dim oTG As TransientObjects = ThisApplication.TransientObjects
Dim oOrange As Color = oTG.CreateColor(255, 128, 26)
Dim oGreen As Color = oTG.CreateColor(0, 140, 26)
Dim oBlue As Color = oTG.CreateColor(0, 128, 255)
' Loop bodies
Dim oBody As SurfaceBody
For Each oBody In oCompDef.SurfaceBodies
' Get Appearance to modify
Dim oAppearance As Asset = oBody.Appearance
Dim oAppearanceName As String = oAppearance.DisplayName
If oAppearance.LocalType = kGenericAssetType Then
' Copy Appearance
oAppearance = oAppearance.Duplicate
If oAppearance.Item("generic_diffuse").HasConnectedTexture = True Then
Dim oImage As String
oImage = oAppearance.Item("generic_diffuse").ConnectedTexture.Item("unifiedbitmap_Bitmap").Value
oAppearance.Item("generic_diffuse").HasConnectedTexture = False
' Add Texture to bumpmap if bumpmap does not exist
If oImage <> "" And oAppearance.Item("generic_bump_map").HasConnectedTexture = False Then
oAppearance.Item("generic_bump_map").HasConnectedTexture = True
oAppearance.Item("generic_bump_map").ConnectedTexture.Item("unifiedbitmap_Bitmap").Value = oImage
End If
End If
Else
Try
' Find "Fixed" Appearance
oAppearanceName = oAppearanceName & " - Fixed"
' define appearance library by name
Dim assetLib As AssetLibrary
assetLib = ThisApplication.AssetLibraries.Item("Inventor Material Library")
' define colors to work with
Dim libAsset1 As Asset
libAsset1 = assetLib.AppearanceAssets.Item(oAppearanceName)
' try to Copy the asset locally.
Try
oAppearance = libAsset1.CopyTo(oDoc)
Catch
' assume error means it's already local
oAppearance = oDoc.Assets(oAppearanceName)
End Try
Catch
MessageBox.Show("Unable to find " & oAppearanceName & " in Inventor Material Library", "No Fixed Appearance") ', MessageBoxIcon.Error)
Exit Sub
End Try
End If
' Define Appearances
Dim oOrangeAppearance As Asset = GetOrCreateAppearanceAsset(oAppearance, oAppearanceName & " - Non-Machined", oOrange)
oAppearance.Item("generic_bump_map").HasConnectedTexture = True
oAppearance.Item("generic_bump_map").ConnectedTexture.Item("unifiedbitmap_Bitmap").Value = "C:\Users\Public\Documents\Autodesk\Inventor 2025\Textures\surfaces\Metal_15.bmp"
Dim oGreenAppearance As Asset = GetOrCreateAppearanceAsset(oAppearance, oAppearanceName & " - Rough Cut", oGreen)
Dim oBlueAppearance As Asset = GetOrCreateAppearanceAsset(oAppearance, oAppearanceName & " - Machined", oBlue)
oAppearance.Item("generic_bump_map").ConnectedTexture.Item("unifiedbitmap_Bitmap").Value = "C:\Users\Public\Documents\Autodesk\Inventor 2025\Textures\bumpmaps\Metal_15_bump.bmp"
Dim oGreenHoleAppearance As Asset = GetOrCreateAppearanceAsset(oAppearance, oAppearanceName & " - Rough Hole", oGreen)
Dim oBlueHoleAppearance As Asset = GetOrCreateAppearanceAsset(oAppearance, oAppearanceName & " - Precision Hole", oBlue)
' Loop through Features
Dim oFeature As PartFeature
For i As Integer = 1 To oBody.AffectedByFeatures.Count
oFeature = oBody.AffectedByFeatures.Item(i)
If TypeOf oFeature Is HoleFeature Then
If oFeature.IsClearanceHole Or oFeature.Tapped Then
Feature.Color(oFeature.Name) = oGreenHoleAppearance.Name
Else
Feature.Color(oFeature.Name) = oBlueHoleAppearance.Name
End If
Else
Feature.Color(oFeature.Name) = oGreenAppearance.Name
End If
Next i
' Set first Feature to non-machined
oFeature = oBody.CreatedByFeature
Feature.Color(oFeature.Name) = oOrangeAppearance.Name
' Determine cut direction in the future?
Next oBody
' Update Override Faces
Dim oFaces As ObjectCollection
oFaces = oDoc.AttributeManager.FindObjects("com.autodesk.inventor.FaceAttributes", "FaceColor")
Dim oFace As Face
For Each oFace In oFaces
oBody = oFace.Parent
Dim oAppearance As Asset = oBody.Appearance
If oFace.Appearance.DisplayName Like "*Rough Cut" Then
oFace.Appearance = oDoc.Assets(oAppearance.DisplayName & " - Rough Cut")
Else If oFace.Appearance.DisplayName Like "* Machined" Then
oFace.Appearance = oDoc.Assets(oAppearance.DisplayName & " - Machined")
Else If oFace.Appearance.DisplayName Like "*Rough Hole" Then
oFace.Appearance = oDoc.Assets(oAppearance.DisplayName & " - Rough Hole")
Else If oFace.Appearance.DisplayName Like "*Precision Hole" Then
oFace.Appearance = oDoc.Assets(oAppearance.DisplayName & " - Precision Hole")
End If
Next oFace
' Pick Precision Machined Surfaces
While True
oFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select a face to toggle Precision Machined")
' If nothing gets selected then we're done
If IsNothing(oFace) Then Exit While
oBody = oFace.Parent
Dim oAppearance As Asset = oBody.Appearance
Try
oAppearance = oDoc.Assets(oAppearance.DisplayName & " - Fixed")
Catch
End Try
If oFace.AppearanceSourceType = AppearanceSourceTypeEnum.kFeatureAppearance Then
If TypeOf oFace.CreatedByFeature Is HoleFeature Then
oFace.Appearance = oDoc.Assets(oAppearance.DisplayName & " - Precision Hole")
Else
oFace.Appearance = oDoc.Assets(oAppearance.DisplayName & " - Machined")
End If
Else
oFace.AppearanceSourceType = AppearanceSourceTypeEnum.kFeatureAppearance
End If
End While
' Delete unused Appearances
Dim foundUnused As Boolean
Do
foundUnused = False
Dim a As Asset
For Each a In oDoc.Assets
If Not a.IsUsed And a.AssetType = kAssetTypeAppearance Then
a.Delete
foundUnused = True
End If
Next
Loop While foundUnused
End Sub
Function GetOrCreateAppearanceAsset(ByVal oAppearance As Asset, ByVal oName As String, ByVal oColor As Color) As Asset
Dim oAssets As Assets = ThisApplication.ActiveEditDocument.Assets
Dim oAsset As Asset
Try ' If Appearance exists
oAsset = oAssets.Item(oName)
Catch ' If Appearance doesn't exist
oAsset = oAppearance.Duplicate
oAsset.DisplayName = oName
Dim oApColor As ColorAssetValue = oAsset.Item("generic_diffuse")
oApColor.Value = oColor
End Try
Return oAsset
End Function