Sometimes have to set appearance twice.

Sometimes have to set appearance twice.

Anonymous
Not applicable
413 Views
3 Replies
Message 1 of 4

Sometimes have to set appearance twice.

Anonymous
Not applicable
Public Sub SetAppearanceToGreenRubber()
    Dim odoc As Document
    Set odoc = ThisApplication.ActiveDocument

    odoc.AppearanceSourceType = AppearanceSourceTypeEnum.kMaterialAppearance
    odoc.Update
    odoc.ActiveAppearance = odoc.AppearanceAssets.Item("Rubber - Green")
    odoc.Save
    'odoc.Close
End Sub

Sometimes this works the first time, sometimes I have to run it twice.

 

More:

I have some parts that I created from a multibody solid - the outer appearance is green rubber (for ..."reasons"), but the section shows as either "generic" or the material.  So, I want to clear the override appearance, then set the appearance to green rubber.  Close is commented out, because I sometimes have to hit the macro 2x.

 

Any ideas?

0 Likes
Accepted solutions (1)
414 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable

An additional note - this code also fails on a new part that has not previously had some appearance set to green rubber.

0 Likes
Message 3 of 4

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@Anonymous,

 

Try below VBA code to set appearance "Rubber - Green" to part document. It also works in first time and new part as well.

 

Public Sub SetAppearanceToGreenRubber()

    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oDef As PartComponentDefinition
    Set oDef = oDoc.ComponentDefinition

    'define appearance library by name
    Dim assetLib As AssetLibrary
    Set assetLib = ThisApplication.AssetLibraries.Item("Inventor Material Library")
     
    'define colors to work with
    Dim libAsset As Asset
    Set libAsset = assetLib.AppearanceAssets.Item("Rubber - Green")
    
    Dim localAsset As Asset
    On Error Resume Next
    Set localAsset = libAsset.CopyTo(oDoc)
    If Err.Number <> 0 Then
        Set localAsset = oDoc.Assets.Item("Rubber - Green")
    End If
    
    oDoc.ActiveAppearance = localAsset
    
    Call oDoc.Save
End Sub

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 4 of 4

Anonymous
Not applicable

This is just about what I ended up with, in the end - yours is more elegant.

 

It's weird to me that the appearances are not available until copied into the document, but I understand.

0 Likes