Public Class ThisRule Sub Main() Dim fileName = "D:\forum\huisje2 - modelstate\huisje.iam" Dim doc As Document = ThisApplication.Documents.Open(fileName, False) Using mainMSManager = New ModelStateManager(doc, GetAnotherModelStateScope(doc)) SetAnotherModelStateActive(mainMSManager.ModelStates) doc = mainMSManager.FactoryDocument SetGuidParameter(doc) For Each refDoc As Document In doc.AllReferencedDocuments Using refMSManager = New ModelStateManager(refDoc, GetAnotherModelStateScope(refDoc)) SetAnotherModelStateActive(refMSManager.ModelStates) SetGuidParameter(refMSManager.FactoryDocument) End Using Next End Using doc.Save2() doc.Close(True) End Sub Private Sub SetGuidParameter(doc) Dim userParameters As UserParameters = doc.ComponentDefinition.Parameters.UserParameters Dim guid = System.Guid.NewGuid.ToString() Try userParameters.Item("GUID").Value = guid Catch ex As Exception userParameters.AddByValue("GUID", guid, "Text") End Try End Sub Private Function GetAnotherModelStateScope(doc As Document) Dim currentScope As MemberEditScopeEnum = doc.ComponentDefinition.ModelStates.MemberEditScope If (currentScope = MemberEditScopeEnum.kEditActiveMember) Then Return MemberEditScopeEnum.kEditAllMembers Else Return MemberEditScopeEnum.kEditActiveMember End If End Function Private Sub SetAnotherModelStateActive(modelStates As ModelStates) Dim activeModelState As ModelState = modelStates.ActiveModelState Dim primaryModelState As ModelState = modelStates.Item(1) If (activeModelState Is primaryModelState) Then modelStates.Item(modelStates.Count).Activate() Else primaryModelState.Activate() End If End Sub End Class Public Class ModelStateManager Implements IDisposable Private _doc As Document Private _documentType As DocumentTypeEnum Private _startActiveModelState As ModelState Private _startMemberEditScope As MemberEditScopeEnum Public Sub New(ByVal doc As Document) If doc.DocumentType <> DocumentTypeEnum.kPartDocumentObject AndAlso doc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Throw New ArgumentException("This is not a part or assembly document") End If If doc Is Nothing Then Throw New ArgumentException("Document may not be NULL") End If _doc = doc _documentType = _doc.DocumentType _doc = FactoryDocument If ModelStates.Count <> 0 Then _startActiveModelState = ModelStates.ActiveModelState End If _startMemberEditScope = ModelStates.MemberEditScope End Sub Public Sub New(ByVal doc As Document, ByVal editScope As MemberEditScopeEnum) Me.New(doc) Me.EditScope = editScope End Sub Public ReadOnly Property FactoryDocument As Document Get If (_doc.ComponentDefinition.IsModelStateMember) Then Return _doc.ComponentDefinition.FactoryDocument End If Return _doc End Get End Property Public ReadOnly Property ModelStates As ModelStates Get Return _doc.ComponentDefinition.ModelStates End Get End Property Public Property EditScope As MemberEditScopeEnum Get Return ModelStates.MemberEditScope End Get Set(ByVal value As MemberEditScopeEnum) ModelStates.MemberEditScope = value End Set End Property Public Sub Dispose() Implements IDisposable.Dispose If (_startActiveModelState IsNot Nothing) Then _startActiveModelState.Activate() End If ModelStates.MemberEditScope = _startMemberEditScope End Sub End Class