Change part's color override VBA code

Change part's color override VBA code

Anonymous
Not applicable
1,504 Views
1 Reply
Message 1 of 2

Change part's color override VBA code

Anonymous
Not applicable

How can i change a part's color override through VBA code?

0 Likes
1,505 Views
1 Reply
Reply (1)
Message 2 of 2

ruthsteed
Autodesk
Autodesk

That depends on what you mean.  Do you mean you want to change the part color in the part itself or in the context of an assembly?  This sub will set the part color if the active document is a part, or the first occurrence's color if the active document is an assembly. It assumes that the "Gold" color style is not already in your document.

 

Sub PartColor()
    Dim oAsset As Asset
    Dim oLib As AssetLibrary: Set oLib = ThisApplication.AssetLibraries("Autodesk Appearance Library")
    Set oAsset = oLib.AppearanceAssets("Gold")
    If ThisApplication.ActiveDocumentType = kPartDocumentObject Then
        Dim oPart As PartDocument: Set oPart = ThisApplication.ActiveDocument
        oPart.ActiveAppearance = oAsset
    ElseIf ThisApplication.ActiveDocumentType = kAssemblyDocumentObject Then
        Dim oAsm As AssemblyDocument: Set oAsm = ThisApplication.ActiveDocument
        Set oAsset = oAsset.CopyTo(oAsm)
        Dim oOcc As ComponentOccurrence
        Set oOcc = oAsm.ComponentDefinition.Occurrences(1)
        oOcc.Appearance = oAsset
    End If
End Sub

 



Ruth Steed
Inventor Development Team
Autodesk, Inc.