Set occurrence override colors

By Xiaodong Liang

The post on modthemachine introduces how to control colors of part and the occurrence of assembly with an existing render style. But it does not affect the active render style of the document of the occurrence. The code below sets  occurrence render style with a new one. It also shows how to do if you want change the active render style of the occurrence’s document.

 

' applies render style to defintion of component
' occurrence
Sub OverrideRenderStyleToOccurrence()
 
Const kStyleName = "MyRS"
  
  ' assume we have had Inventor application
  Dim oDoc As AssemblyDocument
  On Error Resume Next
  oDoc = _InvApplication.ActiveDocument
 
   If (oDoc.DocumentType 
      DocumentTypeEnum.kAssemblyDocumentObject) Then
        MsgBox("There must be an active assembly" + _
           “document. Exiting ...")
            Exit Sub
   End If
 
   ' get occurrence from selection
  Dim oSelectSet As SelectSet
 
   oSelectSet = oDoc.SelectSet
   If oSelectSet.Count  1 Then
         MsgBox("A component must be selected." + _
            "Exiting ...")
            Exit Sub
    End If
    Dim oOccurrence As ComponentOccurrence
 
    oOccurrence = oSelectSet.Item(1)
    
    ' get source doc of occurrence
    Dim oOccDoc As Document
    oOccDoc = oOccurrence.Definition.Document
    ' create new render style
    Dim oStyle As RenderStyle
 
    oStyle = oDoc.RenderStyles(kStyleName)
    If oStyle Is Nothing Then
          oStyle = oDoc.RenderStyles.Add(kStyleName)
    End If
    oStyle.SetAmbientColor(50, 100, 200)
    oStyle.SetDiffuseColor(50, 100, 200)
    oStyle.SetEmissiveColor(50, 100, 200)
    oStyle.SetSpecularColor(50, 100, 200)
 
    ' this does not affect the active render style 
    ' (real style) of the occurrence's document
    oOccurrence.SetRenderStyle(
     StyleSourceTypeEnum.kOverrideRenderStyle,
            oStyle)
 
    ' you can change the active renderstyle
    ' if this occurrence is from a part
    'If (TypeOf oOccDoc Is PartDocument) Then
        '    Dim oPartDoc As PartDocument = oOccDoc
        '    oPartDoc.ActiveRenderStyle = oStyle
     'End If
 
  ' comment following line, if you do not want   ‘to save document
        'oPartDoc.Save()
End Sub

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading