Update appearance asset

Update appearance asset

CadUser46
Collaborator Collaborator
917 Views
2 Replies
Message 1 of 3

Update appearance asset

CadUser46
Collaborator
Collaborator

Cross ref https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/update-all-styles-in-ipt-iam-with-vb...

 

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

 

 

 

CadUser46_0-1653316058691.png

 


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
0 Likes
918 Views
2 Replies
Replies (2)
Message 2 of 3

A.Acheson
Mentor
Mentor

I haven't tested your part but doing this manually. It looks like the styles need to be updated which will bring in the material and the associated appearance. You then need to ensure the appearance has not been overridden in the local document meaning you need to select clear override to follow the material style from the library. Changing  the source appearance is how to do that. Sourced from this post here.  Maybe this will help?

Dim oDoc = ThisDoc.Document

oDoc.AppearanceSourceType = AppearanceSourceTypeEnum.kMaterialAppearance

You could also use the command manager to update material styles similar to the manual activation of the update button. 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 3

d_dankerl6K5U5
Explorer
Explorer
Dim thisDocument As Document = ThisApplication.ActiveDocument
Dim existingRanderStyle As RenderStyle
    
 
For Each existingRanderStyle In thisDocument.RenderStyles
 
If existingRanderStyle.UpToDate = False Then
 
    existingRanderStyle.UpdateFromGlobal
 
    End If
 
Next
0 Likes