Jump to content
Benedick_Miller

Number stamp tool with python - how?

Recommended Posts

Hi,

Some years ago I wrote a few tools in Vectorscript based on a number stamp. The idea is simple enough: enter a starting number in a dialog, start clicking, the number increments and is "printed" in the drawing on each mouse click. In the actual implementation there is other stuff going on too, but it's just this part I need help with at the moment.

 

The Vectorscript tools work fine, but now I would like to re-write them in Python, but I can't get it working at all! This is because I am probably too stupid to understand what I need to do, but also it is because of the way GetPt() seems to works in Python. Also I don't get how to use KeyDown() in Python. I am open to any suggestion on how to do this in python in any way it works:

  • Enter a starting number in a dialog
  • Start clicking to add numbers (as text) that increment with each mouse click.

It seems like this should be simple enough, but I have been trying to get it to work for a while and i can't! Any help would be much appreciated.

 

Here, below, is a simplified version of my Vectorscript that works fine, and below that one example of how I have tried (and failed miserably) to translate it to Python:

 

{
V4
Simple Number Stamp Tool
By: Benedick Miller
Date: 2014-10-06
}

Procedure SimpleNumberStamp;

	VAR 
		n,i,keyhit:INTEGER;
		pt:POINT;
		t:STRING;				

	BEGIN
		
		n:=IntDialog('Enter the starting number','1');
		IF NOT DidCancel THEN
		keyhit:=27;
		i:=1;
		DSelectAll;
					
		while not keydown(keyhit) do
		
		begin
			t:=num2Str(0,n);	
			getpt(pt.x,pt.y);
			TextOrigin(pt.x,pt.y);
			CreateText(t);				
			n:=n+i;
			redrawall;
		end;
		
	END;

Run(SimpleNumberStamp);

And the Python translation:

# Print Incremental Numbers on each mouse click until [Esc]
# NOT WORKING!!!

import vs

def label(pt):
	vs.TextOrigin(pt[0],pt[1])
	vs.CreateText(str(n))
	vs.ReDraw()
	
def kdwn():
	k=vs.KeyDown()
	return k[1]

keyhit = 27
i = 1
n = 1

vs.DSelectAll()

n = vs.IntDialog('Enter the starting number', n ) 
if not vs.DidCancel():
	while not kdwn() == keyhit:
		vs.GetPt(label)
		n = n + i

 

Edited by Benedick_Miller
cleaning up typos, moved vs.KeyDown() to a callback

Share this post


Link to post

Hello Benedick

 

I tryed a few possible solutions but get no results.

 

The main problem is that "in Python this function will NOT block execution. It will execute a callback function with the resulted point"

http://developer.vectorworks.net/index.php/VS:GetPt

 

So I tryed to get around with a second while loop like:

 

mdwn = False

mdwn, na = vs.MouseDown()
        n = n + i
        if mdwn == True:
            continue

 

But it  iterates over and over.

 

So maybe in a future version vs.GetPt() or vs.MouseDown() is like a dialog userinput.

 

Silas

Share this post


Link to post

Hi Silas,

 

thanks for trying and replying.

 

As you say - "in Python this function will NOT block execution. It will execute a callback function with the resulted point" - I did read that but i was hoping there was some way around that.

 

I'll stick to Vectorscript for now for this tool.

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

 

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.

×