<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Issues using app.executeTextCommand() in add-ins in Fusion API and Scripts Forum</title>
    <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/issues-using-app-executetextcommand-in-add-ins/m-p/11331768#M5621</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/12028964"&gt;@en9y37&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I believe the source of the problem is when you chain the execution of the commands.&lt;/P&gt;&lt;P&gt;In Fusion, only one command is active at a time; when you start a new one while other is active, it will fire the command_execute handler just as the new one is being created.&amp;nbsp; This is the case on your code when you start actualizarGsheet from&amp;nbsp;actualizaTodoPostprocesa's commmand_execute.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'll suggest to use a custom event to process the Command.Start's instructions.&lt;/P&gt;&lt;P&gt;Take a look a this example on how to implement the custom event:&amp;nbsp;&lt;A href="https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-B38361DE-1B38-432D-BAE6-ABC3B0E7AB35" target="_blank" rel="noopener"&gt;https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-B38361DE-1B38-432D-BAE6-ABC3B0E7AB35&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When the code in the custom event handler runs, the command_execute had ended and you won't have the problems you had before.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Jorge&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 31 Jul 2022 23:58:39 GMT</pubDate>
    <dc:creator>Jorge_Jaramillo</dc:creator>
    <dc:date>2022-07-31T23:58:39Z</dc:date>
    <item>
      <title>Issues using app.executeTextCommand() in add-ins</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/issues-using-app-executetextcommand-in-add-ins/m-p/11331383#M5620</link>
      <description>&lt;P&gt;Hi there!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Several days ago I started a thread in this forum because I had troubles implementing dropdown menus in my add-ins. After hours of research, tests and the help from&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/12364171"&gt;@Jorge_Jaramillo&lt;/a&gt;&amp;nbsp;, we found that all the problems had a quite weird origin. I'll try to explain.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have created several add-ins in which I use many times the command "&lt;SPAN&gt;app&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;executeTextCommand&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;)". Some of this add-ins behaves as a sort of macros in which I sequence several commands, so this seems to me a great option to be used. Thanks &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/3787950"&gt;@kandennti&lt;/a&gt;&amp;nbsp;for your help!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The problem is, if I call a Fusion native commmand with "&lt;/SPAN&gt;&lt;SPAN&gt;app&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;executeTextCommand&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;)" in my add-ins, everything works properly, but if I call a non-native command, then the execution begins an endless loop and the program crashes. This behaviour happens calling my own command add-ins and others such as the add-in JoinerCAD.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I've developed a couple of simple codes to reproduce this weird behaviour. In this example, when the selection is done in the dropdown, the function "command_execute" is run, but while debugging, I can see that once the line which execute the non-native add-in (line 108) is executed, the function "command_execute" is restarted, and so on.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;In case this gave you more information about this issue, if I make this simple change in line 37, the command dialog is bypassed, but the add-in&amp;nbsp;&lt;SPAN&gt;works completely properly.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;futil.add_handler(cmd_def.commandCreated, command_created)
#Changed command_created to command_execute
futil.add_handler(cmd_def.commandCreated, command_execute)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance to everyone for the help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Here is the code of the add-in from which I call others:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import adsk.core, adsk.fusion, traceback
import os
from ...lib import fusion360utils as futil
app = adsk.core.Application.get()
ui = app.userInterface

# TODO *** Specify the command identity information. ***
CMD_ID = f'actualizaTodoPostprocesa'
CMD_NAME = 'Actualiza todo y postprocesa' 
CMD_Description = 'Actualiza y postprocessa el diseño y sus subensamblajes con los datos de Google Sheets'

# Specify that the command will be promoted to the panel.
IS_PROMOTED = True

# TODO *** Define the location where the command button will be created. ***
# This is done by specifying the workspace, the tab, and the panel, and the 
# command it will be inserted beside. Not providing the command to position it
# will insert it at the end.
WORKSPACE_ID = 'FusionSolidEnvironment'
PANEL_ID = 'SolidScriptsAddinsPanel'
COMMAND_BESIDE_ID = 'ScriptsManagerCommand'

# Resource location for command icons, here we assume a sub folder in this directory named "resources".
ICON_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'resources', '')

# Local list of event handlers used to maintain a reference so
# they are not released and garbage collected.
local_handlers = []


