<?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 Betreff: User Command Preview does not work, execute does. in Fusion API and Scripts Forum</title>
    <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/user-command-preview-does-not-work-execute-does/m-p/10510314#M8382</link>
    <description>&lt;P&gt;Yes! It works. Thank you kanndenti,&amp;nbsp;&lt;BR /&gt;I cleaned the code a bit, to put it here as an example for a working user selection.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;import adsk.core, adsk.fusion, adsk.cam, traceback, os, math
# Global variables to keep them referenced for the duration of the command
_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)
_handlers = []
_dropdownInput3 = adsk.core.DropDownCommandInput.cast(None)
_inputs = adsk.core.CommandInputs.cast(None)

def makeIt(): 
    try:
        # Here is my code that is done with every change in User-Selections:
        # Get UserCommand-Values
        curve = _inputs.itemById('selection').selection(0).entity
        broadness = _inputs.itemById('broadness').value/4
        thickness = _inputs.itemById('thickness').value/2
        rotations = _inputs.itemById('rotations').value
        shape = _dropdownInput3.selectedItem.name

        design = _app.activeProduct
        rootComp = design.rootComponent
        # create new component
        newOcc1 = rootComp.occurrences.addNewComponent(adsk.core.Matrix3D.create())
        root_comp = newOcc1.component
        root_comp.name = 'CordRopeCoil'
        #path = adsk.fusion.Path.create(curve,1) 
        # #see https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-1728AD08-743F-4ABB-AA10-C2B2C53A0757
        path = root_comp.features.createPath(curve)
        planes = root_comp.constructionPlanes
        planeInput = planes.createInput()
        distance = adsk.core.ValueInput.createByReal(0)
        planeInput.setByDistanceOnPath(path, distance)
        
        plane = planes.add(planeInput)
        sketches = root_comp.sketches
        sketch = sketches.add(plane)

        # Define lines for linear profiles
        point1a = sketch.sketchPoints.add(adsk.core.Point3D.create(broadness, 0, 0))
        point1b = sketch.sketchPoints.add(adsk.core.Point3D.create(-broadness,0, 0))
        point2a = sketch.sketchPoints.add(adsk.core.Point3D.create(0, broadness, 0))
        point2b = sketch.sketchPoints.add(adsk.core.Point3D.create(0,-broadness, 0))
        line1 = sketch.sketchCurves.sketchLines.addByTwoPoints(point1a, point1b)
        line2 = sketch.sketchCurves.sketchLines.addByTwoPoints(point2a, point2b)
    
        if shape == 'Rope': loops = 2
        else: loops = 1
        for p in range (0,loops): 
            #create the correct sweep witch edge is used as Path. 
            if p==1: 
                prof = root_comp.createOpenProfile(line1, False) 
            else:
                prof = root_comp.createOpenProfile(line2, False) 
            sweeps = root_comp.features.sweepFeatures 
            sweepInput = sweeps.createInput(prof, path, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
            sweepInput.twistAngle = adsk.core.ValueInput.createByReal(rotations*math.pi*2)
            sweepInput.isSolid = False
            sweep = sweeps.add(sweepInput)
            lastBody = root_comp.bRepBodies.count-1
            body = root_comp.bRepBodies.item(lastBody)
            body.isLightBulbOn = False

            loop = 2
            if shape == 'Coil': loop = 1
            for k in range(0, loop):
                if k ==0: edge = body.edges.item(1)
                else: edge = body.edges.item(3)
                twist = 0
                # The edge of the flat sweep is now used as path for the solid sweeps
                path1 = root_comp.features.createPath(edge, False)
                planeInput = planes.createInput()
                distance = adsk.core.ValueInput.createByReal(0)
                planeInput.setByDistanceOnPath(path1, distance)
                plane1 = planes.add(planeInput)
                sketches1 = root_comp.sketches
                sketch1 = sketches1.add(plane1)
                circle1 = sketch1.sketchCurves.sketchCircles.addByCenterRadius(adsk.core.Point3D.create(0,0,0), thickness/2)
                prof1 = sketch1.profiles.item(0)

                # create solid sweep
                itemIndex1 = root_comp.bRepBodies.count
                sweeps = root_comp.features.sweepFeatures 
                sweepInput1 = sweeps.createInput(prof1, path1, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
                sweepInput1.twistAngle = adsk.core.ValueInput.createByReal(twist)
                sweepInput1.isSolid = True
                sweep = sweeps.add(sweepInput1)
                #_ui.messageBox("your Selection was: Shape: "+shape)

    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyExecutePreviewHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        eventArgs = adsk.core.CommandEventArgs.cast(args)
        makeIt()

class MyExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            eventArgs = adsk.core.InputChangedEventArgs.cast(args)
            makeIt()
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyDestroyHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        adsk.terminate()

class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
# Here the your UserCommands are created
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
            global _inputs
            _inputs = adsk.core.CommandInputs.cast(eventArgs.command.commandInputs)

            message = '&amp;lt;div align="center"&amp;gt;See also my &amp;lt;a href="http:picsandpixels.at/fusion360"&amp;gt;windings-AddIn&amp;lt;/a&amp;gt;&amp;lt;/div&amp;gt;'
            _inputs.addTextBoxCommandInput('fullWidth_textBox', '', message, 1, True)  

            selInput = _inputs.addSelectionInput('selection', 'Select open curve', 'SketchCurves')
            selInput.setSelectionLimits(1)
            selInput.addSelectionFilter('SketchCurves')

            global _dropdownInput3     
            _dropdownInput3 = _inputs.addDropDownCommandInput('dropdown3', 'shape', adsk.core.DropDownStyles.LabeledIconDropDownStyle)
            _dropdownInput3.listItems.add('Cord', False, '')
            _dropdownInput3.listItems.add('Rope', True, '')
            _dropdownInput3.listItems.add('Coil', False, '')

            _inputs.addValueInput('broadness', 'Broadness', 'mm', adsk.core.ValueInput.createByReal(0.2))
            _inputs.addValueInput('thickness', 'Thickness', 'mm', adsk.core.ValueInput.createByReal(0.2))
            _inputs.addIntegerSpinnerCommandInput('rotations', 'Rotations', 0, 50, 1, 10)

            onExecutePreview = MyExecutePreviewHandler()
            eventArgs.command.executePreview.add(onExecutePreview)
            _handlers.append(onExecutePreview)

            onExecute = MyExecuteHandler()
            eventArgs.command.execute.add(onExecute)
            _handlers.append(onExecute)

            onDestroy = MyDestroyHandler()
            eventArgs.command.destroy.add(onDestroy)
            _handlers.append(onDestroy)  

        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

        
def run(context):  
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui  = _app.userInterface
        
        # Create a command.
        cmd = _ui.commandDefinitions.itemById('tableTest')
        if cmd:
            cmd.deleteMe()
# Rename the script in the following line:
        cmd = _ui.commandDefinitions.addButtonDefinition('tableTest', 'Cord, Rope, Coil -Maker', 'Table Test', '')
        onCommandCreated = MyCommandCreatedHandler()
        cmd.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)

        cmd.execute()
        # Set this so the script continues to run.
        adsk.autoTerminate(False)
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="adminTCYL2_0-1627724417652.png" style="width: 400px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/947159i96CB993E6851FCFF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="adminTCYL2_0-1627724417652.png" alt="adminTCYL2_0-1627724417652.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 31 Jul 2021 09:40:23 GMT</pubDate>
    <dc:creator>adminTCYL2</dc:creator>
    <dc:date>2021-07-31T09:40:23Z</dc:date>
    <item>
      <title>User Command Preview does not work, execute does.</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/user-command-preview-does-not-work-execute-does/m-p/10503134#M8380</link>
      <description>&lt;P&gt;I have done a nice little skript to generate Ropes, Coils and Cords along a users curve:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="adminTCYL2_0-1627493951668.png" style="width: 400px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/946167i18421AAF87A905E4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="adminTCYL2_0-1627493951668.png" alt="adminTCYL2_0-1627493951668.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="adminTCYL2_0-1627496231440.png" style="width: 400px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/946180iA13822B40B2C2683/image-size/medium?v=v2&amp;amp;px=400" role="button" title="adminTCYL2_0-1627496231440.png" alt="adminTCYL2_0-1627496231440.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;By now the script works only after pressing "OK". As seen in the montage-picture, the User should see what his settings do while the settings-window is opened. But he sees the result only after pressing Ok and thereby closing this window. This is, because the&amp;nbsp; Class "MyInputChangeHandler" does not work right. But it is not written by me and I am confused. I wrote a boolean named "execute" into line 35. The&amp;nbsp;MyInputChangeHandler sets this to False, so the rest of the skript is only done by&amp;nbsp;&lt;SPAN style="font-family: inherit;"&gt;MyExecuteHandler. Otherwise it tries to make previews, but this leads to failures.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Can someone help me to improve this skript? I want to give it for free to the Autocad Website:&lt;BR /&gt;&lt;BR /&gt;&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;LI-CODE lang="general"&gt;import adsk.core, adsk.fusion, adsk.cam, traceback, os, math
# Global variables to keep them referenced for the duration of the command
_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)
_handlers = []
_dropdownInput3 = adsk.core.DropDownCommandInput.cast(None)
_inputs = adsk.core.CommandInputs.cast(None)

