Bertf Posted February 3, 2016 Share Posted February 3, 2016 (edited) 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 February 3, 2016 by Bertf Quote Link to comment
JBenghiat Posted February 3, 2016 Share Posted February 3, 2016 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 1 Quote Link to comment
Bertf Posted March 24, 2016 Author Share Posted March 24, 2016 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 ) Quote Link to comment
Dieter @ DWorks Posted March 25, 2016 Share Posted March 25, 2016 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)) Quote Link to comment
BillW Posted March 30, 2016 Share Posted March 30, 2016 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. Quote Link to comment
Bertf Posted April 25, 2016 Author Share Posted April 25, 2016 Thank you BillW! x1,y1 = vs.vstGetCurrPt2D() -> This works! Quote Link to comment
ahedley Posted September 23, 2016 Share Posted September 23, 2016 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 ? Quote Link to comment
twk Posted September 23, 2016 Share Posted September 23, 2016 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 Quote Link to comment
Dieter @ DWorks Posted September 23, 2016 Share Posted September 23, 2016 Why not store it in a var of the containing scope? user_point = None def catch_point(pt): user_point = pt vs.GetPt(catch_point) vs.Locus(user_point) vs.Message(user_point[0],' ',user_point[1]) vs.ReDraw() Please take a look at https://bitbucket.org/dieterdworks/vw-dlibrary, this would be an ideal thing to put in it. Quote Link to comment
ahedley Posted September 23, 2016 Share Posted September 23, 2016 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. Quote Link to comment
Dieter @ DWorks Posted September 24, 2016 Share Posted September 24, 2016 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. Quote Link to comment
ahedley Posted September 24, 2016 Share Posted September 24, 2016 Thanks Dieter Quote Link to comment
Ryan McCuaig Posted September 30, 2016 Share Posted September 30, 2016 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. 1 Quote Link to comment
ahedley Posted September 30, 2016 Share Posted September 30, 2016 Ryan, import vs vs.GetPt(lambda pt: vs.Message(str(pt))). Crashes vectorworks 2017 windows. Adrian Quote Link to comment
Ryan McCuaig Posted October 4, 2016 Share Posted October 4, 2016 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) Quote Link to comment
ahedley Posted October 11, 2016 Share Posted October 11, 2016 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. Quote Link to comment
Dieter @ DWorks Posted October 11, 2016 Share Posted October 11, 2016 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. Quote Link to comment
ahedley Posted October 11, 2016 Share Posted October 11, 2016 My apologies, I meant plug-in tools. Quote Link to comment
Dieter @ DWorks Posted October 12, 2016 Share Posted October 12, 2016 16 hours ago, ahedley said: My apologies, I meant plug-in tools. No prob. Quote Link to comment
Nik Posted April 15, 2020 Share Posted April 15, 2020 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) Quote Link to comment
Pat Stanford Posted April 15, 2020 Share Posted April 15, 2020 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. Quote Link to comment
Nik Posted April 16, 2020 Share Posted April 16, 2020 (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 April 16, 2020 by Nik Quote Link to comment
The Hamma Posted November 6, 2022 Share Posted November 6, 2022 This is how it works: import vs; def DoIt(h1): vs.AlrtDialog( "we're in", h1 ) def PickPointCallback(pt): vs.ForEachObjectAtPoint(DoIt, 0, 0, pt[0], pt[1], 5) vs.AlrtDialog( "show let you pick a point, and then show a dialog with the object's handle" ) vs.GetPt( PickPointCallback ) You need to add your function to the (PickPointCallback) definition part of the script to evaluate pt[0] and pt[1] Quote Link to comment
Pat Stanford Posted November 6, 2022 Share Posted November 6, 2022 Be careful about using Do It as your function name, especially if you are posting here to the list. It looks fine if you use a serif font as you have above. But when you switch to a sans serif font it looks like Dolt. DOLT. As in a fool. I used that one time to try and help someone and they though that I had named the procedure DOLT to insult them for not knowing how to do whatever it was. 🤦♂️😂 I now use Execute as my standard function name instead. 2 Quote Link to comment
Recommended Posts
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.