Hi @J_Dumont. Any time multiple ModelStates exist within a single model file, and we are interacting with multiple of those types of files with a single iLogic rule process, things pretty much always get a lot more complicated than it seems like they should be. At that point, there can be multiple 'versions' (ModelState member documents) of the same file open in Inventor's session memory at the same time, then the issue of how to edit one version of it successfully while another 'member' is referencing the same source file arises. One process (setting parameter values) seems to require updating the document just before attempting to make changes to it. It seems like this becomes necessary because you may have made changes to another version of the same file during your code process, where those changes have not yet been 'saved' or written to the source file yet, before trying to make changes to another version being held in memory.
Anyways, below is an edited version of your last code above, with several more error avoiding steps included, and a lot more feedback along the way. If you try to do something in the Try side if a Try/Catch statement, then try to do something equally as error prone on the Catch side, then it is possible for the thing it is trying to do on the Catch side to cause an Exception to be thrown, even though it is technically inside of a Try/Catch statement, so I often just use multiple Try statements instead, with some good feedback either way, during debug runs. Using a bit more code and spreading things out a bit usually doesn't hurt, and can help readability and debug processes later. The worst situation is when you have an assembly open and multiple of the components in that assembly each have multiple ModelStates defines within them, and multiple 'instances' of the same component are set to different ModelStates. The first version you encounter will usually seem to process just fine, then the next 'version' of the same source file will act like it is ReadOnly (IsModifiable = False), due to the multiple versions open in Inventor's memory at the same time.
'get the factory version of current document, then try to cast it to an assembly type variable
'if this fails, no error, just no variable assignment
Dim oADoc As AssemblyDocument = TryCast(ThisDoc.FactoryDocument, Inventor.AssemblyDocument)
'check if variable got a value...if not, then exit rule (current document was not an assembly)
If oADoc Is Nothing Then Return 'exit rule
'update the assembly, if it is needed
If oADoc.RequiresUpdate Then oADoc.Update2(True)
'get the assembly's ModelStates collection object (always get this from Factory, not Member)
Dim oAsmMSs As ModelStates = oADoc.ComponentDefinition.ModelStates
' Check if assembly's MemberEditScope is already set to ActiveMember
If Not oAsmMSs.MemberEditScope = MemberEditScopeEnum.kEditActiveMember Then
' If not, set it to ActiveMember
oAsmMSs.MemberEditScope = MemberEditScopeEnum.kEditActiveMember
End If
'not sure if this is just local variable, or local parameter
myparam = InputBox("Enter Tube Length", "Wet Tube")
Dim oTubeLength As Double = CDblAny(myparam)
Dim oTubeLengthstr As String
oTubeLengthstr = Format(oTubeLength, "0.000")
'I assume 'Tube_Lenth' is a 'local' variable (in the assembly's definition)
Tube_Length = myparam
' Create a new assembly model state, or activate existing
Dim oAsmMS As ModelState = Nothing
Try
'first try to find existing ModelState
oAsmMS = oAsmMSs.Item(oTubeLengthstr)
Logger.Info("Successfully got existing ModelState in assembly.")
Catch
Logger.Error("Error getting existing ModelState in assembly!")
End Try
If oAsmMS Is Nothing Then
Try
oAsmMS = oAsmMSs.Add(oTubeLengthstr)
Logger.Info("Successfully created new ModelState in assembly.")
Catch
Logger.Error("Error creating new ModelState in assembly!")
End Try
End If
If oAsmMS IsNot Nothing Then
oAsmMS.Activate()
'ThisDoc.ActiveModelState = oTubeLengthstr
Else
Return 'exit rule
End If
'make sure our Document variable is still the Factory
oADoc = oAsmMS.FactoryDocument
'get top level component by name
Dim oName As String = "iWet-Outer-No-Seal:1"
Dim occ As ComponentOccurrence = Nothing
Try
occ = oADoc.ComponentDefinition.Occurrences.ItemByName(oName)
Logger.Info("Found specifically named component in assembly.")
Catch
Logger.Info("Error getting specified component in assembly!")
End Try
If occ Is Nothing Then Return 'exit rule
'get the component's Factory document
Dim occPDoc As PartDocument = occ.Definition.Document
If occPDoc.ComponentDefinition.IsModelStateMember Then
occPDoc = occPDoc.ComponentDefinition.FactoryDocument
End If
'update this part, if it is needed
If occPDoc.RequiresUpdate Then occPDoc.Update2(True)
'get the component factory document's ModelStates collection object (from the Factory)
Dim occPDocMSs As ModelStates = occPDoc.ComponentDefinition.ModelStates
'get / create a ModelState in component factory document with same name
'(no need to activate it, or set MemberEditScope of the part)
Dim occPDocMS As ModelState = Nothing
Try
'first try to find existing ModelState in component part
occPDocMS = occPDocMSs.Item(oTubeLengthstr)
Logger.Info("Found existing ModelState in component part.")
Catch
Logger.Error("Error getting specified ModelState in component part!")
End Try
If occPDocMS Is Nothing Then
Try
'try to create the ModelState in the component part
occPDocMS = occPDocMSs.Add(oTubeLengthstr)
Logger.Info("Created new ModelState in component part.")
Catch
Logger.Error("Error creating new ModelState in component part!")
End Try
End If
'now try to set the component to that ModelState
Try
occ.ActiveModelState = oTubeLengthstr
Logger.Info("Successfully set component's active ModelState.")
Catch
Logger.Error("Error setting component's active ModelState!")
End Try
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield

(Not an Autodesk Employee)