How can I change the order of statements in a routine?
I have a lot of path statements and need to sort their execution order by some parameter. I could not find a direct way to modify the routine.Statements since it is read only. One way is routine.clear() then recreate all the statements from scratch but I don’t really like that solution.
Is there an easy way of reordering statements in a routine?
In Python there is no easy way to do this, which is why there is no tutorial on it.
The solution you mentioned is what you have to do to reorder them.
HOWEVER… cue dramatic music you could try options A, B, or C:
Create a Python Process Handler that acts as a Statement Handler for the robot executor. In the OnStatementExecute event, execute the statements in the order that you want. In this case, it doesn’t matter how the statements are arranged in program. That means you could sort the statements as needed and execute them however you see fit.
Go super saiyan and create a module or method that creates a dummy Robot Executor behavior. Populate the program of that executor with the statements from the original program in the order you want. That way you do not have to delete them. Why do this? Well, the Program tab in GUI will show the program of EACH executor in the selected robot, so you can verify your work as you go. Once the program looks the way you want, switch the programs… but the path to enlightenment has revealed a possible shortcut --> add another robot, copy the program to that robot, use the GUI to reorder the statements, and then exchange the robots.
Create a .NET command that you can call in a script to reorder the statements. With path statements you could add a property or schema property that dictates its order of execution, e.g. pathA must come before pathB.
Not sure if there is an add-on for this already floating around, so maybe someone here already has what you need to get the job done.
Thanks for the in-depth suggestions and letting me know there is no straightforward solution. However, all three options are infeasible for me.
Here is my use case: I am developing an add-on which is to be used by an operator. I generate the path statements based on CAD models. So, I want to execute the path statements by default with a sensible order, but I also need to enable the operator to change the order by dragging from GUI in case they need it. So the visual order on the GUI should match with the execution order, which eliminates option A and B. For option C, I don’t have any other .NET commands, so just involving .NET for this task would over-complicate it and since I am not familiar with .NET API I am sure it would take a long time for me to do it.
I guess I have to follow the ugly way of “delete-create again”. I would really be happy if there was a copy constructor for vcStatement, but I guess there isn’t.
Thanks for your response
If you are generating the statements based on CAD model then you do have the option to place them at different indices in the routine at the time of creation. The addStatement() method has an optional argument for such purposes. The default is -1, which adds a statement to end of scope/routine.
Is that what you were after?
That’s exactly what I want, somehow I missed that.
Thanks a lot.