Custom M-Code (Python)

More
01 Dec 2017 12:14 #102560 by andypugh
andypugh replied the topic: Custom M-Code (Python)
I have updated the original post to add named params. They need to be global ( ie, #<_paramname> ) to be visible outside the remap, that was where i was going wrong. The code now prepends the _ character.
Also added sample G-code.

It still needs the user to close the dialog with the X before it continues with the code.
More
01 Dec 2017 12:20 #102561 by andypugh
andypugh replied the topic: Custom M-Code (Python)
And, also, the first version showed the dialog during preview, which is no good.

Fun extra feature to add, populate the dialog box with the current value if it exists.
The following user(s) said Thank You: Todd Zuercher
More
01 Dec 2017 13:47 - 01 Dec 2017 13:51 #102565 by Todd Zuercher
Todd Zuercher replied the topic: Custom M-Code (Python)
Thanks again,

Busy right now, but I hope to play with this some this afternoon.

I'll probably change it to M20-22. M10 and M11 are often used for clamp/unclamp on alot of machines. (but that is nit-picky stuff easily changed.)
Last Edit: 01 Dec 2017 13:51 by Todd Zuercher.
More
01 Dec 2017 18:25 #102571 by Todd Zuercher
Todd Zuercher replied the topic: Custom M-Code (Python)
Finally had some time to play with it. Here are the problems I'm seeing.

I can't seem to get it to populate the input box with an existing value.

If I re-run the g-code file (without an M12 in it) it adds new entry boxes to the old ones.

Is there a way to make it give the parameter a number if one isn't specified with the m10?
More
02 Dec 2017 00:11 #102578 by andypugh
andypugh replied the topic: Custom M-Code (Python)

Todd Zuercher wrote: Finally had some time to play with it. Here are the problems I'm seeing.
I can't seem to get it to populate the input box with an existing value.


I just put up a new version which does this.

If I re-run the g-code file (without an M12 in it) it adds new entry boxes to the old ones


That's a feature. It means you can re-use the dialog box. Use M12 if you don't want to append.

[/quote]Is there a way to make it give the parameter a number if one isn't specified with the m10?[/quote]

In the new version, just set the param in the G-code before setting up the dialog box
More
02 Dec 2017 00:27 #102580 by andypugh
andypugh replied the topic: Custom M-Code (Python)
And the dialog box now closes properly
More
04 Dec 2017 17:55 - 04 Dec 2017 18:01 #102672 by Todd Zuercher
Todd Zuercher replied the topic: Custom M-Code (Python)
Thanks Andy,

I made a couple of minor modifications to it.
Changed the M codes to M20-22 and made it so the focus is on the OK button when the window pops up and bound the return key to the button. Also made it so m20 resets, m21 adds an entry, and m22 calls.
That way the user can just hit the space bar or press enter to continue if no changes are needed to the default entry, instead of having to tab to the OK or find it with the mouse.
#!/usr/bin/python
import sys
from interpreter import *
from Tkinter import *

list = []

# Add an entry
def m21(self, **words):
	global list
	if not self.task: return INTERP_OK
	list.append(self.blocks[self.remap_level].comment)
	return INTERP_OK

def done(self, dialog, entries):
	for entry in entries:
		val = entry[1].get()
		if val.isdigit:
			self.params[entry[0]] = float(val)
	dialog.update()
	dialog.destroy()

# Show the entries
def m22(self, **words):
	if not self.task: return INTERP_OK
	dir(self)
	global list
	entries = []
	row = 1
	if not hasattr(sys, 'argv'):
		sys.argv  = ['']
	dialog = Tk()
	dialog.title(self.blocks[self.remap_level].comment)
	for item in list:
		ret = item.split(";")
		prompt = ret[0]
		if len(ret) == 1:
			param = "_" + prompt.replace(" ", "")
		else:
			param = "_" + ret[1].replace(" ", "")
		Label(dialog, text=prompt).grid(row=row)
		entry = Entry(dialog)
		entry.grid(column=1, row=row)
		try:
			entry.insert(0, self.params[param])
		except:
			pass
		entries.append((param, entry))
		row += 1
	b1 = Button(dialog, text='OK', command=lambda: done(self, dialog, entries))
	b1.grid(row=row, column=0, sticky=W, pady=4)
	b1.focus()
	b1.bind('<Return>', lambda x: done(self, dialog, entries))
	mainloop()
	return INTERP_OK

def m20(self, **words):
	global list
	list = []
	return INTERP_OK
Last Edit: 04 Dec 2017 18:01 by Todd Zuercher.
Time to create page: 0.084 seconds
Powered by Kunena Forum