# Executed when add-in is run.
def start():
    # Create a command Definition.
    cmd_def = ui.commandDefinitions.addButtonDefinition(CMD_ID, CMD_NAME, CMD_Description, ICON_FOLDER)

    # Define an event handler for the command created event. It will be called when the button is clicked.
    futil.add_handler(cmd_def.commandCreated, command_created)
    
    # ******** Add a button into the UI so the user can run the command. ********
    # Get the target workspace the button will be created in.
    workspace = ui.workspaces.itemById(WORKSPACE_ID)

    # Get the panel the button will be created in.
    panel = workspace.toolbarPanels.itemById(PANEL_ID)

    # Create the button command control in the UI after the specified existing command.
    control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False)

    # Specify if the command is promoted to the main toolbar. 
    control.isPromoted = IS_PROMOTED


# Executed when add-in is stopped.
def stop():
    # Get the various UI elements for this command
    workspace = ui.workspaces.itemById(WORKSPACE_ID)
    panel = workspace.toolbarPanels.itemById(PANEL_ID)
    command_control = panel.controls.itemById(CMD_ID)
    command_definition = ui.commandDefinitions.itemById(CMD_ID)

    # Delete the button command control
    if command_control:
        command_control.deleteMe()

    # Delete the command definition
    if command_definition:
        command_definition.deleteMe()

# Function that is called when a user clicks the corresponding button in the UI.
# This defines the contents of the command dialog and connects to the command related events.

def command_created(args: adsk.core.CommandCreatedEventArgs):
    # General logging for debug.
    futil.log(f'{CMD_NAME} Command Created Event')

    # https://help.autodesk.com/view/fusion360/ENU/?contextId=CommandInputs
    inputs = args.command.commandInputs

    # TODO Define the dialog for your command by adding different inputs to the command.
    listaItems = inputs.addDropDownCommandInput('elegirPost','Postprocesador',0).listItems
    listaItems.add("Mucobal",True)
    listaItems.add("Zoroa",False)

    # TODO Connect to the events that are needed by this command.
    futil.add_handler(args.command.execute, command_execute, local_handlers=local_handlers)
    futil.add_handler(args.command.inputChanged, command_input_changed, local_handlers=local_handlers)
    futil.add_handler(args.command.executePreview, command_preview, local_handlers=local_handlers)
    futil.add_handler(args.command.validateInputs, command_validate_input, local_handlers=local_handlers)
    futil.add_handler(args.command.destroy, command_destroy, local_handlers=local_handlers)

# This event handler is called when the user clicks the OK button in the command dialog or 
# is immediately called after the created event not command inputs were created for the dialog.
def command_execute(args: adsk.core.CommandEventArgs):

     
    # TODO ******************************** Your code here ********************************
    try:  
        inputs = args.command.commandInputs
        listaItems = inputs.addDropDownCommandInput('elegirPost','Postprocesador',0).listItems
        listaItems.add("Mucobal",True)
        listaItems.add("Zoroa",False)     
        
        postUsuario: adsk.core.DropDownCommandInput = inputs.itemById('elegirPost')
        nombrePost = postUsuario.selectedItem.name
        
              
        app.executeTextCommand(u'Commands.Start ContextUpdateAllFromParentCmd') #Native command
        app.executeTextCommand(u'Commands.Start actualizarGsheet') #Non-native command
               
        ui.messageBox("If you read this, everything works fine" + nombrePost )

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

# This event handler is called when the command needs to compute a new preview in the graphics window.
def command_preview(args: adsk.core.CommandEventArgs):
    # General logging for debug.
    futil.log(f'{CMD_NAME} Command Preview Event')
    inputs = args.command.commandInputs


# This event handler is called when the user changes anything in the command dialog
# allowing you to modify values of other inputs based on that change.
def command_input_changed(args: adsk.core.InputChangedEventArgs):
    changed_input = args.input
    inputs = args.inputs

    # General logging for debug.
    futil.log(f'{CMD_NAME} Input Changed Event fired from a change to {changed_input.id}')


# This event handler is called when the user interacts with any of the inputs in the dialog
# which allows you to verify that all of the inputs are valid and enables the OK button.
def command_validate_input(args: adsk.core.ValidateInputsEventArgs):
    # General logging for debug.
    futil.log(f'{CMD_NAME} Validate Input Event')

    inputs = args.inputs
    
    # Verify the validity of the input values. This controls if the OK button is enabled or not.
        

