Jump to content
Bertf

vs.GetPt()

Recommended Posts

Hi,

I'm having troubles to get vs.GetPt() working.

What I have is a plugin written in Python that places a textbox with text from the plugin parameters on the mouse last location. -> This works (code below)

Although vs.GetMouse isn't so precisely because when moving the mouse fast after clicking, I receive another location. So I want to use the Getpt() function.

Is there any way to get vs.GetPt() working? Or am I doing something wrong?

p = vs.GetMouse()
CabinetNr = getattr(vs, 'PKastNr')
Material = getattr(vs, 'PMateriaal')
Text = 'K'+ CabinetNr + chr(13) + Material
Length = len(Text)
PosEnter = Text.index(chr(13))

#p = vs.GetPt() --> NOT WORKING HERE

vs.TextOrigin(p)
vs.CreateText(Text)
h = vs.LNewObj()

vs.SetTextFont(h, 0, Length, vs.GetFontID('Arial'))
vs.SetTextSize(h, 0, PosEnter, 12)
vs.SetTextSize(h, PosEnter, Length-PosEnter, 7)

Thank you,

Bert

Edited by Bertf

Share this post


Link to post

Hi Bert,

What kind of plug-in are you creating? This looks like it would be a menu or tool. You may want to look at coding a plug-in object, where you wouldn't need to code the user interaction (PIOs automatically create a tool for inserting them from the tool palettes).

The cursor UI functions in Python need some special handling. In Python, you need to provide a callback function that tells VW what to do with the user clicks. Take a look at the example in the developer wiki. http://developer.vectorworks.net/index.php/VS:GetPt

-Josh

Share this post


Link to post

Hi, JBenghiat.

I've tried your suggestion and it works when I create a command but it doesn't when I create the same code in a tool. Any reason for that?

Thank you (code below)

import vs;

def PickPointCallback(pt):
vs.AlrtDialog( "Point {0}:{1}".format(pt[0],pt[1]) )

vs.GetPt( PickPointCallback )

Share this post


Link to post
Hi, JBenghiat.

I've tried your suggestion and it works when I create a command but it doesn't when I create the same code in a tool. Any reason for that?

Thank you (code below)

import vs;

def PickPointCallback(pt):
vs.AlrtDialog( "Point {0}:{1}".format(pt[0],pt[1]) )

vs.GetPt( PickPointCallback )

Not an answer, but you can use lambda's and the % string format to shorten your code:

vs.GetPt(lambda pt: vs.AlrtDialog('Point %s:%s' % pt))

Share this post


Link to post

For a tool I think you can try

x1,y1 = vs.vstGetCurrPt2D()

or - not tried in code yet

outX, outY, outZ = vs.vstGetCurrPt3D(result)

As an aside I had a Vectorscript tool with

Getpt(x1,y1)

Getptl(x1,y1,x2,y2) {rubberband line}

and couldn't get the Python version of Getptl to work.

Share this post


Link to post

I spent a lot of time trying to get vs.GetPt() to work. I understand the callback function will extract the 2d point e.g

def Callback(pt):
 vs.Locus(pt)
 vs.Message(pt[0],' ',pt[1])
vs.ReDraw()
vs.GetPt(Callback)

However I have not found a way to use pt outside the callback function.

I can draw the locus using pt inside the callback function but not outside.

I really want to create the locus using pt after vs.GetPt(Callback).

Any suggestions ?

Share this post


Link to post

Yea I don't get it either.

The only way to manipulate that 'pt' value is to do it within the callback function.

Well in my testings anyway.

HTH

Share this post


Link to post

Thanks Dieter for the suggestion.

pt is still trapped in the callback function.

user_point = None creates the locus at 0,0 even before you have an oppportunity to select your user point.

Even if you make user_point a global variable within the callback function which should theoretically change user_point = None it still does not work.

Share this post


Link to post
17 hours ago, ahedley said:

Thanks Dieter for the suggestion.

pt is still trapped in the callback function.

user_point = None creates the locus at 0,0 even before you have an oppportunity to select your user point.

Even if you make user_point a global variable within the callback function which should theoretically change user_point = None it still does not work.

I see now, it's stated in the docs that script execution will not be blocked (like most other functions do): http://developer.vectorworks.net/index.php/VS:GetPt

