RGB Color Face wih VBA

RGB Color Face wih VBA

Anonymous
Not applicable
3,358 Views
8 Replies
Message 1 of 9

RGB Color Face wih VBA

Anonymous
Not applicable
Hello

Please I need your help ..how can I pait a Extrude face with a RGB color en VBA, .... I don know use the method, I try but it does not work

Public Sub Pinta()

Dim oFace As Face
Set oFace = oExtrude.EndFaces.Item(1) 'The face

Dim oRed As Color
Set oRed = ThisApplication.TransientObjects.CreateColor(200, 12, 100) 'RGB values

Dim oStyle As RenderStyle
Set oStyle = oPartDoc.RenderStyles.oRed

Call oFace.SetRenderStyle(kOverrideRenderStyle, oStyle)

End Sub



Please help me
Thanks
0 Likes
Accepted solutions (1)
3,359 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable

Sorry, I meant to "paint a face"

 

Thank you for your answers.

0 Likes
Message 3 of 9

LishuangLu
Autodesk
Autodesk

Here's a snippet of VBA code that can change the face's color into Red. Note, here I'm  using the Appearance name for accessing the RenderStyle, we could not assign color directly as RenderStyle. Let me know whether it works for you. 

 

Public Sub Pinta()

    Dim oPartDoc As PartDocument
    Set oPartDoc = ThisApplication.ActiveDocument
   
    Dim oCompDef As PartComponentDefinition
    Set oCompDef = oPartDoc.ComponentDefinition
   
    Dim oExtrude As ExtrudeFeature
    Set oExtrude = oCompDef.Features.ExtrudeFeatures(1)
   
    Dim oFace As Face
    Set oFace = oExtrude.EndFaces.Item(1) 'The face
    
    Dim oStyle As RenderStyle
    Set oStyle = oPartDoc.RenderStyles("Red")
   
    Call oFace.SetRenderStyle(kOverrideRenderStyle, oStyle)

End Sub

 

Thanks,

-Lisa

 

0 Likes
Message 4 of 9

Anonymous
Not applicable

Hi, thank you very much for replying.
The code works to paint a "red" color face.
But I'm trying to paint the face with a color with RGB parameters, create colors with RGB parameters from the vba.
Now I am trying to create new appearances, in case it resolves I will post the code for those who have the same problem.

 

Regards

0 Likes
Message 5 of 9

LishuangLu
Autodesk
Autodesk
Accepted solution

Here's the code sample for creating appearance with RGB value and assign to face.

 


Public Sub Pinta()

    Dim oPartDoc As PartDocument
    Set oPartDoc = ThisApplication.ActiveDocument
   
    Dim oCompDef As PartComponentDefinition
    Set oCompDef = oPartDoc.ComponentDefinition
   
    Dim oTG As TransientObjects
    Set oTG = ThisApplication.TransientObjects
   
    Dim oAssets As Assets
    Set oAssets = oPartDoc.Assets

    Dim oAsset As Asset
    Set oAsset = oAssets.Add(AssetTypeEnum.kAssetTypeAppearance, "Generic", "Test" & "Appearances", "Test")

    Dim generic_color As ColorAssetValue
    Set generic_color = oAsset.Item("generic_diffuse")
      
    generic_color.Value = oTG.CreateColor(200, 12, 100)
   
    Dim oExtrude As ExtrudeFeature
    Set oExtrude = oCompDef.Features.ExtrudeFeatures(1)
   
    Dim oFace As Face
    Set oFace = oExtrude.EndFaces.Item(1) 'The face
   
    oFace.appearance = oAsset

End Sub

 

Thanks,

-Lisa

Message 6 of 9

Anonymous
Not applicable

 

Thanks for the reply, it's perfect.
Well here another option in case anyone needs it. I found it in the help of Autodesk Inventor.

 

 

Public Sub Pinta()

 

 Dim doc As Document
     Set doc = ThisApplication.ActiveDocument

 

' Only document appearances can be edited, so that's what's created.
' This assumes a part or assembly document is active.

 

Dim docAssets As Assets
    Set docAssets = doc.Assets

 

' Create a new appearance asset.
Dim appearance As Asset
    Set appearance = docAssets.Add(kAssetTypeAppearance, "Generic", "Womyx", "Color1")

Dim tobjs As TransientObjects
    Set tobjs = ThisApplication.TransientObjects

 

Thanks for the reply, it's perfect.
Well here another option in case anyone needs it.

 

Dim Color As ColorAssetValue

Set Color = appearance.Item("generic_diffuse")
Color.Value = tobjs.CreateColor(Rvalorx, Gvalorx, Bvalorx)


Dim floatValue As FloatAssetValue
    Set floatValue = appearance.Item("generic_reflectivity_at_0deg")
    floatValue.Value = 0


    Set floatValue = appearance.Item("generic_reflectivity_at_90deg")
     floatValue.Value = 0

 

Dim oFace As Face
    Set oFace = oExtrude.EndFaces.Item(1) 'Selecciono la cara

Dim oStyle As RenderStyle
    Set oStyle = oPartDoc.RenderStyles.Item("Color1")
    Call oFace.SetRenderStyle(kOverrideRenderStyle, oStyle)

 

End Sub

 

Thank you very much ^_^

0 Likes
Message 7 of 9

ekinsb
Alumni
Alumni

There's also one more option that can work depending on what your specific use case is and this one doesn't use appearances.  You can use client graphics to create a copy of the face and directly color the graphics just using an rgb value.  Below is an example.

 

Public Sub SetFaceColor()
    Dim partDoc As PartDocument
    Set partDoc = ThisApplication.ActiveDocument
    
    ' Check to see if the client graphics already exist and delete them if they do.
    On Error Resume Next
    Dim graphics As ClientGraphics
    Set graphics = partDoc.ComponentDefinition.ClientGraphicsCollection.Item("ColorTest")
    If Err.Number = 0 Then
        graphics.Delete
        ThisApplication.ActiveView.Update
        Exit Sub
    End If
    On Error GoTo 0
    
    Dim selectedFace As Face
    Set selectedFace = ThisApplication.CommandManager.Pick(kPartFaceFilter, "Select a face")
            
    ' They don't exist so create them.
    Set graphics = partDoc.ComponentDefinition.ClientGraphicsCollection.Add("ColorTest")
    Dim node As GraphicsNode
    Set node = graphics.AddNode(1)
    
    ' Create surface graphics using the selected face.
    Dim surfGraphics As SurfaceGraphics
    Set surfGraphics = node.AddSurfaceGraphics(selectedFace)
    
    ' Set the priority so that it will display on top of the real face.
    surfGraphics.DepthPriority = 3
    
    ' Define the color using rgb values.
    surfGraphics.color = ThisApplication.TransientObjects.CreateColor(255, 10, 10, 1)
    
    ' Refresh the view.
    ThisApplication.ActiveView.Update
End Sub

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 8 of 9

Anonymous
Not applicable

Thanks for the reply.


I am painting faces of various RGB colors, now I have the problem with the light colors, the white ones are painted gray and when I modify the light in the style the dark colors become clear.

Is there a way to see the actual colors in Autodesk Inventor?

 

Thank you very much

0 Likes
Message 9 of 9

ekinsb
Alumni
Alumni

The way a model is rendered is much more complex than displaying simple colors.  The rendering used is not simple Gouraud or Phong shading and there are a lot of other variables that affect the final result.  The best thing I can recommend is to use the appearance editor and play with different type of appearances and settings for those appearances and see which one gets you the closest to your desired result.  The client graphics option option I gave you might also give better results in this case because it uses a simple rendering model.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog