@Jack_DuttonTM2JK, have a look at these examples.
The challenge to set a MS in all levels comes from the following:
- we can't push MS changes down more then one level in an assembly
- we can't just open the document and set the MS without updating the MS in the assembly it is used
So the 3rd example attempts to chain all this together, but I've not tested it well to see where it might run into issues. I left a lot of logger lines in it for you to try and help debug should you run into issues testing this, but in production it would be better to comment those out since writing lots of log lines can slow things down.
Hope this helps,
Curtis
1) Activate MS at document level
Dim IsVisible As Boolean = InputRadioBox("Turn all Model States On/Off", "On", "Off", False, "iLogic")
Dim oDoc As Document = ThisDoc.Document.ComponentDefinition.FactoryDocument
Dim oModelStates As ModelStates = oDoc.ComponentDefinition.ModelStates
If IsVisible = True
oModelStates.Item("ModelState2").Activate
ElseIf IsVisible = False
oModelStates.Item("[Primary]").Activate
End If
2) Activate MS at first assembly level
( probably no reason to use this version, but it does provide a cleaner example of what is needed to work with MSs from the assembly)
iLogicVb.UpdateWhenDone = True
Dim IsVisible As Boolean = InputRadioBox("Turn all Model States On/Off", "On", "Off", False, "Model State Toggle")
Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisDoc.Document
Dim oDoc As PartDocument
For Each oOcc As ComponentOccurrence In oAssyDoc.ComponentDefinition.Occurrences.AllLeafOccurrences
If oOcc.Definition.Document.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
oDoc = oOcc.Definition.Document
oDoc.Update2(False)
If IsVisible = True
oOcc.ActiveModelState = "ModelState2"
ElseIf IsVisible = False
oOcc.ActiveModelState = "[Primary]"
End If
End If
Next
3) Activate MS at all levels
(Since we can't push MS changes down more then one level in an assembly, we need open the document and set the MS, and then set the MS in the assembly it is used in)
Sub Main
Dim IsVisible As Boolean = InputRadioBox("Select", "MS2", "Primary", False, "Model State Toggle")
Dim oDoc As AssemblyDocument = ThisDoc.Document
'activate ModelState in the top level
If IsVisible = True
ThisDoc.ActiveModelState = "ModelState2"
ElseIf IsVisible = False
ThisDoc.ActiveModelState = "[Primary]"
End If
oMSName = ThisDoc.ActiveModelState
Call TraverseAssembly(oDoc.ComponentDefinition.Occurrences, oMSName)
InventorVb.DocumentUpdate()
End Sub
Dim ProcesssedDocumentsList As New ArrayList
Sub TraverseAssembly(Occurrences As ComponentOccurrences, oMSName As String)
Dim oDoc As AssemblyDocument
Dim oOcc As ComponentOccurrence
For Each oOcc In Occurrences
If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
Call TraverseAssembly(oOcc.SubOccurrences, oMSName)
End If
'get the parent, if it exists, open it and activate the model states in it
If oOcc.ParentOccurrence IsNot Nothing Then
Logger.Info("oOcc: " & oOcc.Name)
Logger.Info("parent: " & oOcc.ParentOccurrence.Name)
Call OpenParent(oOcc.ParentOccurrence, oMSName)
ProcesssedDocumentsList.remove(oParentName = oOcc.ParentOccurrence.Definition.Document.fullfilename)
End If
InventorVb.DocumentUpdate() 'this update is important to keep the assembly synced
Logger.Info(" oOcc: " & oOcc.Name)
Logger.Info("Current MS: " & oOcc.ActiveModelState.ToString)
Try
oOcc.ActiveModelState = oMSName
Catch
Logger.Info("!!!!! Could not activate : " & oMSName & " in " & oOcc.Name)
End Try
Next
End Sub
Sub OpenParent(oParent As ComponentOccurrence, oMSName As String)
oParentDoc = oParent.Definition.Document
oParentDoc.Update2(False)
oParentName = oParentDoc.fullfilename
Logger.Info("oParentName: " & oParentName)
'if this doc has been proccessed already, skip it
If ProcesssedDocumentsList.contains(oParentName) Then Exit Sub
oDoc = ThisApplication.Documents.Open(oParentName, False)
Logger.Info(" Opened: " & oDoc.fullfilename)
ProcesssedDocumentsList.add(oDoc.fullfilename)
oFactoryDoc = ThisDoc.Document.ComponentDefinition.FactoryDocument
Try
oFactoryDoc.ComponentDefinition.ModelStates(oMSName).activate
Catch
Logger.Info("!!!!!!!!!!!!!!!!!!!! Could not activate : " & oMSName & " in " & oParentName)
End Try
For Each iOcc In oDoc.ComponentDefinition.Occurrences
Logger.Info("iOcc: " & iOcc.Name)
iOcc.ActiveModelState = oMSName
Next
oDoc.Close
Logger.Info(" Closed: " & oDoc.fullfilename)
End Sub