That means that you have to make sure that you first call that function, and that the callback is actually your main function to execute with everything in it.

Share this post


Link to post

Note too that something like this will crash Vectorworks 2016/Mac for me:

import vs
def main():
	vs.GetPt(lambda pt: vs.Message(str(pt)))
    
main()

and this will work OK:

import vs

vs.GetPt(lambda pt: vs.Message(str(pt)))

As far as I can tell this is roughly what the line "This cannot be used if there is a function anywhere in the calling chain" from the docs means.

  • Like 1

Share this post


Link to post

Huh, yep. Crashes for me on Mac/2016 and 2017 too. Sorry. (That's what I get for typing in code off the top of my head).

 

I thought it might have been the call to `vs.Message`, maybe on the hypothesis that dialogs tend to be callback-driven too and the message window is kinda-sorta a dialog, but no. This crashes consistently too:

import vs

def cb(pt):
    vs.Locus(pt)

vs.GetPt(cb)

 

Share this post


Link to post

From what I understand GetPt( ) is used in vectorscript when you are trying to create a tool plugin object which may require multiple user points.

 

GetPt( ) and GetPtL( ) are procedures that are used in conjunction.

 

I have noticed in vectorscript it is relatively easy to use these procedures. However in pythonscript it is very challenging, for instance, vs.GetPt( ) using a callback function will work only once. Trying to get multiple vs.GetPt( ) calls thus far for me has proved futile. I have tried without success to use vs.GetPtL( ) which generally uses as its first point the point you select via. vs.GetPt.

 

Has anybody had success creating tool plugin objects requiring user interaction using these procedures or is there another recommended way of creating tools?

 

Most of my experimentation has been developing plugin objects, I want to broaden to tool objects.

Share this post


Link to post
11 hours ago, ahedley said:

Most of my experimentation has been developing plugin objects, I want to broaden to tool objects.

 

Tools aren't objects. Tools are used to generate objects. For example, the rectangle tool will let you create a rectangle in different ways. The rectangle object is the result. The creation of it is what the tool does, and those two have no connection at all. An object should not know how it is created.

Share this post


Link to post

So I have had no sucess in trying to get vs.GetPtL to work.

 

My guess is that it should look something like this:

 

def cb(pt1):
    def cb2(pt2):
        print('point 1: ',pt1,'  point2: ', pt2)
    print('point 1 in cb: ', pt1)
    vs.GetPtL(cb2)            #<-- throws tuple index out of range
    #vs.GetPtL(pt1, cb2)    #<-- throws invalid number of parameters to callback function
    #vs.GetPtL(cb2, pt1)    #<-- throws invalid number of parameters to callback function
    #pt2 = vs.GetPtL(pt1)    #<-- this is what's in the vs.py file as an example, 
                            #but it too throws invalid number of parameters to callback function
vs.GetPt(cb)

 

So I gave up and used GetRect which works (the "rubber band' is a line and not a rect) :

def CB(pt0, pt1):

    print('-CB-')
    print('pt0: ', pt0)
    print('pt1: ', pt1)

  
vs.GetRect(CB)

Share this post


Link to post

I am not good enough in Python to really be commenting, but in Vectorscript, GetPtL returns four Real values. I don't know if GetPtL will accept tuples or not. Try splitting them out yourself instead of expecting it to happen automatically.

 

That certainly makes sense in light of the Invalid number of parameter errors.

Share this post


Link to post
Posted (edited)

In python GetPt and, I assume, GetPtL (since the reference says it works similarly) don't return anything. You create a callback function and can use the point inside the callback function.  You can't set a global variable equal to the point, since the main script keeps running while the callback waits for the user to select a point. 

 

At this point, I've tossed vs.GetPtL out the window, but it would be helpful if someone on the Vectorworks team could properly document the function's parameters, and how many and what parameters are needed in the callback function.

Edited by Nik

Share this post


Link to post

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.


 

7150 Riverwood Drive, Columbia, Maryland 21046, USA   |   Contact Us:   410-290-5114

 

© 2018 Vectorworks, Inc. All Rights Reserved. Vectorworks, Inc. is part of the Nemetschek Group.

×
×
  • Create New...