How to Check if Model State Exists Before Setting It in iLogic?

How to Check if Model State Exists Before Setting It in iLogic?

philippe_abellanosa
Explorer Explorer
131 Views
3 Replies
Message 1 of 4

How to Check if Model State Exists Before Setting It in iLogic?

philippe_abellanosa
Explorer
Explorer

Hi everyone,

I'm working with drawing views that reference assemblies with multiple model states.

 

I'm working on an iLogic rule that sets different model states on drawing views based on user input. My current code attempts to set a model state like this:

 
ModStateName = "4-5-1548-05"
ActiveSheet.View("VIEW15").NativeEntity.SetActiveModelState(ModStateName & "'")
ActiveSheet.View("VIEW10").NativeEntity.SetActiveModelState(ModStateName)

How can I check if a model state name exists before trying to set it? I want to avoid errors when the model state doesn't exist in the part/assembly.

 

Something like

 
If ModelStateExists(ModStateName & "'") Then
ActiveSheet.View("VIEW15").View.Suppressed = False
ActiveSheet.View("VIEW15").NativeEntity.SetActiveModelState(ModStateName & "'") Else ActiveSheet.View("VIEW15").View.Suppressed = True
End If

Inventor 2026

 

 

Has anyone dealt with this before? Any help would be appreciated!

Thanks!

Accepted solutions (1)
132 Views
3 Replies
Replies (3)
Message 2 of 4

J_Pfeifer_
Advocate
Advocate

Create a function that iterates through the model states.

Function DoesModelStateExist() as boolean
Dim oDoc as partdocument = Thisapplication.activeDocument

Dim pComp as partcomponentDefinition = odoc.componentDefinition

Dim DocModelStates as modelstates = PartComp.Modelstates

For each iModelstate as modelstate in Docmodelstates
    If ImodelState.name.contains("Name") 
      Return true
    else if modelstate.Name.contains("Name2") 
      return true
    else
     Return false
    end if 
next
End function




Then you can call this to return true or false. 

 

0 Likes
Message 3 of 4

cidhelp
Advocate
Advocate
Accepted solution

Hello @philippe_abellanosa ,

 

you can try this:

MSName = "4-5-1548-05"
oDrwView = ActiveSheet.View("VIEW10")
Dim oModelStates As ModelStates = oDrwView.ModelFactoryDocument.ComponentDefinition.ModelStates
Dim MSfound As Boolean = False
For Each oModelState As ModelState In oModelStates
	If oModelState.Name.ToUpper = MSName.ToUpper Then
		MSfound = True
		Exit For
	End If
Next
If MSfound Then
	oDrwView.NativeEntity.SetActiveModelState(MSName)
Else
	MsgBox("Modelstate not found: " & MSName,,"Error ModelState")
End If
0 Likes
Message 4 of 4

philippe_abellanosa
Explorer
Explorer

I ended up using this solution over the function since it was good enough for my issue (might use the function in the future @J_Pfeifer_ ). For reference this is the working ilogic code that I ended up implementing.

iLogicForm.Show("Form 1") '
ModStateName = DWG
ModStateName2 = DWG & "'"
oDrwView = ActiveSheet.View("VIEW15")
Dim oModelStates As ModelStates = oDrwView.ModelFactoryDocument.ComponentDefinition.ModelStates
Dim MSfound As Boolean = False
For Each oModelState As ModelState In oModelStates
	If oModelState.Name.ToUpper = ModStateName2.ToUpper Then
		MSfound = True
		Exit For
	End If
Next

If MSfound Then
	ActiveSheet.View("VIEW15").View.Suppressed = False
	ActiveSheet.View("VIEW15").NativeEntity.SetActiveModelState(ModStateName2)
Else
	ActiveSheet.View("VIEW15").View.Suppressed = True
	MsgBox("Modelstate not found: " & MSName,,"Error ModelState")
End If

ActiveSheet.View("VIEW10").NativeEntity.SetActiveModelState(ModStateName)

Thank you so much guys!