Face color change random. Inventor

Face color change random. Inventor

Anonymous
Not applicable
1,953 Views
18 Replies
Message 1 of 19

Face color change random. Inventor

Anonymous
Not applicable
  1. My work is sheet metal. When i assembly all part than i don't understand separate separate part. because every part looking white (or grey). I want randomly change face color. From ilogic or any other way. Can help any one?
0 Likes
Accepted solutions (2)
1,954 Views
18 Replies
Replies (18)
Message 2 of 19

Stakin
Collaborator
Collaborator

@Anonymous wrote:
  1. My work is sheet metal. When i assembly all part than i don't understand separate separate part. because every part looking white (or grey). I want randomly change face color. From ilogic or any other way. Can help any one?

If your assembly didn't have subassembly ,try this

 

 

 

Sub Main()
    Dim asmDoc As AssemblyDocument
    asmDoc = ThisApplication.ActiveDocument
	Dim orendeecount As Long
    Dim localAsset As Asset  
        Dim libAsset As Asset
		orendeecount = ThisApplication.ActiveAppearanceLibrary.AppearanceAssets.Count
		Dim oRandItem As Double
		Dim occ As ComponentOccurrence
		For Each occ  In asmDoc.ComponentDefinition.Occurrences
		oRandItem = Round((orendeecount - 2) * Rnd() + 1, 0)
		libAsset = ThisApplication.ActiveAppearanceLibrary.AppearanceAssets.Item(oRandItem)       
        Try
			localAsset = libAsset.CopyTo(asmDoc)
		Catch		
		End Try
		occ.Appearance = localAsset
		Next           
End Sub

 

 

0 Likes
Message 3 of 19

Ralf_Krieg
Advisor
Advisor

Hello

 

@Stakin 

Your Code will fail if Asset already exists in the local assembly.

 

@Anonymous

my alternative suggestion:

Dim oApp As Inventor.Application = ThisApplication
Dim oAssDoc As AssemblyDocument = oApp.ActiveDocument

Dim iIndex As Integer
Dim oAsset As Asset
Dim oLocalAsset As Asset
Dim oRefedDoc As Document

For Each oRefedDoc In oAssDoc.ReferencedDocuments
    'generate a random item number
    iIndex = Int(oApp.ActiveAppearanceLibrary.AppearanceAssets.Count * Rnd + 1)
    
    'get indexed item in asset library
    oAsset = oApp.ActiveAppearanceLibrary.AppearanceAssets.Item(iIndex)
    
    'copy asset to local document if not already there
	Try
        oLocalAsset = oAsset.CopyTo(oAssDoc, True)
	Catch
		oLocalAsset = oAssDoc.AppearanceAssets.Item(oAsset.Name)
	End Try
    
    'get all occurrences of the current document (all occurrences of a part are same color)
    Dim oOccs As ComponentOccurrencesEnumerator
    oOccs = oAssDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oRefedDoc)
    
    'assign asset
    Dim oOcc As ComponentOccurrence
    For Each oOcc In oOccs
        oOcc.Appearance = oLocalAsset
    Next
	
	oLocalAsset=Nothing
Next

 

This script will color all occurrences of one part in same color.


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 4 of 19

Stakin
Collaborator
Collaborator

Your code return HRESULT:0x80070057 (E_INVALIDARG) error, in ilogic window

 

0 Likes
Message 5 of 19

Ralf_Krieg
Advisor
Advisor

Ok, try this one

 

Dim oApp As Inventor.Application = ThisApplication
Dim oAssDoc As AssemblyDocument = oApp.ActiveDocument

Dim iIndex As Integer
Dim oAsset As Asset
Dim oLocalAsset As Asset
Dim oRefedDoc As Document

For Each oRefedDoc In oAssDoc.ReferencedDocuments
	
    'generate a random item number
    iIndex = Int(oApp.ActiveAppearanceLibrary.AppearanceAssets.Count * Rnd + 1)
    
    'get indexed item in asset library
    oAsset = oApp.ActiveAppearanceLibrary.AppearanceAssets.Item(iIndex)
    
    'copy asset to local document if not already there
	Try
        oLocalAsset = oAsset.CopyTo(oAssDoc, True)
	Catch
		For Each oLocalAsset In oAssDoc.AppearanceAssets
			If oLocalAsset.Name = oAsset.Name Then Exit For
		Next
	End Try
    
    'get all occurrences of the current document (all occurrences of a part are same color)
    Dim oOccs As ComponentOccurrencesEnumerator
    oOccs = oAssDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oRefedDoc)
    
    'assign asset
    Dim oOcc As ComponentOccurrence
    For Each oOcc In oOccs
        oOcc.Appearance = oLocalAsset
    Next
	
	oLocalAsset=Nothing
Next

R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 6 of 19

Stakin
Collaborator
Collaborator

Can you try the below code? I don't install the inventor on this computer.😥.thanks

