Create Model States using iLogic

Create Model States using iLogic

zoe.baker-bushby
Enthusiast Enthusiast
6,345 Views
23 Replies
Message 1 of 24

Create Model States using iLogic

zoe.baker-bushby
Enthusiast
Enthusiast

I have an assembly that has a duplication of the same part. 

The part has only the master model state, but I want to be able to add model states to this part via ilogic.

 

This line creates model states at the assembly level (which I don't need).

oModelState = oDoc.ComponentDefinition.ModelStates.Add(oName)
		oDoc.ActiveModelState = oName

 

This is what I am trying to do... vaguely...

Dim oDoc As AssemblyDocument
	
Dim oOcc As ComponentOccurrence 	
For Each oOcc In Occurrences
'The below line does not work, what is the correct syntax????
oOcc.ComponentDefinition.ModelStates.Add(oName)
next

 

Any help/advice would be great?

 

Accepted solutions (3)
6,346 Views
23 Replies
Replies (23)
Message 21 of 24

tecnico
Contributor
Contributor

Unfortunately this rule of error, I will try to explain myself better.
I need to have a rule that when I enter a length value (on a dialog box that appears when the rule or module is executed) and once the length is set (which will be the only variable that will change) the rule will have to create a model state with that specific value and a model state with the same name as the variable value.
If the variable has already assumed that value and therefore a model state with that name is already present, it will not have to create any model state but simply load the existing one with the name corresponding to the entered value.

0 Likes
Message 22 of 24

J_Dumont
Advocate
Advocate

Hi Wesley,

I'm using Inventor 2026, and this code has been beneficial. However, I am running into an issue.

The code is still in development, and once I have the model states working, I can add the code to change the parameters.

 

From the assembly, the rule prompts the user for a length. From there, the code will create a model state at the assembly level using the length as its name. I then want to make a model state in a part file using the same name.

 

This works great the first time. The next time I run it, I get an error.

 

Here is my code:

Dim oDoc As AssemblyDocument = ThisDoc.Document
Dim oMSs As ModelStates = oDoc.ComponentDefinition.ModelStates

' Check if MemberEditScope is already set to ActiveMember
If oMSs.MemberEditScope <> MemberEditScopeEnum.kEditActiveMember Then
  ' If not, set it to ActiveMember
  oMSs.MemberEditScope = MemberEditScopeEnum.kEditActiveMember
End If

myparam = InputBox("Enter Tube Length", "Wet Tube")
Dim oTubeLength As Double = CDblAny(myparam)
Dim oTubeLengthstr As String
oTubeLengthstr = Format(oTubeLength, "0.000")
Tube_Length=myparam

' Create a new model state
Try 
	Dim newModelState As ModelState = oMSs.Add(oTubeLengthstr)
	Catch
		ThisDoc.ActiveModelState=oTubeLengthstr
End Try

Dim doc As AssemblyDocument = ThisDoc.Document
oName = "iWet-Outer-No-Seal:1"
occ = doc.ComponentDefinition.Occurrences.ItemByName(oName)
Dim facDoc As PartDocument = occ.Definition.Document
oPCD = facDoc.ComponentDefinition
If oPCD.IsModelStateMember Then
	facDoc = oPCD.FactoryDocument
End If
oMStates = oPCD.ModelStates
newModelStateName = oTubeLengthstr
oMState = oMStates.Add(newModelStateName)
oMState.Activate()
occ.ActiveModelState = newModelStateName

The image below shows the result after the code is first run. At this point, the part file doesn't have any model states.

J_Dumont_0-1754136200294.png

The next time the rule is run, it displays an error.

J_Dumont_1-1754136241842.png

 

The model state is created properly in the assembly, but not in the part.

J_Dumont_2-1754136290266.png

 

Any assistance would be greatly appreciated.

 

 

 

 

0 Likes
Message 23 of 24

WCrihfield
Mentor
Mentor

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

EESignature

(Not an Autodesk Employee)

Message 24 of 24

J_Dumont
Advocate
Advocate

Hi Wesley,

 

Thank you for the effort you put into this email. It works perfectly.

A model state is created at the assembly and part level, which are named the same.

 

Now, I need to modify a parameter at the part level for the newly created active state, and I'm running into issues.

 

I found another solution offered by you that might work, located here:

https://forums.autodesk.com/t5/inventor-programming-ilogic/model-states-and-inability-to-change-para...

 

I would like to get your thoughts.

 

 

 

 

 

0 Likes