Thanks @JhoelForshav, but for some reason your code does nothing. However, I was able to come up with a solution that seem to be doing the task I wanted.
I first use one rule to take user input to capture the new Model State and place it as a custom iProperty and then use another rule to push this custom iProperty that has a new Model State name to all the sub-assemblies and parts. I have added a rule to all the sub-assemblies and parts to change Model State to what the custom iProperty has. And then I run another rule in my man assembly that runs this rule that is on each individual part. Below are snippets of all the rules. Let me know if there is a faster/better way of doing this.
Let me know if my explanation makes sense, can do a screencast to show how it works.
Step 1:
'Promt the user for the value of a variable
modelstate = InputBox("Please Enter the Model Sate Here", "Custom Property Input Box", "Eg. 21017-xxx")
'assign the value of that variable to a custom property
iProperties.Value("Custom", "Model_State:") = modelstate
Step 2:
Sub Main
'Gets the Active Document
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
oOccs = oDoc.ComponentDefinition.Occurrences
'Call the subprocedure to traverse the assembly
Call TraverseAssembly(oOccs)
End If
End Sub
'***************
Public Sub TraverseAssembly(oOccs As ComponentOccurrences)
'Integer to traverse the assembly occurrences
Dim i As Integer
'For i=1 To oOccs.Count
Dim oOcc As ComponentOccurrence
For Each oOcc In oOccs
'oOcc = oOccs.Item(i)
If oOcc.Suppressed = False
'Occurrence Name (Display Name)
oOccName = oOcc.Name
'Gets the first three leters of the Occurrence Name (e.g. "STK")
oOccPrefix = Left(oOccName, 3)
If oOccPrefix <> "STK"
Try
'
iProperties.Value(oOccName, "Custom", "Model_State:") = iProperties.Value("Custom", "Model_State:")
Catch
'It won't be able to change Read-Only parts (e.g. Content Center)
'MessageBox.Show("This is a content center file: " & oOcc.Name, "Title")
End Try
End If
If oOcc.DefinitionDocumentType = kAssemblyDocumentObject
Call TraverseAssembly(oOcc.SubOccurrences)
End If
End If
Next
End Sub
Step 3:
Sub Main
auto = iLogicVb.Automation
' Set Rule name
Dim ruleName As String
ruleName = "Update_Model_State"
' Get the active assembly.
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
' Get all of the referenced documents.
Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments
' Iterate through the list of documents.
Dim oRefDoc As Document
For Each oRefDoc In oRefDocs
Dim rule As Object
rule = auto.GetRule(oRefDoc, ruleName)
If (rule Is Nothing) Then
' Call MsgBox("No rule named " & ruleName & " was found in the document.")
Else
' MessageBox.Show(rule.Name, oRefDoc.DisplayName)
Dim i As Integer
i = auto.RunRuleDirect(rule)
End If
Next
End Sub
Here is the rule that is in the individual parts:
Dim doc As PartDocument = ThisDoc.Document
Dim comp As DerivedPartComponent = doc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Item(1)
Dim derivedDef = comp.Definition
Logger.Info("current model state = {0}", derivedDef.ActiveModelState)
Dim stateWanted = iProperties.Value("Custom", "Model_State:")
If (derivedDef.ActiveModelState <> stateWanted) Then
derivedDef.ActiveModelState = stateWanted
comp.Definition = derivedDef ' apply the modified definition back to the DerivedPartComponent
End If