Model States

Introduction to Model State

Model state is introduced to both Part and Assembly documents since Inventor 2022,  and the LOD(Level of Detail) in assembly will be removed from UI, the LOD related APIs will be hidden, so all the LOD related funcitons need to be migrated to Model State. In assembly, most of the functions that can work in legacy LOD can also work in model state, only some functions in legacy LOD are changed, and in model state there are some enhancements against legacy LOD(e.g. the file properties can be overriden for each model state). Because of this big change, API developers may need to know how this will impact the API behaviors.

The behavior changes from Level of Detail to Model State

1. The legacy LevelOfDetailRepresentation objects are migrated to ModelState objects. So the FullDocumentName will represent the “FullFileName<ModelState>” instead of legacy “FullFileName<LevelofDetail>”.


2. In legacy assembly data the iAssembly and LOD can exist in the same assembly file, while since Inventor 2022 the iAssembly and model states are mutually exclusive.It means ModelState is not supported in iAssembly document, and vice versa. Like ComponentOccurrences.AddiAssemblyMember doesn't support the ModelState in Options argument.

3. Suppress ComponentOccurrenceProxy is not allowed. If a legacy data has a suppressed ComponentOccurrenceProxy then you can only unsuppress it in Inventor 2022 to remove the Suppress(Override) status:

4. Document for Primary 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:
5. OnActivateModelState won't be triggered when activate or deactivate a substitute model state but the OnActivateDocument should be used to monitor this action instead.

