Update appearance asset
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have a subset of models that are proving difficult to automate update styles.
What i want is to update "Steel, Mild" to match the library but this does not happen. It would appear that to do this manually requires two steps instead of the usual one.
1. Update Styles > Autodesk Appearance Library > Steel, local, yes.
2. Update Styles > Inventor Material Library > Steel, local, yes.
I thought i could force this to work in the api by applying Generic Material, Default Appearance, purge everything then get the Steel, Mild style from the library. In this subset though the Steel Appearance cannot be deleted, thus it does not get overwritten when i get Steel, Mild from the library. I have tried manually tracing this style, broken every reference i can find but still it cannot be deleted.
I just need a robust method to ensure the material and colour styles get updated.
rivate Sub PurgeUpdateAssets()
' https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/update-all-styles-in-ipt-iam-with-vba/m-p/9612023
Dim pDoc As PartDocument: Set pDoc = ThisApplication.ActiveDocument
' Apply Generic Material
Dim aGeneric As String: aGeneric = "Generic"
On Error Resume Next
Dim mAsset As MaterialAsset: Set mAsset = pDoc.Assets(aGeneric)
If Err <> 0 Then Set mAsset = GetMatAssetFromLib(pDoc, aGeneric)
pDoc.ActiveMaterial = mAsset
On Error GoTo 0
' Apply Default Appearance
Dim aDefault As String: aDefault = "Default"
On Error Resume Next
Dim aAsset As Asset: Set aAsset = pDoc.AppearanceAssets("Default")
If Err <> 0 Then Set aAsset = GetAppAssetFromLib(pDoc, aDefault)
pDoc.ActiveAppearance = aAsset
On Error GoTo 0
' Purge old materials
For Each mAsset In pDoc.MaterialAssets
'pDoc.MaterialAssets("Steel, Mild").Delete
If mAsset.IsUsed = False Then mAsset.Delete
Next
For Each aAsset In pDoc.Assets
Debug.Print aAsset.DisplayName
If aAsset.IsUsed = False Then aAsset.Delete
Next
' Get new material from library
pDoc.ActiveMaterial = GetMatAssetFromLib(pDoc, "Steel, Mild")
Dim Matl As Material: Set Matl = pDoc.Materials.Item("Steel, Mild")
Call Matl.UpdateFromGlobal
' Iterate through the objects that have an override.
Dim obj As Object
For Each obj In pDoc.ComponentDefinition.AppearanceOverridesObjects
' Set the source of the appearance based on the type of object.
' It's possible to use kPartAppearance in all cases, but this sets it to the default source used by Inventor when no overrides exist.
If TypeOf obj Is SurfaceBody Then
obj.AppearanceSourceType = kPartAppearance
ElseIf TypeOf obj Is PartFeature Then
obj.AppearanceSourceType = kPartAppearance
ElseIf TypeOf obj Is Face Then
obj.AppearanceSourceType = kPartAppearance
Else
MsgBox "Unexpected type with appearance override: " & TypeName(obj)
End If
Next
pDoc.ComponentDefinition.ClearAppearanceOverrides
End Sub
Private Function GetMatAssetFromLib(oDoc As PartDocument, AssetName As String) As MaterialAsset
Dim matLib As AssetLibrary: Set matLib = ThisApplication.AssetLibraries.Item("Inventor Material Library")
Dim libAsset As Asset: Set libAsset = matLib.MaterialAssets.Item(AssetName)
On Error Resume Next
Set GetMatAssetFromLib = libAsset.CopyTo(oDoc)
Debug.Print Err.Number
If Err Then Set GetMatAssetFromLib = libAsset
End Function
Private Function GetAppAssetFromLib(oDoc As PartDocument, AssetName As String) As Asset
Dim assetLib As AssetLibrary: Set assetLib = ThisApplication.AssetLibraries.Item("Autodesk Appearance Library")
Dim libAsset As Asset: Set libAsset = assetLib.AppearanceAssets.Item("AssetName")
On Error Resume Next
Set GetAppAssetFromLib = libAsset.CopyTo(oDoc)
Debug.Print Err.Number
If Err Then Set GetAppAssetFromLib = libAsset
End Function
Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.
---------------------------------------------------------------------------------------------------------------------------
Inventor 2010 Certified Professional
Currently using 2023 Pro