# This event handler is called when the command terminates.
def command_destroy(args: adsk.core.CommandEventArgs):
    # General logging for debug.
    futil.log(f'{CMD_NAME} Command Destroy Event')

    global local_handlers
    local_handlers = []&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The code for the add-in to be called, just in case you could find anything wrong in it:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import adsk.core, adsk.fusion, traceback
import os
from ...lib import fusion360utils as futil


app = adsk.core.Application.get()
ui = app.userInterface


# TODO *** Specify the command identity information. ***
CMD_ID = f'actualizarGsheet'
CMD_NAME = 'Actualizar de Google Sheets'
CMD_Description = 'Actualiza el estado del diseño con los datos de Google Sheets, solo en el diseño actual'

# Specify that the command will be promoted to the panel.
IS_PROMOTED = False

# TODO *** Define the location where the command button will be created. ***
# This is done by specifying the workspace, the tab, and the panel, and the 
# command it will be inserted beside. Not providing the command to position it
# will insert it at the end.
WORKSPACE_ID = 'FusionSolidEnvironment'
PANEL_ID = 'SolidScriptsAddinsPanel'
COMMAND_BESIDE_ID = 'ScriptsManagerCommand'

# Resource location for command icons, here we assume a sub folder in this directory named "resources".
ICON_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'resources', '')

# Local list of event handlers used to maintain a reference so
# they are not released and garbage collected.
local_handlers = []


# Executed when add-in is run.
def start():
    # Create a command Definition.
    cmd_def = ui.commandDefinitions.addButtonDefinition(CMD_ID, CMD_NAME, CMD_Description, ICON_FOLDER)

    # Define an event handler for the command created event. It will be called when the button is clicked.
    futil.add_handler(cmd_def.commandCreated, command_created) #command_execute ejecuta la secuencia sin pasar por dialogo previo

    # ******** Add a button into the UI so the user can run the command. ********
    # Get the target workspace the button will be created in.
    workspace = ui.workspaces.itemById(WORKSPACE_ID)

    # Get the panel the button will be created in.
    panel = workspace.toolbarPanels.itemById(PANEL_ID)

    # Create the button command control in the UI after the specified existing command.
    control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False)

    # Specify if the command is promoted to the main toolbar. 
    control.isPromoted = IS_PROMOTED


# Executed when add-in is stopped.
def stop():
    # Get the various UI elements for this command
    workspace = ui.workspaces.itemById(WORKSPACE_ID)
    panel = workspace.toolbarPanels.itemById(PANEL_ID)
    command_control = panel.controls.itemById(CMD_ID)
    command_definition = ui.commandDefinitions.itemById(CMD_ID)

    # Delete the button command control
    if command_control:
        command_control.deleteMe()

    # Delete the command definition
    if command_definition:
        command_definition.deleteMe()

# Function that is called when a user clicks the corresponding button in the UI.
# This defines the contents of the command dialog and connects to the command related events.

def command_created(args: adsk.core.CommandCreatedEventArgs):
    # General logging for debug.
    futil.log(f'{CMD_NAME} Command Created Event')

    # https://help.autodesk.com/view/fusion360/ENU/?contextId=CommandInputs
    inputs = args.command.commandInputs

    # TODO Define the dialog for your command by adding different inputs to the command.

    # TODO Connect to the events that are needed by this command.
    futil.add_handler(args.command.execute, command_execute, local_handlers=local_handlers)
    futil.add_handler(args.command.inputChanged, command_input_changed, local_handlers=local_handlers)
    futil.add_handler(args.command.executePreview, command_preview, local_handlers=local_handlers)
    futil.add_handler(args.command.validateInputs, command_validate_input, local_handlers=local_handlers)
    futil.add_handler(args.command.destroy, command_destroy, local_handlers=local_handlers)

# This event handler is called when the user clicks the OK button in the command dialog or 
# is immediately called after the created event not command inputs were created for the dialog.
def command_execute(args: adsk.core.CommandEventArgs):
    # General logging for debug.
    futil.log(f'{CMD_NAME} Command Execute Event')

    # TODO ******************************** Your code here ********************************
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        ui.messageBox("Se ha ejecutado actualizar Gsheet")
   
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
       


# This event handler is called when the command needs to compute a new preview in the graphics window.
def command_preview(args: adsk.core.CommandEventArgs):
    # General logging for debug.
    futil.log(f'{CMD_NAME} Command Preview Event')
    inputs = args.command.commandInputs


