#!/usr/bin/env python3.8

'''
Code to reproduce the bug where RunProgram throws up a modal dialog.

https://robodk.com/forum/Thread-How-to-use-RUNMODE-QUICKVALIDATE
'''

import os
from robodk import robolink, robomath

UR10e_path = "../../third_party/robodk/UR10e.robot"

rdk = robolink.Robolink()

station_name = "Modal Dialog Bug"
station = rdk.Item(station_name, robolink.ITEM_TYPE_STATION)
if station.Valid():
    print(f"'{station_name}' station already open.  Closing")
    rdk.setActiveStation(station)
    rdk.CloseStation()

station = rdk.AddStation(station_name)
assert station.Valid()

rdk.setActiveStation(station)

robot = station.AddFile(os.path.abspath(UR10e_path))
assert robot.Valid()

t = rdk.AddTarget("t")
assert t.Valid()

t.setPoseAbs(robomath.transl(5000,0,0)) # Move target out of reach of robot

p = rdk.AddProgram("p")
p.MoveJ(t)

rdk.setRunMode(robolink.RUNMODE_QUICKVALIDATE)
result = rdk.RunProgram("p", wait_for_finished=True)
print(f"\nProgram is invalid, but running in RUNMODE_QUICKVALIDATE acts like nothing's wrong (and returns {result})\n")
print(f"Next we will run with RUNMODE_SIMULATE, where we get a modal dialog box that also blocks the API, and no indication there was a problem. (Returns {result})\n")

rdk.setRunMode(robolink.RUNMODE_SIMULATE)
rdk.RunProgram("p", wait_for_finished=True)
print("Done")
