Creating Model States in sub-sub-assemblies prompts dialog box

Creating Model States in sub-sub-assemblies prompts dialog box

ben.goodelliott
Contributor Contributor
169 Views
2 Replies
Message 1 of 3

Creating Model States in sub-sub-assemblies prompts dialog box

ben.goodelliott
Contributor
Contributor

bengoodelliott_1-1741378540673.png

Hi folks,

 

    I've been trying (and succeeding) to create model states 2 layers down from the main assembly with iLogic. However, before my rule completes, I am prompted with the message box above. I have tried forcing a save in the sub-assembly, the sub-sub-assembly, and top level assembly with no luck.

    I've looked through the forum and done several google searches without finding anything directly relevant as far as I can tell. Does anyone have any suggestions?

 

Thanks

0 Likes
170 Views
2 Replies
Replies (2)
Message 2 of 3

Ivan_Sinicyn
Advocate
Advocate

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
0 Likes
Message 3 of 3

ben.goodelliott
Contributor
Contributor

Hi Ivan, thanks for the reply.

 

I think I've managed to isolate the issue to another rule attempting to set the model state back to Primary.

 

Thanks for your time.

0 Likes