Thank you so much for your help on this @Ralf_Krieg , your code worked flawlessly.
I have made minor addition to include user input for the Model State. If anyone is
Sub Main
oDoc = ThisDoc.Document
'Saves this document
oDoc.Save
'Checks if the active document is an assembly
If oDoc.DocumentType = kAssemblyDocumentObject
'Gets the assembly occurrences
Dim oOccs As ComponentOccurrences = oDoc.ComponentDefinition.Occurrences
'Call the subprocedure to traverse the assembly
myModelState = InputBox("Enter your Model State Name", "iLogic - Model State""")
TraverseAssembly(oOccs, myModelState)
ChangeModelState(myModelState)
End If
iLogicVb.UpdateWhenDone = True
End Sub
'***************
Public Sub TraverseAssembly(oOccs As ComponentOccurrences,myModelState As String)
Dim oOcc As ComponentOccurrence
For Each oOcc In oOccs
If oOcc.Suppressed = False Then
'Occurrence Name (Display Name)
oOccName = oOcc.Name
Try
iProperties.Value(oOccName, "Custom", "Model_State:") = myModelState
Catch
'It won't be able to change Read-Only parts (File not checked out from the Vault)
MessageBox.Show("This is a Read Only: " & oOcc.Name, "Title")
End Try
If oOcc.DefinitionDocumentType = kAssemblyDocumentObject
TraverseAssembly(oOcc.Definition.Occurrences, myModelState)
End If
End If
Next
'Update Assembly Properties
iProperties.Value("Custom", "Model_State:") = myModelState
End Sub
Private Sub ChangeModelState(myModelState As String)
Dim oDoc As AssemblyDocument = ThisDoc.Document
Dim oOccs As ComponentOccurrences = oDoc.ComponentDefinition.Occurrences
Call ProcessOccs(oOccs, myModelState)
End Sub
Private Sub ProcessOccs(ByVal oOccs As ComponentOccurrences, myModelState As String)
Dim oOcc As ComponentOccurrence
For Each oOcc In oOccs
If oOcc.SubOccurrences.Count > 0 Then
Call ProcessOccs(oOccs, myModelState) 'recursive call in case of subassemblies
End If
If IsDerived(oOcc) Then
If oOcc.DefinitionDocumentType = kPartDocumentObject Then
Dim oOccCompDef As PartComponentDefinition= oOcc.Definition
Dim oOccDerivedPartComponents As DerivedPartComponents= oOccCompDef.ReferenceComponents.DerivedPartComponents
Dim oOccDerivedPartComponent As DerivedPartComponent
'Change Model State of each occurence
For Each oOccDerivedPartComponent In oOccDerivedPartComponents
Dim oOccDerivedPartCompDef As DerivedPartDefinition = oOccDerivedPartComponent.Definition
oOccDerivedPartCompDef.ActiveModelState = myModelState
oOccDerivedPartComponent.Definition = oOccDerivedPartCompDef
Next
End If
Else
'Code for non derived parts
End If
Next
End Sub
Private Function IsDerived(ByVal oOcc As ComponentOccurrence) As Boolean
Dim oDoc As Document = oOcc.Definition.Document
Dim oPartDoc As PartDocument
If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
oPartDoc = DirectCast(oDoc, PartDocument)
If oPartDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Count >0 Then
Return True
End If
End If
Return False
End Function
interested, here is the final code.