<?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 Javascript add-in to modify user parameters in Fusion API and Scripts Forum</title>
    <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/javascript-add-in-to-modify-user-parameters/m-p/5994603#M20456</link>
    <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been testing out writing a Javascript Add-in which would manipulate user params. Since it's a new environment I've started with the AddInSample javascript code which is bundled with Autodesk Fusion 360&amp;nbsp;and created new addIn with nearly identical sources to make the UI initialization work. The major modifications I've made are in the &lt;STRONG&gt;onInputChanged&lt;/STRONG&gt; function. The current version of the source looks the following way:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        var onInputChanged = function(args) {
            console.log('onInputChanged', args, args.input, args.input.name, '=', args.input.value);
        
            var name = args.input.name;
            var expr = args.input.value;
            try
            {
                var app = adsk.core.Application.get();
                var design = app.activeProduct;
		var userParams = design.userParameters
                var p = design.userParameters.itemByName(name);
		p.expression = ''+expr;
            } catch (e) {
                ui.messageBox(locStrings.FailedInInputChangedEvent + errorDescription(e));
            }
        };&lt;/PRE&gt;&lt;P&gt;What I've been struggling with is the runtime behaviour of the code above. To begin with I've defined 2 user parameters and used them to dimension a rectangle in a sketch. When I edit a parameter in the UI of the add in what is happening is the following sequence of events which I've caught with the debugger breakpoint:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;STRONG&gt;onInputChanged&amp;nbsp;&lt;/STRONG&gt;function is called with the value from UI inputs&lt;/LI&gt;&lt;LI&gt;The new value of parameter expression is being applied&lt;/LI&gt;&lt;LI&gt;After&amp;nbsp;&lt;STRONG&gt;onInputChanged&lt;/STRONG&gt;&amp;nbsp;function finishes previous user parameters values are being restored&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;All of these are illustrated in images I've attached:&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;SPAN&gt;step1.png - before applying the new value the dimension in the upper edge of the screenshot is 10mm&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;step2.png - after updating the parameter's expression new value is applied and the&amp;nbsp;value of the dimension in the upper edge applies and is 20mm&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;step3.png - after the function exits the dimension returns to 10mm&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I'm suspecting is that handling&amp;nbsp;of the userParams manipulation is done in a temporary context/thread and thus it doesn't apply to the activeDocument/design context so it could be persisted. Does somebody maybe know how to persist the new expression value to the design?&lt;/P&gt;</description>
    <pubDate>Mon, 18 Jan 2016 21:44:46 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2016-01-18T21:44:46Z</dc:date>
    <item>
      <title>Javascript add-in to modify user parameters</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/javascript-add-in-to-modify-user-parameters/m-p/5994603#M20456</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been testing out writing a Javascript Add-in which would manipulate user params. Since it's a new environment I've started with the AddInSample javascript code which is bundled with Autodesk Fusion 360&amp;nbsp;and created new addIn with nearly identical sources to make the UI initialization work. The major modifications I've made are in the &lt;STRONG&gt;onInputChanged&lt;/STRONG&gt; function. The current version of the source looks the following way:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        var onInputChanged = function(args) {
            console.log('onInputChanged', args, args.input, args.input.name, '=', args.input.value);
        
            var name = args.input.name;
            var expr = args.input.value;
            try
            {
                var app = adsk.core.Application.get();
                var design = app.activeProduct;
		var userParams = design.userParameters
                var p = design.userParameters.itemByName(name);
		p.expression = ''+expr;
            } catch (e) {
                ui.messageBox(locStrings.FailedInInputChangedEvent + errorDescription(e));
            }
        };&lt;/PRE&gt;&lt;P&gt;What I've been struggling with is the runtime behaviour of the code above. To begin with I've defined 2 user parameters and used them to dimension a rectangle in a sketch. When I edit a parameter in the UI of the add in what is happening is the following sequence of events which I've caught with the debugger breakpoint:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;STRONG&gt;onInputChanged&amp;nbsp;&lt;/STRONG&gt;function is called with the value from UI inputs&lt;/LI&gt;&lt;LI&gt;The new value of parameter expression is being applied&lt;/LI&gt;&lt;LI&gt;After&amp;nbsp;&lt;STRONG&gt;onInputChanged&lt;/STRONG&gt;&amp;nbsp;function finishes previous user parameters values are being restored&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;All of these are illustrated in images I've attached:&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;SPAN&gt;step1.png - before applying the new value the dimension in the upper edge of the screenshot is 10mm&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;step2.png - after updating the parameter's expression new value is applied and the&amp;nbsp;value of the dimension in the upper edge applies and is 20mm&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;step3.png - after the function exits the dimension returns to 10mm&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I'm suspecting is that handling&amp;nbsp;of the userParams manipulation is done in a temporary context/thread and thus it doesn't apply to the activeDocument/design context so it could be persisted. Does somebody maybe know how to persist the new expression value to the design?&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2016 21:44:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/javascript-add-in-to-modify-user-parameters/m-p/5994603#M20456</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-01-18T21:44:46Z</dc:date>
    </item>
    <item>
      <title>Re: Javascript add-in to modify user parameters</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/javascript-add-in-to-modify-user-parameters/m-p/5995021#M20457</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to recommend you do that in onExecute event instead of onInputChanged. The following Python sample would demostrade what you want. You can translate it to Javascript if you prefer to Javascript language.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a command, generally we only commit the changes which made in onExecute. For those changes in other events, we will treat it as command preview and abort them right away before onExecute event is executed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Marshal&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;#Author-