Sub Main()
    Dim asmDoc As AssemblyDocument
    asmDoc = ThisApplication.ActiveDocument
    Dim orendeecount As Long
    Dim oAsset As Asset
    Dim oAsset_Array As New ArrayList
    For Each oAsset In ThisApplication.ActiveAppearanceLibrary.AppearanceAssets
        oAsset_Array.Add(oAsset.Name)
        oAsset_Array.Sort()
    Next

    Dim localAsset As Asset  
    Dim libAsset As Asset
Dim oAssName As String
		orendeecount = Asset_Array.Size()
		Dim oRandItem As Double
		Dim occ As ComponentOccurrence
		For Each occ  In asmDoc.ComponentDefinition.Occurrences
		oRandItem = Round((orendeecount - 2) * Rnd() + 1, 0)
		oAssName=Asset_Array.Item(oRandItem)
                libAsset = ThisApplication.ActiveAppearanceLibrary.AppearanceAssets.Item(oAssName)       
        Try
			localAsset = libAsset.CopyTo(asmDoc)
		Catch	
                        localAsset = asmDoc.AppearanceAssets.Item(oAssName)
		End Try
		occ.Appearance = localAsset
		Next           
End Sub

 

0 Likes
Message 7 of 19

Stakin
Collaborator
Collaborator
Accepted solution

Try this one,run perfectly

Sub Main()
    Dim asmDoc As AssemblyDocument
    asmDoc = ThisApplication.ActiveDocument
    Dim orendeecount As Long
    Dim oAsset As Asset
    Dim oAsset_Array As New ArrayList
    For Each oAsset In ThisApplication.ActiveAppearanceLibrary.AppearanceAssets
        oAsset_Array.Add(oAsset.Name)
    Next
    Dim localAsset As Asset  
    Dim libAsset As Asset
	Dim oAssName As String
		orendeecount = oAsset_Array.Count()
	Dim oRandItem As Double
	Dim occ As ComponentOccurrence
	For Each occ  In asmDoc.ComponentDefinition.Occurrences
		oRandItem = Round((orendeecount - 2) * Rnd() + 1, 0)
		oAssName = oAsset_Array.Item(oRandItem)	
		libAsset = ThisApplication.ActiveAppearanceLibrary.AppearanceAssets.Item(oAssName)
		
		Try
			localAsset = libAsset.CopyTo(asmDoc)
		Catch	
		        localAsset = asmDoc.Assets.Item(libAsset.DisplayName)
		End Try
		occ.Appearance = localAsset
	Next           
End Sub
0 Likes
Message 8 of 19

Anonymous
Not applicable

Thank you. Yes this is perfect for Assembly.  And do you know how i can change a part faces color randomly?  If you know plz tell me.

0 Likes
Message 9 of 19

Stakin
Collaborator
Collaborator

Assembly Doc,Try below code,the code is more simple。:) 

Sub Main()
    Dim asmDoc As AssemblyDocument
    asmDoc = ThisApplication.ActiveDocument
	Dim orendeecount As Long
    Dim localAsset As Asset  
        Dim libAsset As Asset
		orendeecount = ThisApplication.ActiveAppearanceLibrary.AppearanceAssets.Count
		Dim oRandItem As Double
		Dim occ As ComponentOccurrence
		For Each occ  In asmDoc.ComponentDefinition.Occurrences
		oRandItem = Round((orendeecount - 2) * Rnd() + 1, 0)
		libAsset = ThisApplication.ActiveAppearanceLibrary.AppearanceAssets.Item(oRandItem)       
        Try
			localAsset = libAsset.CopyTo(asmDoc)
		Catch	
                      localAsset = asmDoc.Assets.Item(libAsset.DisplayName)	
		End Try
		occ.Appearance = localAsset
		Next           
End Sub

 

 



fsd

0 Likes
Message 10 of 19

Stakin
Collaborator
Collaborator
Accepted solution

Part face color change,try the code below:

Sub main()
Dim oDoc As PartDocument = ThisDoc.Document
Dim oStyle As RenderStyle 
Dim oRscount As Double
oRscount = oDoc.RenderStyles.Count
Dim oRandItem As Double
Dim oStyle_Array As New ArrayList
For Each oStyle In oDoc.RenderStyles
    oStyle_Array.Add(oStyle.Name)
Next	
For Each oSolid As SurfaceBody In oDoc.ComponentDefinition.SurfaceBodies
	For Each oFace As Face In oSolid.Faces
		oRandItem = Round((oRscount - 2) * Rnd() + 1, 0)
		oStyle= oDoc.RenderStyles(oStyle_Array(oRandItem))
		oFace.SetRenderStyle(kOverrideRenderStyle, oStyle)
	Next
Next
End Sub

enjoy! 

Message 11 of 19

Anonymous
Not applicable

Thank you too much. After all i got it. Your many many thanks. Can i set,  collect materials from favorite or custom library?

0 Likes
Message 12 of 19

Anonymous
Not applicable
Sorry. My last question no need to answer. I got answer.
If appearance section i tik favourites than collect materials from favorite. If appearance section i tik my customs library than collect materials from customs library. Thank you. Thank you.
0 Likes
Message 13 of 19

Anonymous
Not applicable
Thank you
0 Likes
Message 14 of 19

mecanicu
Advocate
Advocate
Kudos for this code.
I tried this code on a multi body part and it's working as expected but I would like to know if it's possible to modify this code to exclude first two body (or specific body by name) and if it's possible to use a specific set of custom appearances (a custom library or a user list)
Thank you!
0 Likes
Message 15 of 19

Stakin
Collaborator
Collaborator

@mecanicu wrote:
Kudos for this code.
I tried this code on a multi body part and it's working as expected but I would like to know if it's possible to modify this code to exclude first two body (or specific body by name) and if it's possible to use a specific set of custom appearances (a custom library or a user list)
Thank you!

Change by body index

Sub main()
Dim oDoc As PartDocument = ThisDoc.Document
Dim oChangeBodyIndex As Integer
oChangeBodyIndex=1
Call oChangeBodyColorByIndex(oDoc,oChangeBodyIndex)
End Sub

Private Function oChangeBodyColorByIndex(oDoc As PartDocument,oBodyIndex As Integer)
Dim oStyle As RenderStyle 
Dim oRscount As Double
oRscount = oDoc.RenderStyles.Count
Dim oRandItem As Double
Dim oStyle_Array As New ArrayList
For Each oStyle In oDoc.RenderStyles
    oStyle_Array.Add(oStyle.Name)
Next	
	For Each oFace As Face In oDoc.ComponentDefinition.SurfaceBodies.Item(oBodyIndex).Faces
		oRandItem = Round((oRscount - 2) * Rnd() + 1, 0)
		oStyle= oDoc.RenderStyles(oStyle_Array(oRandItem))
		oFace.SetRenderStyle(kOverrideRenderStyle, oStyle)
	Next
end Function

change color by body Name

Sub main()
Dim oDoc As PartDocument = ThisDoc.Document
Dim oBodyName As String
oBodyName="Solid1"
Call oChangeBodyColorByName(oDoc,oBodyName)
End Sub

Private Function oChangeBodyColorByName(oDoc As PartDocument,oBodyName As String)
Dim oStyle As RenderStyle 
Dim oRscount As Double
oRscount = oDoc.RenderStyles.Count
Dim oRandItem As Double
Dim oStyle_Array As New ArrayList
For Each oStyle In oDoc.RenderStyles
    oStyle_Array.Add(oStyle.Name)
Next	
For Each oSolid As SurfaceBody In oDoc.ComponentDefinition.SurfaceBodies
	If oSolid.Name=oBodyName then
	For Each oFace As Face In oSolid.Faces
		oRandItem = Round((oRscount - 2) * Rnd() + 1, 0)
		oStyle= oDoc.RenderStyles(oStyle_Array(oRandItem))
		oFace.SetRenderStyle(kOverrideRenderStyle, oStyle)
	Next
	end if
Next
end Function

 

 

 

Message 16 of 19

mecanicu
Advocate
Advocate
Hi and thanks' for fast replay.
I don't think I explained exactly what I needed. I don't want to change face colors for "Solid1" or index 1.
I want Solid 1 and Solid2 to remain the same (original color, exclude them from face color change) and change the rest of solids... Solid3 Solid4 Solid5 ....Solid 45.
As for colors it would be ideal if i can specify what to use by appearances name. i still want them random apply on faces but only for the set i specify ( ideal 4. ex. red, blue, green, orange)
Hope I made myself understood this time.... Tx
0 Likes
Message 17 of 19

Stakin
Collaborator
Collaborator

you can make a loop to change the color of solid by name or index,exclude the solid1 and solid 2

Sub main()
Dim oDoc As PartDocument = ThisDoc.Document

For Each osolid As surfacebody in odoc.componentDefinition.surfacebadies
if osolid.Name="Solid1" or osolid.Name="Solid2" then
continue For
else
Call oChangeBodyColorByName(oDoc,osolid.Name)
end if
next
End Sub

Private Function oChangeBodyColorByName(oDoc As PartDocument,oBodyName As String)
Dim oStyle As RenderStyle 
Dim oRscount As Double
oRscount = oDoc.RenderStyles.Count
Dim oRandItem As Double
Dim oStyle_Array As New ArrayList
For Each oStyle In oDoc.RenderStyles
    oStyle_Array.Add(oStyle.Name)
Next	
For Each oSolid As SurfaceBody In oDoc.ComponentDefinition.SurfaceBodies
	If oSolid.Name=oBodyName then
	For Each oFace As Face In oSolid.Faces
		oRandItem = Round((oRscount - 2) * Rnd() + 1, 0)
		oStyle= oDoc.RenderStyles(oStyle_Array(oRandItem))
		oFace.SetRenderStyle(kOverrideRenderStyle, oStyle)
	Next
	end if
Next
end Function

 

 

0 Likes
Message 18 of 19

mecanicu
Advocate
Advocate
Not working! Error "Public member 'surfacebadies' on type 'PartComponentDefinition' not found."
0 Likes
Message 19 of 19

Stakin
Collaborator
Collaborator

sorry,

please modify the "surfacebadies" to "surfacebodies"