How to override a solid body color from an assembly

How to override a solid body color from an assembly

sergio_duran
Advocate Advocate
1,462 Views
5 Replies
Message 1 of 6

How to override a solid body color from an assembly

sergio_duran
Advocate
Advocate

Hi everyone,

 

I would like to know how to override a solid body color from an assembly using either VBA (API) or VB.NET (iLogic).

 

There is another thread in which the color of bodies from a multi-body part are changed, then I adjusted it to do the same from the assembly but it changes the original body color rather than override it in the assembly level. In my code, I'm checking occurrence by occurrence, filtering only parts with more than one body, and finally, changing the color of solid bodies. It works well, but it changes the original color of every body.

 

Here is the thread 

http://forums.autodesk.com/t5/inventor-customization/how-do-i-change-the-color-of-a-solid-in-a-multi...

 

iLogic has this snippet (please see below) to override a component color in the assembly level but the original color will remain the same.

 

SyntaxEditor Code Snippet

Component.Color("PartA:1")

 

I'd like to do something similar for solid bodies

 

Thanks in advance!

0 Likes
1,463 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable

I've done something similar with Renderstyle. This is for a single part but I'm sure can be adapted for an assembly 

 

SyntaxEditor Code Snippet

'SET THIS PART AS STEEL (EN3B) BLACKENED 

Dim oPartDoc As PartDocument 
Dim oRenderStyle As RenderStyle 
Dim oMaterial As Material


'Set the active Document
oPartDoc = ThisApplication.ActiveDocument 

'Update material 
iProperties.Material=("070M20 (EN3B)")

'Change Part Style
Try 
    
    oRenderStyle = oPartDoc.RenderStyles.Item("Steel (Black)") 

    ' Assign the render style to the part. 
     oPartDoc.ActiveRenderStyle = oRenderStyle
    
    Catch
    
    MsgBox("Render Style cannot be found")
    
End Try

'Update Finish
Try
    iProperties.Value("Custom", "FINISH")="BLACKEN"
    
    Catch
    
    'Do Nothing
    
End Try

InventorVb.DocumentUpdate()

 

0 Likes
Message 3 of 6

sergio_duran
Advocate
Advocate
Thanks! but the question is about color/appearance for Solid Bodies
0 Likes
Message 4 of 6

Anonymous
Not applicable

Here's some iLogic code which I based on code from http://inventbetter.blogspot.com

 

' http://inventbetter.blogspot.com
Option Explicit

Sub Main
	Dim oAsmDoc as AssemblyDocument
	
	oAsmDoc = ThisApplication.ActiveDocument
	' Call the function which does the recursion
	
	Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences,1)
	
End Sub

Private Sub traverseAssembly(Occurrences as ComponentOccurrences, Level as Integer)
	' Iterate through all of the occurrences in this assembly.  Level 1 components
	
	Dim oOcc as ComponentOccurrence
	Dim oCompDef as ComponentDefinition

	Dim oAssy As Inventor.AssemblyDocument
	Dim oPart As Inventor.PartDocument

	Dim invCustomPropertySet As Inventor.PropertySet
	
	Dim sDescription As String 
		
	For Each oOcc in Occurrences
		
		If (InStr(oOcc.Name, "BareCable")) > 0 Then
			' We have a conductor !
			' Get the selected  item document occurrence name
			oCompDef = oOcc.Definition
			oPart = oCompDef.Document
	
			invCustomPropertySet = oPart.PropertySets.Item("Design Tracking Properties")
			sDescription = invCustomPropertySet.Item("Description").Value

			Select Case sDescription
				Case "1192.5 KCMIL 61 AAC"
					'MessageBox.Show("1195.5 KCMIL 61 AAC", "Cadet Blue")
					Component.Color(oOcc.Name) = "Cadet Blue"

				Case "1192.5 KCMIL 38/19 ACSS/TW (STD STR)"
					'MessageBox.Show("1192.5 KCMIL 38/19 ACSS/TW (STD STR)", "Yellow")
					Component.Color(oOcc .Name) = "Yellow"

				Case "636 KCMIL 37 AAC"
					Component.Color(oOcc .Name) = "Red"
				Case Else
					Component.Color(oOcc.Name) = "Purple"
			End Select  
			
		End If
		
		' Check to see if this occurrence represents a subassembly
		' and recursively call this function to traverse through it.
		If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
				Call traverseAssembly( oOcc.SubOccurrences, Level + 1)
		End If
	Next
End Sub

Could be helpful, or not.

 

 

 

0 Likes
Message 5 of 6

sergio_duran
Advocate
Advocate
yosso22,

Thanks! but the question is about color/appearance for Solid Bodies. I need to override colors of bodies from Multi-body parts
0 Likes
Message 6 of 6

Anonymous
Not applicable

Ahh...multi-body parts, missed that, apologize.

 

Here's a VBA example...that might help.

 

The asset VBA code came from the IV help file.

 

Option Explicit

Sub main()

If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
    Stop
End If

Dim assyDoc As AssemblyDocument
Set assyDoc = ThisApplication.ActiveDocument

Dim oAsset1 As Asset
Dim oAsset2 As Asset

On Error Resume Next
    Set oAsset1 = assyDoc.Assets.Item("Bamboo")
    Set oAsset2 = assyDoc.Assets.Item("Chrome - Polished")
    If Err Then
        On Error GoTo 0
        ' Failed to get the appearance in the document, so import it.
        
        ' Get an asset library by name.  Either the displayed name (which
        ' can changed based on the current language) or the internal name
        ' (which is always the same) can be used.
        Dim assetLib As AssetLibrary
        Set assetLib = ThisApplication.AssetLibraries.Item("Autodesk Appearance Library")
        'Set assetLib = ThisApplication.AssetLibraries.Item("314DE259-5443-4621-BFBD-1730C6CC9AE9")
        
        ' Get an asset in the library.  Again, either the displayed name or the internal
        ' name can be used.
        Dim libAsset As Asset
        Set libAsset = assetLib.AppearanceAssets.Item("Bamboo")
        
        ' Copy the asset locally.
        Set oAsset1 = libAsset.CopyTo(assyDoc)
        
        Set libAsset = assetLib.AppearanceAssets.Item("Chrome - Polished")
        Set oAsset2 = libAsset.CopyTo(assyDoc)
    End If
    On Error GoTo 0

Dim oDef As AssemblyComponentDefinition
Set oDef = assyDoc.ComponentDefinition

Dim oOcc As ComponentOccurrence

For Each oOcc In oDef.Occurrences

    Dim surBodies As SurfaceBodies
    Set surBodies = oOcc.SurfaceBodies
    
    Dim surBody As SurfaceBody
    For Each surBody In surBodies
        If surBody.Faces.Count > 3 Then
            surBody.appearance = oAsset1
        Else
            surBody.appearance = oAsset2
        End If
    Next
Next

End Sub
0 Likes