def makeIt(execute): 
    try:
        # Here is my code that is done with every change in User-Selections:

        # Get UserCommand-Values
        curve = _inputs.itemById('selection').selection(0).entity
        broadness = _inputs.itemById('broadness').value
        thickness = _inputs.itemById('thickness').value
        rotations = _inputs.itemById('rotations').value
        shape = _dropdownInput3.selectedItem.name

        design = _app.activeProduct
        root_comp = design.rootComponent
        # create new component
        # newOcc1 = rootComp.occurrences.addNewComponent(adsk.core.Matrix3D.create())
        # root_comp = newOcc1.component
        # root_comp.name = 'Windings'
        path = root_comp.features.createPath(curve)
        planes = root_comp.constructionPlanes
        planeInput = planes.createInput()
        distance = adsk.core.ValueInput.createByReal(0)
        planeInput.setByDistanceOnPath(path, distance)
        
        #execute = True
        if execute:
            plane = planes.add(planeInput)
            sketches = root_comp.sketches
            sketch = sketches.add(plane)

            # Define lines for linear profiles
            point1a = sketch.sketchPoints.add(adsk.core.Point3D.create(broadness, 0, 0))
            point1b = sketch.sketchPoints.add(adsk.core.Point3D.create(-broadness,0, 0))
            point2a = sketch.sketchPoints.add(adsk.core.Point3D.create(0, broadness, 0))
            point2b = sketch.sketchPoints.add(adsk.core.Point3D.create(0,-broadness, 0))
            line1 = sketch.sketchCurves.sketchLines.addByTwoPoints(point1a, point1b)
            line2 = sketch.sketchCurves.sketchLines.addByTwoPoints(point2a, point2b)
        

            if shape == 'Rope': loops = 2
            else: loops = 1
            for p in range (0,loops): 
                #create the correct sweep witch edge is used as Path. 
                if p==1: 
                    prof = root_comp.createOpenProfile(line1, False) 
                else:
                    prof = root_comp.createOpenProfile(line2, False) 
                sweeps = root_comp.features.sweepFeatures 
                sweepInput = sweeps.createInput(prof, path, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
                sweepInput.twistAngle = adsk.core.ValueInput.createByReal(rotations*math.pi*2)
                sweepInput.isSolid = False
                sweep = sweeps.add(sweepInput)
                lastBody = root_comp.bRepBodies.count-1
                body = root_comp.bRepBodies.item(lastBody)
                body.isLightBulbOn = False

                loop = 2
                if shape == 'Coil': loop = 1
                for k in range(0, loop):
                    if k ==0: edge = body.edges.item(1)
                    else: edge = body.edges.item(3)
                    twist = 0
                    # The edge of the flat sweep is now used as path for the solid sweeps
                    path1 = root_comp.features.createPath(edge, False)
                    planeInput = planes.createInput()
                    distance = adsk.core.ValueInput.createByReal(0)
                    planeInput.setByDistanceOnPath(path1, distance)
                    plane1 = planes.add(planeInput)
                    sketches1 = root_comp.sketches
                    sketch1 = sketches1.add(plane1)
                    circle1 = sketch1.sketchCurves.sketchCircles.addByCenterRadius(adsk.core.Point3D.create(0,0,0), thickness/2)
                    prof1 = sketch1.profiles.item(0)

                    # create solid sweep
                    itemIndex1 = root_comp.bRepBodies.count
                    sweeps = root_comp.features.sweepFeatures 
                    sweepInput1 = sweeps.createInput(prof1, path1, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
                    sweepInput1.twistAngle = adsk.core.ValueInput.createByReal(twist)
                    sweepInput1.isSolid = True
                    sweep = sweeps.add(sweepInput1)

            # Here is my code that is additionaly done when User presses OK:
            _ui.messageBox("your Selection was: Shape: "+shape + ", Broadness: "+ str(broadness)+ ", Thickness: "+ str(thickness)+", Rotations: "+ str(rotations))

    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

# Event handler for the inputChanged event.
class MyInputChangedHandler(adsk.core.InputChangedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            eventArgs = adsk.core.InputChangedEventArgs.cast(args)
            changedInput = eventArgs.input
            execute = False
            makeIt(execute)
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

# Event handler for the execute event.
class MyExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            eventArgs = adsk.core.InputChangedEventArgs.cast(args)
            execute=True
            makeIt(execute)
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

# Event handler for the destroy event.
class MyDestroyHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        adsk.terminate()

# Event handler for the commandCreated event.
# Here you create your UserCommands
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
            global _inputs
            _inputs = adsk.core.CommandInputs.cast(eventArgs.command.commandInputs)

            message = '&amp;lt;div align="center"&amp;gt;See also my &amp;lt;a href="http:picsandpixels.at/windings"&amp;gt;windings-AddIn&amp;lt;/a&amp;gt;&amp;lt;/div&amp;gt;'
            _inputs.addTextBoxCommandInput('fullWidth_textBox', '', message, 1, True)  

            selInput = _inputs.addSelectionInput('selection', 'Select open curve', 'SketchCurves')
            selInput.setSelectionLimits(1)
            selInput.addSelectionFilter('SketchCurves')

            global _dropdownInput3     
            _dropdownInput3 = _inputs.addDropDownCommandInput('dropdown3', 'shape', adsk.core.DropDownStyles.LabeledIconDropDownStyle)
            _dropdownInput3.listItems.add('Cord', False, '')
            _dropdownInput3.listItems.add('Rope', True, '')
            _dropdownInput3.listItems.add('Coil', False, '')

            _inputs.addValueInput('broadness', 'Broadness', 'mm', adsk.core.ValueInput.createByReal(0.4))
            _inputs.addValueInput('thickness', 'Thickness', 'mm', adsk.core.ValueInput.createByReal(0.2))
            _inputs.addIntegerSpinnerCommandInput('rotations', 'Rotations', 0, 20, 1, 10)

            # Connect to command execute.
            onExecute = MyExecuteHandler()
            eventArgs.command.execute.add(onExecute)
            _handlers.append(onExecute)

            # Connect to input changed.
            onInputChanged = MyInputChangedHandler()
            eventArgs.command.inputChanged.add(onInputChanged)
            _handlers.append(onInputChanged)

            # Connect to the command terminate.
            onDestroy = MyDestroyHandler()
            eventArgs.command.destroy.add(onDestroy)
            _handlers.append(onDestroy)  
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
        
def run(context):  
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui  = _app.userInterface
        
        # Create a command.
        cmd = _ui.commandDefinitions.itemById('tableTest')
        if cmd:
            cmd.deleteMe()
# Rename the script in the following line:
        cmd = _ui.commandDefinitions.addButtonDefinition('tableTest', 'Cord, Rope, Coil -Maker', 'Table Test', '')
        onCommandCreated = MyCommandCreatedHandler()
        cmd.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)

        cmd.execute()
        # Set this so the script continues to run.
        adsk.autoTerminate(False)
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))&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;</description>
      <pubDate>Wed, 28 Jul 2021 18:17:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/user-command-preview-does-not-work-execute-does/m-p/10503134#M8380</guid>
      <dc:creator>adminTCYL2</dc:creator>
      <dc:date>2021-07-28T18:17:18Z</dc:date>
    </item>
    <item>
      <title>Re: User Command Preview does not work, execute does.</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/user-command-preview-does-not-work-execute-does/m-p/10509906#M8381</link>
      <description>&lt;P&gt;Hi &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/6857866"&gt;@adminTCYL2&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For preview, use the executePreview event instead of the inputChanged event.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-dbecfaf8-9bb6-4e40-9df3-0a3e48136a4b" target="_blank" rel="noopener"&gt;https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-dbecfaf8-9bb6-4e40-9df3-0a3e48136a4b&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I modified it like this, and it works correctly.&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;・・・
class MyInputChangedHandler(adsk.core.InputChangedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            eventArgs = adsk.core.InputChangedEventArgs.cast(args)
            # changedInput = eventArgs.input
            # execute = False
            # makeIt(execute)
        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:
・・・
            onExecutePreview = MyExecutePreviewHandler()
            eventArgs.command.executePreview.add(onExecutePreview)
            _handlers.append(onExecutePreview)
・・・

class MyExecutePreviewHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        eventArgs = adsk.core.CommandEventArgs.cast(args)

        execute = True
        makeIt(execute)
&lt;/LI-CODE&gt;
&lt;P&gt;Items created by the executePreview event will be deleted on their own without the need to delete them.&lt;/P&gt;</description>
      <pubDate>Sat, 31 Jul 2021 03:50:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/user-command-preview-does-not-work-execute-does/m-p/10509906#M8381</guid>
      <dc:creator>kandennti</dc:creator>
      <dc:date>2021-07-31T03:50:52Z</dc:date>
    </item>
    <item>
      <title>Betreff: User Command Preview does not work, execute does.</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/user-command-preview-does-not-work-execute-does/m-p/10510314#M8382</link>
      <description>&lt;P&gt;Yes! It works. Thank you kanndenti,&amp;nbsp;&lt;BR /&gt;I cleaned the code a bit, to put it here as an example for a working user selection.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;import adsk.core, adsk.fusion, adsk.cam, traceback, os, math
# Global variables to keep them referenced for the duration of the command
_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)
_handlers = []
_dropdownInput3 = adsk.core.DropDownCommandInput.cast(None)
_inputs = adsk.core.CommandInputs.cast(None)

