Jump to content

How to get the output of a 'callback' in the main part of the code


Recommended Posts

When using vs.GetRect I need to use a callback when using this function in Python. Unfortuantly I'm unable to display the points of both corners of the box in the main code. I am able to display it  within the callback code but I'm unable to pass the value to the main code. Here is is a simplified part of the code. Can anybody help me out. Thanks!

 

import vs

def _extract_xy(arg):

    if isinstance(arg, (tuple, list)):
        return float(arg[0]), float(arg[1])
    else:
        return

def on_rect_callback(*args):

    if len(args) == 2:  # two points
        x1, y1 = _extract_xy(args[0])
        x2, y2 = _extract_xy(args[1])
    else:
        vs.AlrtDialog(f"Unexpected GetRect callback signature: {len(args)} arg(s).")
        return


    vs.AlrtDialog(
        f"P1 here is working: ({x1:.3f}, {y1:.3f})\n"
        f"P2 here is working: ({x2:.3f}, {y2:.3f})\n"
    )

def run():
    # Ask the user to drag a marquee; callback receives the points.
    x1, y1, x2, y2 = vs.GetRect(on_rect_callback)

    # Here is the TypeError: cannot unpack non-iterable NoneType object
    vs.AlrtDialog(
        f"P1 in main code: ({x1:.3f}, {y1:.3f})\n"
        f"P2 in main code: ({x2:.3f}, {y2:.3f})\n"
    )
run()

 

Edited by MarcelP102
Link to comment

Unfortunately it works only for VectorScript (pascal), but not for Python.
In Pascal Vectorscript the execution of the script is paused, the user interaction is performed, and then the script continues with returning the result of GetRect.
In Python, the main script is completed, vs.GetRect returns always None and the rest of the code is executed, that's why you get the error. Then the user interaction is completed, leaving you only with the possibility to continue your logic only from within the callback function.

 

You can see the result of this:
vs.AlrtDialog( str(vs.GetRect(on_rect_callback) ))

 

Same issue with vs.GetLine or vs.TrackObject. 

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...