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

Leave a Reply