Assigning different colors to each feature

By Vladimir Ananyev

Q: How to assign feature color using Inventor’s API?

A: You may assign any render style to part features. These changes will be saved in the document file.

Sub AssignColorToFeatures()

  ‘Assign render style to part feature #1

 

  Dim oDoc As PartDocument = Nothing

  Try

    oDoc = TryCast(_oApp.ActiveDocument, PartDocument)

  Catch ex As Exception

    MsgBox("Please open a part document in Inventor")

    Exit Sub

  End Try

 

  Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition

  ‘Get reference to the render style object "MyFeatureStyle"

  ‘Create it if it doesn’t exist.  Color – green.

  Dim RS As RenderStyle

  Try

    RS = oDoc.RenderStyles.Item("MyFeatureStyle")

  Catch ex As
Exception

    RS = oDoc.RenderStyles.Add("MyFeatureStyle")

    RS.SetAmbientColor(0, 255, 0)  ‘Green

  End Try

  ‘assign new render style to part feature

  Dim oPartFeature As PartFeature = oDef.Features.Item(1)

  Call oPartFeature.SetRenderStyle(StyleSourceTypeEnum.kOverrideRenderStyle, RS)

End Sub

If you need temporary colors you may use also HighlightSet functionality. In this case you add all faces of the specified feature to the HighlightSet object with desired color. The color assigned through highlight set is temporary and feature colors will revert back to original when model refreshes.

Sub AssignRandomColorToFeatures()

  ‘Iterate through all features, add every feature faces

  ‘to different highlight set with random color.

 

  Dim oDoc As PartDocument = Nothing

  Try

    oDoc = TryCast(_oApp.ActiveDocument, PartDocument)

  Catch ex As Exception

    MsgBox("Please open a part document in Inventor")

    Exit Sub

  End Try

 

  Randomize()    ‘ Initialize random-number generator. 

  Dim oCompDef As PartComponentDefinition = oDoc.ComponentDefinition

 

  For Each oFeat As PartFeature In oCompDef.Features

    ‘ Generate random values between 1 and 255.

    Dim MyCol1 As Byte = CByte((255 * Rnd()))

    Dim MyCol2 As Byte = CByte((255 * Rnd()))

    Dim MyCol3 As Byte = CByte((255 * Rnd()))

 

    ‘Define a highlight set        

    Dim oStartHLSet As HighlightSet

    oStartHLSet = oDoc.CreateHighlightSet

    Dim oColor As Color = _oApp.TransientObjects.CreateColor(MyCol1, MyCol2, MyCol3)

    oColor.Opacity = 1  ‘ Set the opacity  

    oStartHLSet.Color = oColor

 

    ‘Add all faces of current feature to highlightset

    For Each oFace As Face In oFeat.Faces

      oStartHLSet.AddItem(oFace)

    Next

  Next

<

p style=”line-height: normal;margin: 0cm 0cm 0pt” class=”MsoNormal”>End Sub


Comments