6. When open assemly with Primary or custom model state activated, the document(s) referenced by substitute model states will be also included in the Document.ReferencedDocuments/AllReferencedDocuments collection(in Inventor 2022 RTM they won't be included but since Inventor 2022.1 will). But when the subsitute model state is activated the assembly's Document.ReferencedDocuments/AllReferencedDocuments collection will return the referenced document(s) by current substitute document only.

7. Document.File.AllReferencedFiles returns all referenced files for the IAM file including the substitutes and suppressed components.

8. It is required to save dirty assembly before adding a substitute into it, otherwise AddSubstitute will raise error.

9. When use Documents.Open or OpenWithOptions to open one IAM file multiple times with different ModelState specified, it would just switch active ModelState in the same document window instead of displaying in different document windows.

10. For Documents.ItemByName, if model state factory document and model state member document with the same FullDocumentName are both opened in memory it will return model state factory document firstly.

11. The model state factory's Document.FileSaveCounter will increase by 1 for each model state's save, this means if there are multiple model states require to save, when save the model state factory document may cause the FileSaveCounter to increase by a number that is bigger than 1. This also applies to File or FileDescriptor.FileSaveCounter. The model state member's Document.FileSaveCounter will only record the model state member document's save count.

New Model State APIs in Inventor 2022

1. PartComponentDefinition.ModelStates/AssemblyComponentDefinition.ModelStates: Access Model States.

2. AssemblyComponentDefinition/PartComponentDefinition.IsModelStateFactory/IsModelStateMember : Determine whether the document is model state factory or member document. If the document is a model state member document, ComponentDefinition/ModelState.FactoryDocument can be used to get the relative model state factory document.

3. ComponentOccurrence.ActiveModelState:Switch ComponentOccurrence model state. It is required that ComponentOccurrence.Definition.Document is up-to-date; otherwise, Save dialog would pop up to ask for document save, a failure will occur if user chooses not to save.

4. ComponentOccurrence.Replace/Replace2 will use the last active model state by default.

5. BOMView.ModelStateMemberName returns the model state member name that current BOMView is based on.

6. PropertySets edit is impacted by ModelStates.MemberEditScope.

If MemberEditScope is set to kEditActiveMember, editting Property’s value for active ModelState just overrides the value for the active ModelState; otherwise the value will be applied to the Property for all ModelState. The Document.FilePropertySets represents the iProperties in Shell Extension for Part/Assembly documents.

7. AddBendTableWithOptions:Support to add bend table with specified model state.

8. AddConfigurationTable:Support to create table with specifying model states.

9. DocumentDescriptor.ReferencedModelState:Tell the model state type of the DocumentDescriptor.

10. ModelingEvents.OnGenerateModelStateMember:To monitor when the model state member is generated.

11. FileOpenOptions.DefaultModelStateInAssembly&DefaultModelStateInPart:Set default model state when opening assembly or part document.

12. In Apprentice: ApprenticeServerComponent.Open can open part or assembly with specifying model state in FullDocumentName, but it always opens the model state factory document. ApprenticeServerDocument.ComponentDefinition.ModelStates is not accessible at present. FileManager.GetModelStates/GetModelStateName/GetLastActiveModelState are supported in Apprentice.

13. For custom addin: Environment.PreserveWhenSwitchModelState allows addin to preserve its environment when switch model states. For built-in addin this API will always return True to keep legacy behavior.

Equivalent APIs(Level of Detail vs Model State)

Level of Detail APIs Model State APIs
FullDocumentName (e.g. “C:\Assembly1.iam<Level of Detail1>”) FullDocumentName (e.g. “C:\Assembly1.iam<Model State1>”)
ComponentOccurrence. ActiveLevelOfDetailRepresentation ComponentOccurrence.ActiveModelState
DerivedAssemblyDefinition. ActiveLevelOfDetailRepresentation DerivedAssemblyDefinition.ActiveModelState
  DerivedPartDefinition.ActiveModelState
ShrinkwrapDefinition.ActiveLevelOfDetailRepresentation ShrinkwrapDefinition.ActiveModelState
AssemblyComponentDefinition. RepresentationsManager. LevelOfDetailRepresentations AssemblyComponentDefinition.ModelStates
  PartComponentDefinition.ModelStates
RepresentationsManager.ActiveLevelOfDetailRepresentation ModelStates.ActiveModelState
FileOpenOptions.DefaultLevelOfDetail FileOpenOptions.DefaultModelStateInAssembly
  FileOpenOptions.DefaultModelStateInPart
  SaveOptions.DefaultToSaveForModelStateUpdates
FileManager.GetLastActiveLevelOfDetailRepresentation FileManager.GetLastActiveModelState
FileManager.GetLevelOfDetailName FileManager.GetModelStateName
FileManager.GetLevelOfDetailRepresentations FileManager.GetModelStates
  AssemblyComponentDefinition.IsModelStateFactory
  AssemblyComponentDefinition.IsModelStateMember
  AssemblyComponentDefinition.FactoryDocument
  PartComponentDefinition.IsModelStateFactory
  PartComponentDefinition.IsModelStateMember
  PartComponentDefinition.FactoryDocument
LevelOfDetailEnum ModelStateTypeEnum
RepresentationEvents ModelStateEvents
RepresentationEvents.OnActivateLevelOfDetailRepresentation ModelStateEvents.OnActivateModelState
RepresentationEvents.OnDelete ModelStateEvents.OnDeleteModelState
RepresentationEvents.OnNewLevelOfDetailRepresentation ModelStateEvents.OnNewModelState
  BOMView.ModelStateMemberName
AssemblyDocument.LevelOfDetailName AssemblyDocument.ModelStateName
  PartDocument.ModelStateName
  ModelingEvents.OnGenerateModelStateMember
DocumentDescriptor.ReferencedLevelOfDetail DocumentDescriptor.ReferencedModelState
DrawingView.ActiveLevelOfDetailRepresentation DrawingView.ActiveModelState & SetActiveModelState
DesignViewRepresentation.CopyToLevelOfDetail DesignViewRepresentation. CopyComponentVisibilityToSuppression
  ModelState.CopyComponentSuppressionToVisibility
BrowserNode(Level of Detail root).NativeObject = LevelOfDetailRepresentation BrowserNode(ModelStates).NativeObject = ModelState

Working with Model State API

Get model state names in a document. Place an Assembly1.iam to C:\Temp folder firstly:

Sub GetModelStateNames()
    Dim sFileName As String
    sFileName = "C:\Temp\Assembly1.iam"
    
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.Documents.Open(sFileName)

    Dim oAssemblyCompDef As AssemblyComponentDefinition
    Set oAssemblyCompDef = oDoc.ComponentDefinition

    ' Get ModelStates collection object.
    Dim oModelStates As ModelStates
    Set oModelStates = oAssemblyCompDef.ModelStates

    ' Get all model state names from ModelStsates object.
    Dim oModelState As ModelState
    For Each oModelState In oModelStates
        Debug.Print oModelState.Name
    Next
    
    oDoc.Close True
    
    ' Get all model state name from FileManager
    Dim oFileManager As FileManager
    Set oFileManager = ThisApplication.FileManager
    
    Dim sModelStateNames() As String
    sModelStateNames = oFileManager.GetModelStates(sFileName)
    
    Dim sName As Variant
    For Each sName In sModelStateNames
        Debug.Print sName
    Next
    
End Sub

Switch model state for a ComponentOccurrence. Open an assembly with occurrences(not virtual component):

Sub SwitchActiveModelStateOfOccurrence()
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument

    Dim oAssemblyCompDef As AssemblyComponentDefinition
    Set oAssemblyCompDef = oDoc.ComponentDefinition

    ' Specify an occurrence to change its active model state.
    Dim oOccu As ComponentOccurrence
    Set oOccu = oAssemblyCompDef.Occurrences(1)

    ' Get native document ComponentDefinition of occurrence.
    Dim oOccuDef As ComponentDefinition
    Set oOccuDef = oOccu.Definition
    
    ' Before switch the active model state of an occurrence you should check if the document is up to date.
    If oOccuDef.Document.RequiresUpdate Then
        MsgBox("Can not switch active model state because the native document is not up to date!")
    Else
        Dim oModelStates As ModelStates
        Set oModelStates = oOccuDef.ModelStates
        
        Dim oModelState As ModelState
        For Each oModelState In oModelStates
            If oModelState.Name <> oOccu.ActiveModelState Then
    
                oOccu.ActiveModelState = oModelState.Name
                Exit Sub
            End If
        Next
    End If
End Sub


More info about model state

As each substitute in an assembly has its own Document pointer, when query each and every occurrences in an .iam file you should query the occurrences from Primary model state and also iterate each and every substitue to get their occurrences. Also when deal with attributes and reference keys each substitute has its own document context, so when use AttributeManager or ReferenceKeyManager you should consider the Document for each substitute but not only the Primary model state.

Model state member document is generated(if not yet) when a ModelState is specified to create a ComponentOccurrence, DrawingView, DerivedPart and DerivedAssembly etc..
Model state member document is not modifiable(Document.IsModifiable returns True) in below situations:

It is good practice to check the Document.IsModifiable property before trying to edit a document when work with model state document.