Aborting a script execution

Aborting a script execution

TRIALCAD2014
Enthusiast Enthusiast
892 Views
3 Replies
Message 1 of 4

Aborting a script execution

TRIALCAD2014
Enthusiast
Enthusiast

I'm trying to abort further execution of a script using the API.

I tried 

ui.terminateActiveCommand()
but the script keeps executing so that does not work.
plain return works only in the outermost method but not in nested calls.
Normally this would be something like exit(-77) or abort()
I could not find anything in the documentation.
 
Thank you,
Adrian
0 Likes
893 Views
3 Replies
Replies (3)
Message 2 of 4

BrianEkins
Mentor
Mentor

With the way Fusion uses Python, your program is sharing an instance of Python with all of the other programs running. This means you can't terminate Python but instead your program will need to complete its execution.

 

You can add logic that will cause it to terminate earlier but it is logic you'll need to add.  There are different ways to approach this. For example, all of your functions can return whether they were successful or not and the calling code can check the return and determine if it should continue or also return a failure code. These would percolate to the top where you can return from the original function.

 

You can also use a try except statement in the top-level function and then any time you want to abort you can raise an exception which will result in the except statement being called and you can clean up and return.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 4

TRIALCAD2014
Enthusiast
Enthusiast
Thank you for the reply. I suspected that.

The percolate is cumbersome and error-prone because it's easy to miss
some conditional things or exceptions and it adds a lot of code.

Using exceptions to implement exit functionality is also problematic
both at the code maturity level and also hard to control when it comes
to handling legitimate exceptions.

What is   ui.terminateActiveCommand() good for? I was under the
impression that scripts are implemented as commands so terminating the
command should terminate the scripts.

0 Likes
Message 4 of 4

tykapl.breuil
Advocate
Advocate

I've never used it so I may be wrong but ui.terminateActiveCommand terminates the active command that you can get with ui.activeCommand. The command here is the activation of a commandDefinition, it is what is launched when clicking on a button. Ie you click on extrude, the command Dialog is here, you terminate it and it dissapears.

 

As @BrianEkins said, you're better off simply terminating your program if you want to stop.

0 Likes