2 responses to “Assigning different colors to each feature”

  1. Jean-Marc Avatar
    Jean-Marc

    Hello Valdimir,
    I am trying to highlight all feature of an assembly but it raises an exception when highlighting faces.
    Any idea?
    Here is the code
    ‘ Get all of the leaf occurrences of the assembly.
    Dim oLeafOccs As ComponentOccurrencesEnumerator
    oLeafOccs = oAsmDef.Occurrences.AllLeafOccurrences()
    For Each oOcc In oLeafOccs
    If oOcc.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
    Dim oPartDef As PartComponentDefinition
    oPartDef = oOcc.Definition
    For Each oFeature In oPartDef.Features
    highlightFeature(oFeature)
    Next
    End If
    Next
    Private Sub highlightFeature(ByRef inFeature As PartFeature)
    Dim oFeature As PartFeature
    oFeature = inFeature
    If Not _HLSetDetectionFeatureResult Is Nothing Then
    _HLSetDetectionFeatureResult.Clear()
    _HLSetDetectionFeatureResult = Nothing
    End If
    ‘ Create an higlight set
    _HLSetDetectionFeatureResult = _invApp.ActiveDocument.CreateHighlightSet
    ‘ Change the highlight color for the set to red.
    Dim oRed As Color
    oRed = _invApp.TransientObjects.CreateColor(180, 34, 34)
    ‘ Set the opacity
    oRed.Opacity = 0.6
    _HLSetDetectionFeatureResult.Color = oRed
    ‘ Add all start faces to the highlightset. Skip holes because
    ‘ they don’t support the StartFaces property.
    If oFeature.Type = ObjectTypeEnum.kHoleFeatureObject Then
    Dim oFace As Face
    For Each oFace In oFeature.StartFaces
    _HLSetDetectionFeatureResult.AddItem(oFace)
    Next
    End If
    ‘ Add all end faces to the highlightset.
    For Each oFace In oFeature.EndFaces
    _HLSetDetectionFeatureResult.AddItem(oFace)
    Next
    ‘ Add all end faces to the highlightset.
    For Each oFace In oFeature.SideFaces
    _HLSetDetectionFeatureResult.AddItem(oFace)
    Next
    End Sub

  2. Hi Jean-Marc,
    Working with part features in an assembly context you should use not PartFeature objects but their corresponding proxy objects.
    See VB.NET example below.
    Private Sub Test_HighLightProxyFaces()
    ‘ Module-level declarations:
    ‘Private _oApp As Inventor.Application
    ‘Private _oHLSet As HighlightSet
    Dim oAssyDoc As AssemblyDocument _
    = TryCast(_oApp.ActiveDocument, AssemblyDocument)
    Dim oAssyDef As AssemblyComponentDefinition _
    = oAssyDoc.ComponentDefinition
    ‘ Create a new highlight set for face(s).
    If _oHLSet Is Nothing Then
    _oHLSet = oAssyDoc.CreateHighlightSet
    Else
    _oHLSet.Clear()
    End If
    ‘ Change the highlight color for the set to red.
    Dim oRed As Color = _oApp.TransientObjects _
    .CreateColor(255, 0, 0)
    oRed.Opacity = 0.3 ‘ Set the opacity
    _oHLSet.Color = oRed
    ‘ Get all of the leaf occurrences of the assembly.
    Dim oLeafOccs As ComponentOccurrencesEnumerator _
    = oAssyDef.Occurrences.AllLeafOccurrences()
    For Each oOcc As ComponentOccurrence In oLeafOccs
    If oOcc.DefinitionDocumentType = _
    DocumentTypeEnum.kPartDocumentObject Then
    Dim oPartDef As PartComponentDefinition = cc.Definition
    For Each oFeature As PartFeature In oPartDef.Features
    ‘get proxy feature
    Dim oProxyFeature As Object = Nothing
    Call oOcc.CreateGeometryProxy(oFeature, oProxyFeature)
    Dim oFaces As Faces = Nothing
    Select Case oProxyFeature.Type
    Case ObjectTypeEnum.kHoleFeatureProxyObject
    Dim oFP As HoleFeatureProxy = oProxyFeature
    oFaces = oFP.Faces
    Case ObjectTypeEnum.kExtrudeFeatureProxyObject
    Dim oFP As ExtrudeFeatureProxy = oProxyFeature
    oFaces = oFP.Faces
    Case ObjectTypeEnum.kRevolveFeatureProxyObject
    Dim oFP As RevolveFeatureProxy = oProxyFeature
    oFaces = oFP.Faces
    ‘and all other appropriate cases
    End Select
    If oFaces IsNot Nothing Then
    For Each oFace As Face In oFaces
    Call _oHLSet.AddItem(oFace)
    Next
    End If
    Next ‘oFeature
    End If
    Next
    End Sub ‘Test_HighLightFaces

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading