How to switch and delete a ModelState

How to switch and delete a ModelState

AnJanson
Advocate Advocate
2,414 Views
4 Replies
Message 1 of 5

How to switch and delete a ModelState

AnJanson
Advocate
Advocate

Hi,

In an assembly I place a part and derive it, based in on this sample.

This creates a new modelstate for the substitute part.

Next I would like to switch back to the master modelstate and delete all other modelstates.

This is the part of my code:

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

ThisApplication.SilentOperation = True
For Each oMS As ModelState In oAsmCompDef.ModelStates
	If oMS.ModelStateType = ModelStateTypeEnum.kMasterModelStateType Then
		oMS.Activate
	End If
Next
ThisApplication.SilentOperation = False

ThisApplication.ActiveDocument.update2()
ThisApplication.ActiveDocument.rebuild2()
	
For Each oMS As ModelState In oAsmCompDef.ModelStates
	If oMS.ModelStateType = ModelStateTypeEnum.kSubstituteModelStateType Then
		Try
			oMS.Delete
		Catch ex As Exception
			MsgBox(ex.Message)
		End Try
	End If
Next

The delete command throughs an error. When I debug the code I can see that this occures because the switch to the master modelstate is not yet over.

If I stop the code, go back to the assembly and start the code again, it works.

How can I update the assembly after setting the master modelstate to active?

 

This code was updated from Inventor 2021, where I used displayreprensentations, and everything worked as expected.

0 Likes
Accepted solutions (1)
2,415 Views
4 Replies
Replies (4)
Message 2 of 5

Ralf_Krieg
Advisor
Advisor
Accepted solution

Hello

 

Could be a bug?

If you don't need the SubstitueModelState, don't create it. Comment out (or delete) these two lines in your code:

Dim oSubstituteMS As ModelState
Set oSubstituteMS = oDef.ModelStates.AddSubstitute(strSubstituteFileName)

The shrinkwrap will be created, but without creating the model state.


R. Krieg
RKW Solutions
www.rkw-solutions.com
Message 3 of 5

AnJanson
Advocate
Advocate

@Ralf_Krieg  schrieb:

Could be a bug?

I assume so too ☹️



@Ralf_Krieg  schrieb:

If you don't need the SubstitueModelState, don't create it.

...

The shrinkwrap will be created, but without creating the model state.


I do not need the modelstate, but I need the substitute in my assembly. I could workaround this by placing the part as an occurence.

0 Likes
Message 4 of 5

zoe.baker-bushby
Enthusiast
Enthusiast

Hi this is an old post...

 

However for future searchers, myself included, the possible reason 

oMS.Delete

Isn't working is due to the fact you're not setting the active model state to Master.

In my own work I have noticed that Master needs to be active when trying to delete model states via iLogic.

 

I've also found using this syntax isn't enough:

Component.ActiveModelState("NAME HERE") = "Master"

 

Instead you've got to use something like:

Dim doc As AssemblyDocument = ThisDoc.Document
occ = doc.ComponentDefinition.Occurrences.ItemByName("NAME HERE")
facDoc = occ.Definition.FactoryDocument
oPCD = facDoc.ComponentDefinition
oMStates = oPCD.ModelStates
oMState = oMStates.Item("Master")
oMState.Activate()

Where sometimes the line:

facDoc = occ.Definition.FactoryDocument

Needs to be:

facDoc = occ.Definition.Document

 

I've learnt this via trial and error. No real understanding applied so if someone wishes to explain why then please do.

Or if there is a better way then I am willing to learn.

 

 

 

 

 

 

 

0 Likes
Message 5 of 5

CattabianiI
Collaborator
Collaborator

This is a tricky matter which is important to understand.

From the introduction to Model States API
4. Document for Master and custom ModelState shares the same Document but each substitute has its own Document pointer. That means in legacy Inventor when open assembly document with different LOD specified the returned AssemblyDocument pointers are different, while in Inventor 2022:

  • When open document with specifying different master/custom ModelState, only one AssemblyDocument pointer is returned.
  • A substitute’s Document pointer is different from any other ModelState’s Document’s pointer(the same as legacy behavior).

 

What does the bold sentence mean? 
It means that the active document when a master or a custom model state is active, is a different instance of the active document when a substitute model state is the active one.

What does this mean in your code?
It means that, assuming the substitute model state is the active one, the component definition you get in line 2 is a different instance of the component definition you would get after activating the master model state. And it means that once you activate the master model state the oAsmCompDef variable contains an instance which is not valid anymore because that document (the substitute one) has been closed.

Said this, try to rewrite the rule to make it works 🙂

or just check here:

' Assume a substitute model state is active
Dim subAsmDoc As Document = ThisApplication.ActiveDocument
Dim subAsmCmpDef As AssemblyComponentDefinition
subAsmCmpDef = subAsmDoc.ComponentDefinition

For Each oMS As ModelState In subAsmCmpDef.ModelStates
	If oMS.ModelStateType = ModelStateTypeEnum.kMasterModelStateType Then
		oMS.Activate
		Continue For
	End If
Next
	
Dim factoryAsmDoc As Document = ThisApplication.ActiveDocument
Dim factoryAsmCmpDef As AssemblyComponentDefinition = factoryAsmDoc.ComponentDefinition
	
For Each oMS As ModelState In factoryAsmCmpDef.ModelStates
	If oMS.ModelStateType = ModelStateTypeEnum.kSubstituteModelStateType Then
		Try
			oMS.Delete
		Catch ex As Exception
			MsgBox(ex.Message)
		End Try
	End If
Next