Inventor API: Change the color of a selected face in an assembly document

Q:  How can I change the color of the face by selecting a face object from an assembly document?

A:  In an assembly document you should use the NativeObject of a face proxy object to obtain the original occurrence. By modifying the NativeObject’s color property you can modify the color of the face in the assembly.

Selecting face in an assembly context you really get FaceProxy object of this face. You need to use NativeObject property of this face proxy object to get reference to the original face. Then you are able to modify face color.

The following VBA example demonstrates how this is done.

Sub test()
    Dim oSelectSet As SelectSet
    Set oSelectSet = ThisApplication.ActiveDocument.SelectSet
   
    On Error Resume Next
    If oSelectSet.count <> 1 Then
        Exit Sub
    End If
   
    ‘ check if it’s a face
    Dim oFaceProxy As FaceProxy
    Set oFaceProxy = oSelectSet.Item(1)
   
    If oFaceProxy Is Nothing Then
        Exit Sub
    End If
    On Error GoTo 0
   
    ‘ get the native object, which resides in the definition of the occurrence
    Dim oFace As Face
    Set oFace = oFaceProxy.NativeObject
   
    ‘ get the document where the face belongs
    Dim oDoc As Document
    Set oDoc = oFace.Parent.ComponentDefinition.Document
   
‘ NOTE: make sure that the style "myColor" already

‘ exists in the document where the face belongs
‘ if it does not exist yet then this call will fail

 

    Dim oStyle As RenderStyle
    Set oStyle = oDoc.RenderStyles.Item("myColor") ‘your customized color
   
    Call oFace.SetRenderStyle(kOverrideRenderStyle, oStyle)
End Sub

 

by Vladimir Ananyev


Comments

4 responses to “Inventor API: Change the color of a selected face in an assembly document”

  1. William Avatar
    William

    Hello,
    I’m having problems getting this to work in an Inventor 2013 .iam file. Could you provide more detailed information so I can solve my problem. Thanks

  2. William Avatar
    William

    What code needs modified to work on an iAssembly?
    Thanks

  3. Hi William,
    Possible reason could be the absence of required render style in the native document. Let’s move this discussion to the Inventor Customization Forum
    http://forums.autodesk.com/t5/Autodesk-Inventor-Customization/bd-p/120
    Could you post your simplest assembly and used code for test purposes.
    Thank you,
    Vladimir

  4. Michal Miro Avatar
    Michal Miro

    It seems that the described method is no longer available and the way to set the color of a face is to assign an attribute set containing proper attribute to a face. However, I found out how Inventor does it itself, I got stuck with AppearanceSourceType – how to change kFeatureAppearance to kOverrideAppearance?

Leave a Reply to WilliamCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading