Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Edit solid bodies color

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
TONELLAL
512 Views, 3 Replies

Edit solid bodies color

Hello,

Is it possible, via VB, to modify solidbodies color ?

Actually, I need to modify part color. But in some cases, the part color and the solidbody color are not the same. So I need to modify the Solidbody color, to match the part color.

I found the bodies in ComponentDefinition.SurfaceBodies, but I can't find something like ComponentDefinition.SurfaceBodies.Item(1).color.

3 REPLIES 3
Message 2 of 4

Dim oPartDoc As Inventor.PartDocument
    Set oPartDoc = ThisApplication.ActiveDocument
    
    Dim oPartDef As PartComponentDefinition
    Set oPartDef = oDoc.ComponentDefinition
    
    Dim oBody As SurfaceBody
    Set oBody = oPartDef.SurfaceBodies.Item(1)
    
    Dim ors As RenderStyle
    Set ors = oPartDoc.RenderStyles.Item("RED")
    
    Call oBody.SetRenderStyle(kOverrideRenderStyle, ors)

 Try this

Message 3 of 4
ekinsb
in reply to: TONELLAL

Public Sub SetBodyColor()
    Dim partDoc As PartDocument
    Set partDoc = ThisApplication.ActiveDocument
    
    Dim body As SurfaceBody
    For Each body In partDoc.ComponentDefinition.SurfaceBodies
        ' Get the Autodesk Material Library.
        Dim lib As AssetLibrary
        Set lib = ThisApplication.AssetLibraries.item("AD121259-C03E-4A1D-92D8-59A22B4807AD")
        
        ' Randomly generate an index for an item within the libary.
        Dim index As Long
        index = Int((lib.AppearanceAssets.Count * Rnd) + 1)
        
        ' Get the random appearance.
        Dim libAppearance As Asset
        Set libAppearance = lib.AppearanceAssets.item(index)
        
        ' Get the local version of the appearance, if it exists.
        Dim localAppearance As Asset
        On Error Resume Next
        Set localAppearance = partDoc.AppearanceAssets.item(libAppearance.Name)
        If Err.Number <> 0 Then
            ' Copy the asset into the part.
            Set localAppearance = libAppearance.CopyTo(partDoc)
        End If
        On Error GoTo 0
        
        ' Assign the appearance to the body.
        body.appearance = localAppearance
    Next
End Sub

 

The previous post will work.  The previous RenderStyle API is no longer officially supported as of Inventor 2014, but it does continue to work.  If you want to use the new appeaerances, the sample below illustrates that.

 

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 4 of 4
TONELLAL
in reply to: TONELLAL

Ok, I'll use the 1st code for 2012, then the 2nd for 2014...

Thanks for your help !

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report