Here's a simple code. Select a subassembly and call the context menu to create a new Model State
Sub Main()
' Initialize document and events
oDoc = ThisApplication.ActiveDocument
oUserInputEvents = ThisApplication.CommandManager.UserInputEvents
' Initialize the button
InitializeButtonDefinition()
' Attach the button execution handler
AddHandler oButtonDef.OnExecute, AddressOf CreateNewModelStateHandler
End Sub
Private Sub InitializeButtonDefinition()
Try
Dim oControlDefs As ControlDefinitions = ThisApplication.CommandManager.ControlDefinitions
' Check if the button already exists to avoid duplicate creation
Try
oButtonDef = oControlDefs.Item("CreateModelStateBtn")
Catch ex As Exception
oButtonDef = Nothing
End Try
If oButtonDef Is Nothing Then
' Create a new button
oButtonDef = oControlDefs.AddButtonDefinition("Create Model State", _
"CreateModelStateBtn", _
CommandTypesEnum.kShapeEditCmdType, _
"{FB2A51F6-321A-4E6B-8F91-3A7D4E9B1234}", _
"Creates a new model state for the selected subassembly")
End If
Catch ex As Exception
MsgBox("Error initializing the button: " & ex.Message, vbCritical, "Error")
End Try
End Sub
' Module-level variable declarations
Dim oDoc As AssemblyDocument
Dim oSubAsm As ComponentOccurrence
Dim oModelStates As ModelStates
Dim WithEvents oUserInputEvents As UserInputEvents
Dim oButtonDef As ButtonDefinition
' Handler for the OnLinearMarkingMenu event to add the button to the context menu
Private Sub oUserInputEvents_OnLinearMarkingMenu(ByVal SelectedEntities As ObjectsEnumerator, _
ByVal SelectionDevice As SelectionDeviceEnum, _
ByVal LinearMenu As CommandControls, _
ByVal AdditionalInfo As NameValueMap) Handles oUserInputEvents.OnLinearMarkingMenu
' Check if there are selected items and that the selection was made in the browser
If SelectedEntities.Count = 0 Or SelectionDevice <> SelectionDeviceEnum.kBrowserSelection Then Exit Sub
' Get the first selected object
Dim oSelected As Object = SelectedEntities.Item(1)
' Check if the selected object is a subassembly
If TypeOf oSelected Is ComponentOccurrence Then
oSubAsm = oSelected
If oSubAsm.Definition.Document.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
' Add the button to the context menu
LinearMenu.AddButton(oButtonDef)
End If
End If
End Sub
' Handler for executing the command to create a new model state
Private Sub CreateNewModelStateHandler(ByVal Context As NameValueMap)
Try
' Check if a subassembly is selected
If oSubAsm Is Nothing Then
MsgBox("Subassembly not selected", vbExclamation, "Error")
Exit Sub
End If
' Get the subassembly document
Dim oSubAsmDoc As Document = oSubAsm.Definition.Document
' Check if the document is a model state member
If oSubAsm.Definition.IsModelStateMember Then
' If it is a state member, obtain the factory document via the active model state
Dim oActiveModelState As ModelState = oSubAsm.Definition.ModelStates.ActiveModelState
oSubAsmDoc = oActiveModelState.FactoryDocument
End If
' Get ModelStates from the factory document
oModelStates = oSubAsmDoc.ComponentDefinition.ModelStates
' Create a new model state
Dim newStateName As String
newStateName = InputBox("Enter new model state name:", "Create Model State", "ModelState" & (oModelStates.Count + 1))
If newStateName = "" Then Exit Sub
Dim oNewModelState As ModelState = oModelStates.Add(newStateName)
' Activate the new state in the subassembly
'oSubAsm.ActiveModelState = newStateName
' Update the main document
oDoc.Update
MsgBox("New model state created: " & newStateName, vbInformation, "Success")
Catch ex As Exception
MsgBox("Error creating model state: " & ex.Message, vbCritical, "Error")
End Try
End Sub
INV 2025.3