#Description-

import adsk.core, adsk.fusion, traceback
import os

# global set of event handlers to keep them referenced for the duration of the command
handlers = []

app = adsk.core.Application.get()
if app:
    ui = app.userInterface

class MyCommandExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            command = args.firingEvent.sender
            inputs = command.commandInputs

            # We need access to the inputs within a command during the execute.
            nameInput = inputs.itemById('Name')
            valInput = inputs.itemById('Value')

            name = nameInput.value
            expr = valInput.value
            design = app.activeProduct
            p = design.userParameters.itemByName(name)
            p.expression = '' + expr           

        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyCommandDestroyHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            # when the command is done, terminate the script
            # this will release all globals which will remove all event handlers
            adsk.terminate()
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):    
    def __init__(self):
        super().__init__()        
    def notify(self, args):
        try:
            cmd = args.command
            onExecute = MyCommandExecuteHandler()
            cmd.execute.add(onExecute)
            onDestroy = MyCommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            
            # keep the handler referenced beyond this function
            handlers.append(onExecute)
            handlers.append(onDestroy)

            # Define the inputs.
            inputs = cmd.commandInputs

            inputs.addStringValueInput('Name', 'Number of Parameter', 'A')
            inputs.addStringValueInput('Value', 'Value of Parameter', '1 in')

        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
                
def run(context):
    try:
        commandId = 'ModifyParameter'
        commandName = 'Modify parameter'
        commandDescription = 'Modify parameter'
        cmdDef = ui.commandDefinitions.itemById(commandId)
        if not cmdDef:
            resourceDir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'resources') # absolute resource file path is specified
            cmdDef = ui.commandDefinitions.addButtonDefinition(commandId, commandName, commandDescription, resourceDir)

        onCommandCreated = MyCommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        # keep the handler referenced beyond this function
        handlers.append(onCommandCreated)

        inputs = adsk.core.NamedValues.create()
        cmdDef.execute(inputs)

        # prevent this module from being terminate when the script returns, because we are waiting for event handlers to fire
        adsk.autoTerminate(False)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Jan 2016 07:07:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/javascript-add-in-to-modify-user-parameters/m-p/5995021#M20457</guid>
      <dc:creator>marshaltu</dc:creator>
      <dc:date>2016-01-19T07:07:26Z</dc:date>
    </item>
    <item>
      <title>Re: Javascript add-in to modify user parameters</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/javascript-add-in-to-modify-user-parameters/m-p/5995784#M20458</link>
      <description>&lt;P&gt;Hey check this example out. &amp;nbsp;I actually use both methods. &amp;nbsp;It is kind of nice to see the parameters update live when you are changing them, then you commit the values in on execute.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;hope this helps:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/tapnair/ParamEdit" target="_blank"&gt;https://github.com/tapnair/ParamEdit&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry it is also in Python. &amp;nbsp;BTW if you are going to be doing anythign that involves a lot of actions like updating parameters you will find it significantly faster in python vs. Javascript. &amp;nbsp; JS runs out of process and is orders of magnatude slower to compute. &amp;nbsp;If it is possible for your application.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2016 16:30:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/javascript-add-in-to-modify-user-parameters/m-p/5995784#M20458</guid>
      <dc:creator>prainsberry</dc:creator>
      <dc:date>2016-01-19T16:30:35Z</dc:date>
    </item>
    <item>
      <title>Re: Javascript add-in to modify user parameters</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/javascript-add-in-to-modify-user-parameters/m-p/5996070#M20459</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;SPAN class="login-bold"&gt;&lt;A href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/2549199" target="_self"&gt;prainsberry&lt;/A&gt;!&amp;nbsp;This is&amp;nbsp;exactly what I wanted to write in JS as live preview is a really useful feature.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2016 18:46:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/javascript-add-in-to-modify-user-parameters/m-p/5996070#M20459</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-01-19T18:46:13Z</dc:date>
    </item>
    <item>
      <title>Re: Javascript add-in to modify user parameters</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/javascript-add-in-to-modify-user-parameters/m-p/5996103#M20460</link>
      <description>Thanks man, glad to help out.  Feel free to use this code as needed.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Patrick Rainsberry</description>
      <pubDate>Tue, 19 Jan 2016 18:57:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/javascript-add-in-to-modify-user-parameters/m-p/5996103#M20460</guid>
      <dc:creator>prainsberry</dc:creator>
      <dc:date>2016-01-19T18:57:53Z</dc:date>
    </item>
  </channel>
</rss>