# This event handler is called when the user changes anything in the command dialog
# allowing you to modify values of other inputs based on that change.
def command_input_changed(args: adsk.core.InputChangedEventArgs):
    changed_input = args.input
    inputs = args.inputs

    # General logging for debug.
    futil.log(f'{CMD_NAME} Input Changed Event fired from a change to {changed_input.id}')


# This event handler is called when the user interacts with any of the inputs in the dialog
# which allows you to verify that all of the inputs are valid and enables the OK button.
def command_validate_input(args: adsk.core.ValidateInputsEventArgs):
    # General logging for debug.
    futil.log(f'{CMD_NAME} Validate Input Event')

    inputs = args.inputs
    
    # Verify the validity of the input values. This controls if the OK button is enabled or not.
    valueInput = inputs.itemById('value_input')
    if valueInput.value &amp;gt;= 0:
        inputs.areInputsValid = True
    else:
        inputs.areInputsValid = False
        

# This event handler is called when the command terminates.
def command_destroy(args: adsk.core.CommandEventArgs):
    # General logging for debug.
    futil.log(f'{CMD_NAME} Command Destroy Event')

    global local_handlers
    local_handlers = []&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jul 2022 15:32:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/issues-using-app-executetextcommand-in-add-ins/m-p/11331383#M5620</guid>
      <dc:creator>en9y37</dc:creator>
      <dc:date>2022-07-31T15:32:21Z</dc:date>
    </item>
    <item>
      <title>Re: Issues using app.executeTextCommand() in add-ins</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/issues-using-app-executetextcommand-in-add-ins/m-p/11331768#M5621</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/12028964"&gt;@en9y37&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I believe the source of the problem is when you chain the execution of the commands.&lt;/P&gt;&lt;P&gt;In Fusion, only one command is active at a time; when you start a new one while other is active, it will fire the command_execute handler just as the new one is being created.&amp;nbsp; This is the case on your code when you start actualizarGsheet from&amp;nbsp;actualizaTodoPostprocesa's commmand_execute.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'll suggest to use a custom event to process the Command.Start's instructions.&lt;/P&gt;&lt;P&gt;Take a look a this example on how to implement the custom event:&amp;nbsp;&lt;A href="https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-B38361DE-1B38-432D-BAE6-ABC3B0E7AB35" target="_blank" rel="noopener"&gt;https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-B38361DE-1B38-432D-BAE6-ABC3B0E7AB35&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When the code in the custom event handler runs, the command_execute had ended and you won't have the problems you had before.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Jorge&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jul 2022 23:58:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/issues-using-app-executetextcommand-in-add-ins/m-p/11331768#M5621</guid>
      <dc:creator>Jorge_Jaramillo</dc:creator>
      <dc:date>2022-07-31T23:58:39Z</dc:date>
    </item>
    <item>
      <title>Re: Issues using app.executeTextCommand() in add-ins</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/issues-using-app-executetextcommand-in-add-ins/m-p/11411990#M5622</link>
      <description>&lt;P dir="ltr"&gt;Hi again &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/12364171"&gt;@Jorge_Jaramillo&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;&lt;P dir="ltr"&gt;&amp;nbsp;&lt;/P&gt;&lt;P dir="ltr"&gt;I tried something with your response but I was not able to work something out, mainly because the event handlers thing is something really hard to understand to me.&lt;/P&gt;&lt;P dir="ltr"&gt;&amp;nbsp;&lt;/P&gt;&lt;P dir="ltr"&gt;Finally, I've decided to aim the solution from another point of view, and instead of using execute text commands, I'm coding my own functions, some of theme replicating the commands I called with execute text command.&amp;nbsp;&lt;/P&gt;&lt;P dir="ltr"&gt;&amp;nbsp;&lt;/P&gt;&lt;P dir="ltr"&gt;I recognize everything makes much more sense this way, I get much more control about what I want to do and my add-ins work properly.&lt;/P&gt;&lt;P dir="ltr"&gt;&amp;nbsp;&lt;/P&gt;&lt;P dir="ltr"&gt;Thanks anyway for your help, this issue keeps without solution by now, but it's been a great chance to learn tones of things this last weeks.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Sep 2022 18:35:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/issues-using-app-executetextcommand-in-add-ins/m-p/11411990#M5622</guid>
      <dc:creator>en9y37</dc:creator>
      <dc:date>2022-09-09T18:35:54Z</dc:date>
    </item>
  </channel>
</rss>

