- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi all,
I want to write an iLogic code that will do the following actions:
1. I will give a list of part names as input
2. The code will parse through a big assembly file (that contains sub-assemblies) to find the parts, and color all of them to a certain color
I have a working version for a code that works for a simple assembly where there's no sub-assembly:
' iLogic rule To color parts In an assembly Using Partial part names
' List of part names (or partial names) to color
Dim partsToColor As String() = {"SAG3YZM5" } ' Add other part names as needed
Dim partsToColorCount As Integer = partsToColor.Length ' Count the number of elements in list so we can set the loop size
Dim colorStyleName As String = "Magenta" ' Replace with desired color style name
' Loop through each component in the assembly
Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument
Dim oCompOccs As ComponentOccurrences
oCompOccs = oAssyDoc.ComponentDefinition.Occurrences
For Each oOcc As ComponentOccurrence In oCompOccs
' Check if the component name contains any of the specified part names
For Index = 1 To partsToColorCount
If oOcc.Name.Contains(partsToColor(Index - 1)) Then 'Subtract 1 since the index of the first element in the array is 0
' Try to set the color of the component
Try
Component.Color(oOcc.Name) = colorStyleName
MessageBox.Show("Colored component: " & oOcc.Name)
Catch
MessageBox.Show("Error setting color for component: " & oOcc.Name)
End Try
End If
Next
NextHowever, when I try to expand this to bigger assemblies, I couldn't get the code to work:
' iLogic rule To color parts In an assembly Using Partial part names
Sub Main
' List of part names (or partial names) to color
Dim partsToColor As String() = {"SAG3YZM5" } ' Add other part names as needed
Dim partsToColorCount As Integer = partsToColor.Length ' Count the number of elements in list so we can set the loop size
Dim colorStyleName As String = "Magenta" ' Replace with desired color style name
' Loop through each component in the assembly
Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument
oAssyDocType = oAssyDoc.DocumentType
If oAssyDocType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MessageBox.Show("This process only runs on assemblies.")
Exit Sub
Else
Call ColorAssembly
End If
Dim oCompOccs As ComponentOccurrences
oCompOccs = oAssyDoc.ComponentDefinition.Occurrences
End Sub
Sub ColorAssembly(AssyDocument As AssemblyDocument, FileList())
For Each oOcc As ComponentOccurrence In oCompOccs
If oOcc Is On FileList() Then
Call ColorPart
ElseIf oOcc = Assembly
Call ColorAssembly()
End If
Next
End Sub
Sub ColorPart(OccurenceInAssy As Occurrence)
' Loop through each component in the assembly
Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument
Dim oCompOccs As ComponentOccurrences
oCompOccs = oAssyDoc.ComponentDefinition.Occurrences
For Each oOcc As ComponentOccurrence In oCompOccs
' Check if the component name contains any of the specified part names
For Index = 1 To partsToColorCount
If oOcc.Name.Contains(partsToColor(Index - 1)) Then 'Subtract 1 since the index of the first element in the array is 0
' Try to set the color of the component
Try
Component.Color(oOcc.Name) = colorStyleName
MessageBox.Show("Colored component: " & oOcc.Name)
Catch
MessageBox.Show("Error setting color for component: " & oOcc.Name)
End Try
End If
Next
Next
End SubHere, the code will:
1. Check if an occurrence is a part or assembly
2. If it's a part, then execute the ColorPart subroutine; if it's an assembly, call ColorAssembly subroutine, where it goes into the assembly and look for the parts.
I couldn't get the code to work, and I'm fairly new to VBA. Could anyone help me with my issue?
Thank you so much!
Solved! Go to Solution.