Change color of sub assembly components while working in top level asy.

Change color of sub assembly components while working in top level asy.

Anonymous
Not applicable
1,554 Views
8 Replies
Message 1 of 9

Change color of sub assembly components while working in top level asy.

Anonymous
Not applicable

All,

 

     I am working on an iLogic rule to change the color of a sub assembly component while working in a top level asy.  This component could be 2 or 3 levels deep.  I have been able to successfully turn the visibility on or off for the component through all of the sub assemblies, but when I use a similar code to just change the component color, it doesn't work.  It is only changing the color if that component is in the top level.  Below is the code that I am working with.  Any help is greatly appreciated.  Thanks!

 

SyntaxEditor Code Snippet

 ' Rule toggles all components visibility
Dim oAsmDef As AssemblyComponentDefinition _
= ThisApplication.ActiveDocument.ComponentDefinition
Dim oList As ComponentOccurrencesEnumerator = oAsmDef.Occurrences.AllLeafOccurrences
For Each oOcc As ComponentOccurrence In oList


'Get the browser name of the component
oOccName = oOcc.Name
'Check if the desired bit of text is in the component's name
If InStr(oOccName, "80299") > 0 Then
If oOcc.Visible = True Then
Component.Color(oOccName) = "Yellow (Clear)"

Else

End If
End If

Next


   

0 Likes
Accepted solutions (1)
1,555 Views
8 Replies
Replies (8)
Message 2 of 9

Jef_E
Collaborator
Collaborator

You should read this blogpost. If there are more questions then I would be happy to help.

 

http://modthemachine.typepad.com/my_weblog/2009/03/accessing-assembly-components.html



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 3 of 9

Anonymous
Not applicable

Jef_E,

 

     I have referenced this blogpost before, while trying to figure out this ilogic rule, and even spoke with the writer of the post, but it still was not functional.  The writer of the post was not able to provide any other assistance or insight either.  I appreciate the help though.

 

Thanks!

0 Likes
Message 4 of 9

Jef_E
Collaborator
Collaborator

This seems to work for me?

 

Sub Main TraverseAssemblySample()
    ' Get the active assembly.
    Dim oAsmDoc As AssemblyDocument
     oAsmDoc = ThisApplication.ActiveDocument
	 
	 Dim oCount As Integer
	 oCount = 0

    ' Call the function that does the recursion.
    Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1, oCount)
	
	MsgBox(oCount)
End Sub

Private Sub TraverseAssembly(ByVal Occurrences As ComponentOccurrences, _
							 ByVal Level As Integer, _
							 ByRef oCount As Integer)
    ' Iterate through all of the occurrence in this collection.  This
    ' represents the occurrences at the top level of an assembly.
    For Each oOcc As ComponentOccurrence In Occurrences
		
		If oOcc.Name = "12992-X05_EarthingBoss_003:1" Then
		
			Component.Color(oOcc.Name) = "Yellow"

			oCount += 1
		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, oCount)
        End If
    Next
End Sub

 

Edits should be made where the text is red.

		If oOcc.Name = "12992-X05_EarthingBoss_003:1" Then
		
			Component.Color(oOcc.Name) = "Yellow"

			oCount += 1
		End If

I also count the number of occurrences that have their color change so you can see if it works.



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 5 of 9

Jef_E
Collaborator
Collaborator

Warning: by this method the color overwritten in the assembly that hold the part or assembly.

 

For example when you run the code from asm 1 (asm is short for assembly ) and the occurrence name you entered = part 1, you can clear the overwrite from part 1. When you run enter occurrence name part 4 you can only clear the overwrite from asm 2 NOT from asm 1

 

asm 1

- part 1

- part 2

- part 3

- asm 2

- - part 4

- - part 5

 



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 6 of 9

Anonymous
Not applicable

Jef_E,

 

     This is helping, but only in the top level assembly.  In some cases I may need to change a part of subasy that is 3 to 4 levels deep.  It seems like count function is looking at all levels, but the change color aspect is only changing in the top level.  For example, if I want to change the color of "Part 1" to red and the total qty of "Part 1" in the top level plus all subassemblies combined is (6), the count function returns 6, but the color change will only change the color of "Part 1" if it is in the very top level.  Do you know if there is something I missing, that is preventing the traverse from actually changing the color of the parts within the subassembly, or even deeper?  My current code, which is almost the same as what you sent me, is below.

 

Thanks!

 

 

SyntaxEditor Code Snippet

Sub Main TraverseAssemblySample()
' Get the active assembly.
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument

Dim oCount As Integer
oCount = 0

' Call the function that does the recursion.
Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1, oCount)

MsgBox(oCount)
End Sub

Private Sub TraverseAssembly(ByVal Occurrences As ComponentOccurrences, _
ByVal Level As Integer, _
ByRef oCount As Integer)
' Iterate through all of the occurrence in this collection. This
' represents the occurrences at the top level of an assembly.
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument


For Each oOcc As ComponentOccurrence In Occurrences

If Left(oOcc.Name,5) = "Part 1" Then

Component.Color(oOcc.Name) = "Red"

oCount += 1
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, oCount)
End If
Next



End Sub

0 Likes
Message 7 of 9

Jef_E
Collaborator
Collaborator
Accepted solution

Are you sure? I made a test with the code I posted.

Edit: I think your view representation is not associative.

 

 


Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
Message 8 of 9

Anonymous
Not applicable

Jef_E,

 

     The associative representation is what I was missing.  The rule is now working correctly.  

 

Thanks!

0 Likes
Message 9 of 9

Jef_E
Collaborator
Collaborator

Could you also mark the code post as solution?



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes