Python Script: Sketch created while UI is displayed does not persist

Python Script: Sketch created while UI is displayed does not persist

mash_farid
Explorer Explorer
458 Views
4 Replies
Message 1 of 5

Python Script: Sketch created while UI is displayed does not persist

mash_farid
Explorer
Explorer

I am trying to create a script that displays a dialog box with multiple parameters and then creates an object according to those parameters. The user can check the result, modify the parameters and see the result again and again until he's satisfied.

The problem I'm facing is that an object that is created in the (value changed) event handler, does not persist. It disappears from view (and the sketches list) as soon as the event handling terminates.
I have created a small version of the script (see then attached document) to demonstrate the problem:
In the notify() function of ValueChangedHandler()   
after the message box is displayed and user clicks Ok, the segment disappears.

mash_farid_0-1739665091222.png

what is the problem ?

Edit: Every sketch added before cmdDef.execute() in the main thread survives.  Sketches added after the dialog box is displayed -even if the sketch is added in the same main thread-  is deleted when I close the dialog box. So it does not matter if the sketch is added in an event handler or not, it will be discarded at the end.

 

0 Likes
Accepted solutions (1)
459 Views
4 Replies
Replies (4)
Message 2 of 5

mash_farid
Explorer
Explorer
import adsk.core, adsk.fusion, adsk.cam, traceback

app = adsk.core.Application.get()
ui = app.userInterface
handlers = []
design = adsk.fusion.Design.cast(app.activeProduct)
sketch = None  # Global reference to sketch

# CommandCreated handler class
class DialogCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def notify(self, args):
        dialog = args.command
        inputs = dialog.commandInputs
        
        button = inputs.addBoolValueInput('clickMe', 'Check', True)
        
        # Attach an event handler directly to the command's inputs
        valueHandler = ValueChangedHandler()
        dialog.inputChanged.add(valueHandler)  # Attach to the command's inputChanged event
        handlers.append(valueHandler)

# InputChanged handler class
class ValueChangedHandler(adsk.core.InputChangedEventHandler):
    def notify(self, args):
        global sketch
        changedInput = args.input
        if changedInput.id == 'clickMe':
            try:
                rootComp = design.rootComponent
                sketches = rootComp.sketches
                xyPlane = rootComp.xYConstructionPlane
                
                sketch = sketches.add(xyPlane)
                
                # Draw a single line segment
                startPoint = adsk.core.Point3D.create(0, 0, 0)
                endPoint = adsk.core.Point3D.create(100, 0, 0)
                sketch.sketchCurves.sketchLines.addByTwoPoints(startPoint, endPoint)
                
                app.activeViewport.refresh()
                ui.messageBox(f'New value: {changedInput.value}\nLine segment drawn')
            except Exception as e:
                ui.messageBox(f'Error: {traceback.format_exc()}')

# CommandDestroyed handler class (cleanup)
class DialogDestroyHandler(adsk.core.CommandEventHandler):
    def notify(self, args):
        global handlers
        handlers.clear()

# Main run function
def run(context):
    try:
        cmdDef = ui.commandDefinitions.itemById('NumericDialogCommand')
        if not cmdDef:
            cmdDef = ui.commandDefinitions.addButtonDefinition('NumericDialogCommand', 'Numeric Dialog', 'Dialog with numeric input')
        
        createdHandler = DialogCreatedHandler()
        destroyHandler = DialogDestroyHandler()
        
        cmdDef.commandCreated.add(createdHandler)
        handlers.append(createdHandler)
        handlers.append(destroyHandler)
        
        cmdDef.execute()
    except Exception as e:
        ui.messageBox(f'Failed:\n{traceback.format_exc()}')
0 Likes
Message 3 of 5

mash_farid
Explorer
Explorer

I confirm that while the UI thread is running, every sketch added is deleted on exit from the dialog box.

I don't know why.
The only workaround so far is to :

  • While the UI is active in the dialog box, save the created sketches in a global entity
  • create a threading.Event that will be set when the dialog box is closed with [Ok]
  • create another threading.Event that will be set when the dialog box is closed with Cancel]
  • in the main thread after cmDef.execute()  wait for either of the events to be set
  • if it's the [Ok] event then add the sketches saved in the global entity
  • if it's [cancel] event then terminate, 

 

0 Likes
Message 4 of 5

BrianEkins
Mentor
Mentor
Accepted solution

You should create the sketch in the ExecutePreview event of the command, not in the ValueChanged event.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 5 of 5

mash_farid
Explorer
Explorer

Thank you Brian,

Of course! 
I should've asked for a simple Hello World example that does what I want to do in the first place.

But I learned a lot during the painful process of looking for a solution.

cheers

0 Likes