def makeIt(): 
    try:
        # Here is my code that is done with every change in User-Selections:
        # Get UserCommand-Values
        curve = _inputs.itemById('selection').selection(0).entity
        broadness = _inputs.itemById('broadness').value/4
        thickness = _inputs.itemById('thickness').value/2
        rotations = _inputs.itemById('rotations').value
        shape = _dropdownInput3.selectedItem.name

        design = _app.activeProduct
        rootComp = design.rootComponent
        # create new component
        newOcc1 = rootComp.occurrences.addNewComponent(adsk.core.Matrix3D.create())
        root_comp = newOcc1.component
        root_comp.name = 'CordRopeCoil'
        #path = adsk.fusion.Path.create(curve,1) 
        # #see https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-1728AD08-743F-4ABB-AA10-C2B2C53A0757
        path = root_comp.features.createPath(curve)
        planes = root_comp.constructionPlanes
        planeInput = planes.createInput()
        distance = adsk.core.ValueInput.createByReal(0)
        planeInput.setByDistanceOnPath(path, distance)
        
        plane = planes.add(planeInput)
        sketches = root_comp.sketches
        sketch = sketches.add(plane)

        # Define lines for linear profiles
        point1a = sketch.sketchPoints.add(adsk.core.Point3D.create(broadness, 0, 0))
        point1b = sketch.sketchPoints.add(adsk.core.Point3D.create(-broadness,0, 0))
        point2a = sketch.sketchPoints.add(adsk.core.Point3D.create(0, broadness, 0))
        point2b = sketch.sketchPoints.add(adsk.core.Point3D.create(0,-broadness, 0))
        line1 = sketch.sketchCurves.sketchLines.addByTwoPoints(point1a, point1b)
        line2 = sketch.sketchCurves.sketchLines.addByTwoPoints(point2a, point2b)
    
        if shape == 'Rope': loops = 2
        else: loops = 1
        for p in range (0,loops): 
            #create the correct sweep witch edge is used as Path. 
            if p==1: 
                prof = root_comp.createOpenProfile(line1, False) 
            else:
                prof = root_comp.createOpenProfile(line2, False) 
            sweeps = root_comp.features.sweepFeatures 
            sweepInput = sweeps.createInput(prof, path, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
            sweepInput.twistAngle = adsk.core.ValueInput.createByReal(rotations*math.pi*2)
            sweepInput.isSolid = False
            sweep = sweeps.add(sweepInput)
            lastBody = root_comp.bRepBodies.count-1
            body = root_comp.bRepBodies.item(lastBody)
            body.isLightBulbOn = False

            loop = 2
            if shape == 'Coil': loop = 1
            for k in range(0, loop):
                if k ==0: edge = body.edges.item(1)
                else: edge = body.edges.item(3)
                twist = 0
                # The edge of the flat sweep is now used as path for the solid sweeps
                path1 = root_comp.features.createPath(edge, False)
                planeInput = planes.createInput()
                distance = adsk.core.ValueInput.createByReal(0)
                planeInput.setByDistanceOnPath(path1, distance)
                plane1 = planes.add(planeInput)
                sketches1 = root_comp.sketches
                sketch1 = sketches1.add(plane1)
                circle1 = sketch1.sketchCurves.sketchCircles.addByCenterRadius(adsk.core.Point3D.create(0,0,0), thickness/2)
                prof1 = sketch1.profiles.item(0)

                # create solid sweep
                itemIndex1 = root_comp.bRepBodies.count
                sweeps = root_comp.features.sweepFeatures 
                sweepInput1 = sweeps.createInput(prof1, path1, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
                sweepInput1.twistAngle = adsk.core.ValueInput.createByReal(twist)
                sweepInput1.isSolid = True
                sweep = sweeps.add(sweepInput1)
                #_ui.messageBox("your Selection was: Shape: "+shape)

    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyExecutePreviewHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        eventArgs = adsk.core.CommandEventArgs.cast(args)
        makeIt()

class MyExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            eventArgs = adsk.core.InputChangedEventArgs.cast(args)
            makeIt()
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyDestroyHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        adsk.terminate()

class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
# Here the your UserCommands are created
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
            global _inputs
            _inputs = adsk.core.CommandInputs.cast(eventArgs.command.commandInputs)

            message = '&amp;lt;div align="center"&amp;gt;See also my &amp;lt;a href="http:picsandpixels.at/fusion360"&amp;gt;windings-AddIn&amp;lt;/a&amp;gt;&amp;lt;/div&amp;gt;'
            _inputs.addTextBoxCommandInput('fullWidth_textBox', '', message, 1, True)  

            selInput = _inputs.addSelectionInput('selection', 'Select open curve', 'SketchCurves')
            selInput.setSelectionLimits(1)
            selInput.addSelectionFilter('SketchCurves')

            global _dropdownInput3     
            _dropdownInput3 = _inputs.addDropDownCommandInput('dropdown3', 'shape', adsk.core.DropDownStyles.LabeledIconDropDownStyle)
            _dropdownInput3.listItems.add('Cord', False, '')
            _dropdownInput3.listItems.add('Rope', True, '')
            _dropdownInput3.listItems.add('Coil', False, '')

            _inputs.addValueInput('broadness', 'Broadness', 'mm', adsk.core.ValueInput.createByReal(0.2))
            _inputs.addValueInput('thickness', 'Thickness', 'mm', adsk.core.ValueInput.createByReal(0.2))
            _inputs.addIntegerSpinnerCommandInput('rotations', 'Rotations', 0, 50, 1, 10)

            onExecutePreview = MyExecutePreviewHandler()
            eventArgs.command.executePreview.add(onExecutePreview)
            _handlers.append(onExecutePreview)

            onExecute = MyExecuteHandler()
            eventArgs.command.execute.add(onExecute)
            _handlers.append(onExecute)

            onDestroy = MyDestroyHandler()
            eventArgs.command.destroy.add(onDestroy)
            _handlers.append(onDestroy)  

        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

        
def run(context):  
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui  = _app.userInterface
        
        # Create a command.
        cmd = _ui.commandDefinitions.itemById('tableTest')
        if cmd:
            cmd.deleteMe()
# Rename the script in the following line:
        cmd = _ui.commandDefinitions.addButtonDefinition('tableTest', 'Cord, Rope, Coil -Maker', 'Table Test', '')
        onCommandCreated = MyCommandCreatedHandler()
        cmd.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)

        cmd.execute()
        # Set this so the script continues to run.
        adsk.autoTerminate(False)
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="adminTCYL2_0-1627724417652.png" style="width: 400px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/947159i96CB993E6851FCFF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="adminTCYL2_0-1627724417652.png" alt="adminTCYL2_0-1627724417652.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 31 Jul 2021 09:40:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/user-command-preview-does-not-work-execute-does/m-p/10510314#M8382</guid>
      <dc:creator>adminTCYL2</dc:creator>
      <dc:date>2021-07-31T09:40:23Z</dc:date>
    </item>
  </channel>
</rss>

