Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: Cbyrdtopper on December 21, 2017, 09:22:21 AM

Title: Message Box with Cancel?
Post by: Cbyrdtopper on December 21, 2017, 09:22:21 AM
So the simple wx.messagebox is great.  But is there a wx.widget that gives me a message box with "Okay" and "Cancel" as options to choose?
Title: Re: Message Box with Cancel?
Post by: Chaoticone on December 21, 2017, 09:47:11 AM
local rc = wx.wxMessageBox("This is my Message", "This is my Caption",16) --Ok, Cancel

--wxMessageBox Types
--2 = Yes, No
--4 = Ok
--16 = Ok, Cancel
--18 = Yes, No, Cancel

--wxMessageBox Return Values
--Yes = 2
--OK = 4
--No = 8
--Cancel = 16
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on December 21, 2017, 10:04:30 AM
Yes!   Awesome, I'm going to try this once I get back to the shop today. 
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 08, 2018, 10:00:31 AM
So,  I've got another message box question.
Is there a way I can have a user type in a number in a message box, and then use that number to set an offset?
Title: Re: Message Box with Cancel?
Post by: Chaoticone on February 08, 2018, 11:25:06 AM
Sure is..........

local MyNum = wx.wxGetNumberFromUser("Message", "Prompt", "Caption", 50, 0, 100) --Default, min, max

Or you can get it as a string and then convert it to a number. Can do some checks with either, flip numbers, etc. Goods and bads using both methods. Just have to figure out what works best for you and your condition.

local MyText = wx.wxGetTextFromUser("Message", "Caption", "Default Value")

Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 08, 2018, 11:33:03 AM
Okay.  Very nice.  I'll have to play with it and see what works well.
Just to let you know what I'm trying to accomplish:
On the lathe we are retrofitting, I want to prompt the operator on startup to enter the current tool in the turret.  Just in case Mach4 is shut down before retaining the current tool. 
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 08, 2018, 11:39:27 AM
Excellent!
Works perfectly!  Since I'm using a number.  wx.wxGetNumberFromUser works great!
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 08, 2018, 11:53:20 AM
Do you know what the Return Values are for this, I can't seem to find them?  If the operator hits "cancel" I want to Pop up the User Input Box again.
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 08, 2018, 12:02:46 PM
Okay, So, I've found the Return Value I need.  But how do I get my syntax correct.  

local ActualTool, rc = wx.wxGetNumberFromUser()
if rc == -1 then
mc.mcCntlSetLastError(inst, "ERROR")
else
mc.mcToolSetCurrent(inst, ActualTool)
end

This isn't working correctly.  When I hit cancel, it puts a "-1" in my current tool and skips over the    if rc == -1
Obviously I have something wrong.  

If I just have rc =wx.wxGetNumberFromUser() everything works properly.  But I wold rather have the variable "Actual Tool" instead of just "rc"

Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 08, 2018, 01:04:25 PM
I'm not sure if this is the "correct" way to do this.  But it works for my proof of concept.

--What is the current tool?
function m110()

local inst = mc.mcGetInstance()
local ActualTool = wx.wxGetNumberFromUser("What tool is in the turret?", "Tool # ","Current Tool", 1, 1, 6) -- Default, Min, Max.
local rc = ActualTool
if rc == -1 then
wx.wxMessageBox("Set the current tool to the actual tool.")
else
mc.mcToolSetCurrent(inst, ActualTool)
end

end --m110

if (mc.mcInEditor() == 1) then
 m110()
end
Title: Re: Message Box with Cancel?
Post by: Chaoticone on February 08, 2018, 02:36:17 PM
I'm not sure what your exact flow is but you may need to do a couple of message boxes. One ask a question........ then pop up next based on response. Could have lots of options for next. Might even need more steps which = more message boxes. I hope that makes sense.
Title: Re: Message Box with Cancel?
Post by: rhtuttle on February 08, 2018, 02:52:29 PM
Correct me if i'm wrong but wx.wxGetNumberFromUser() only works with integers. 

hth

rt
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 08, 2018, 03:06:17 PM
Brett,
I only put the 2nd wx.wx.messagebox in there to test with.  I will make it loop until the operator puts a number in there, I'm forcing them to set the current tool to the actual tool in turret rather than just assuming the operator will be sure to put the correct tool number in the DRO.  This code will run only on the start up. 

Speaking of running only 1 time.
I put this in the "First Run PLC Script."  It didn't crash and as far as I can tell it didn't alter anything else.  Is that okay?  Or is there a better way to make something run only on startup?   Open to any and all suggestions. 

Also, I thought we could use Global Variables (like "testcount" in PLC Script) everywhere, even in a macro.  It won't let me.
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 08, 2018, 03:07:46 PM
Correct me if i'm wrong but wx.wxGetNumberFromUser() only works with integers. 

hth

rt

I'm using this to get the "Actual Tool" that is in the turret, so all I need is the integer. 
This box pops up, the operator looks at the turret sees that "Tool #" is the current tool, they type it in the message box and it updates the Current Tool DRO.
Title: Re: Message Box with Cancel?
Post by: rhtuttle on February 08, 2018, 03:11:23 PM
Yes, but you asked also asked for a way to ask the user for a number for a 'position'.  Thought I would save someone in the future reading this thread some time so they don't try it for that.
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 08, 2018, 03:12:40 PM
Yes, but you asked also asked for a way to ask the user for a number for a 'position'.  Thought I would save someone in the future reading this thread some time so they don't try it for that.

Gotcha!   ;)
Title: Re: Message Box with Cancel?
Post by: Chaoticone on February 08, 2018, 03:20:00 PM
Quote
I put this in the "First Run PLC Script."  It didn't crash and as far as I can tell it didn't alter anything else.  Is that okay?  Or is there a better way to make something run only on startup?   Open to any and all suggestions.  

Yes, that is acceptable. I would build the function in the screen load script and call that function only once from the PLC script by doing like you have in first run or maybe even third run.

Quote
Also, I thought we could use Global Variables (like "testcount" in PLC Script) everywhere, even in a macro.  It won't let me.

I like calling them regionally global.  ;D

Macros and screens run in 2 different instances so ones global is not global to the other. The only thing (that i know of) right now that is global global are registers. They can be read and written to from the screen, macros, modules, lua panels, etc.
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 08, 2018, 03:34:25 PM
Quote
I put this in the "First Run PLC Script."  It didn't crash and as far as I can tell it didn't alter anything else.  Is that okay?  Or is there a better way to make something run only on startup?   Open to any and all suggestions. 

I like calling them regionally global.  ;D

Brett, I like it! lol :)
I thought about making a function.  It would make it a little cleaner.  I'll give that a try next. 
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 08, 2018, 04:45:34 PM
This is working great!
I went ahead and made it a function in the screen load script; just to tidy things up.  

I also put it on the 25th scan.  

if testcount == 25 then
SetActualTool()
end

It was popping up right as Mach4 was initializing, and it was hiding it behind Mach.  So, moved it to 25 and it pops up on the screen just fine, in about 1.5 seconds. 
Title: Re: Message Box with Cancel?
Post by: Chaoticone on February 08, 2018, 05:44:14 PM
 :)
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 09, 2018, 10:00:56 AM
So,
to make this loop, I was going to do something like this:

while rc == -1 do
local ActualTool = wx.wxGetNumberFromUser()
local rc = ActualTool
end

but it won't change the rc inside the loop.  I changed the variable name to Test and it returned an ERROR.   Why is that?

While writing this reply, I was testing the loop again.  It isn't even changing my variable ActualTool from the GetNumberFromUser. 
Title: Re: Message Box with Cancel?
Post by: DazTheGas on February 09, 2018, 02:52:37 PM
Try using the repeat command like this

Code: [Select]
local inst = mc.mcGetInstance()
repeat
    local ActualTool = wx.wxGetNumberFromUser("What tool is in the turret?", "Tool # ","Current Tool", 1, 1, 6) -- Default, Min, Max.
until ActualTool ~= -1
--mc.mcToolSetCurrent(inst, ActualTool)
wx.wxMessageBox(tostring(ActualTool))

DazTheGas
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 09, 2018, 04:26:02 PM
Thanks Daz.  That's exactly what I was looking for.  I had no idea that command even existed!! :)
Title: Re: Message Box with Cancel?
Post by: Chaoticone on February 09, 2018, 05:28:06 PM
Thanks Daz.  That's exactly what I was looking for.  I had no idea that command even existed!! :)

You will love this then.......

http://www.lua.org/manual/5.2/
Title: Re: Message Box with Cancel?
Post by: joeaverage on February 09, 2018, 07:28:15 PM
Hi,
Chaoticone's link to the Lua 5.2 manual is great, I usually have it open when I'm doing any coding, I still have to delve into it a lot.

Just be warned there is a Lua 5.3 and some things change and can and will trip you up if you blithely believe that Mach can handle 5.3.

Craig
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 10, 2018, 10:34:09 AM
Thanks for the info, Brett and Craig.
I've looked through that manual before.  I don't know why I didn't think to look for a "Repeat" command.  Couldn't see the forest for the trees I guess.
Craig,
I'm guessing Mach uses the 5.2 stuff then?
Title: Re: Message Box with Cancel?
Post by: DazTheGas on February 10, 2018, 10:49:23 AM
HAHAHAHA Once youve finished with the Lua Manual you can then move on to wxLua Manual  :o ;D :D ;)

DazTheGas
Title: Re: Message Box with Cancel?
Post by: Chaoticone on February 10, 2018, 11:00:44 AM
HAHAHAHA Once youve finished with the Lua Manual you can then move on to wxLua Manual  :o ;D :D ;)

DazTheGas

Very true Daz. If the reference manual makes you happy the wxLua manual might put you over the edge.  ;D

In the reference manual, look at http://www.lua.org/manual/5.2/manual.html#9
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 10, 2018, 12:33:55 PM
After testing the "Repeat" Command.  It isn't working as expected.  When I run through the code, it returns an ERROR when it reads "while actualtool ~=1"  After I input a number in the box, it reads that number just fine.  But as soon as the debugger gets on the "While" line, it returns an ERROR in the ActualTool Variable.
Title: Re: Message Box with Cancel?
Post by: DazTheGas on February 10, 2018, 01:26:01 PM
post the whole code you are using as I have tested what i posted and all works.

DazTheGas
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 10, 2018, 01:40:07 PM
Daz,
Here is my entire macro.  The repeat command works, to test this I kept hitting cancel and it pops back up as expected. 

However, when I input a number, it acknowledges that number until it reads the "until" line, then the variable "ActualTool1" returns an ERROR instead of #.

--What is the actual tool?
function m110()
local inst = mc.mcGetInstance()

repeat
     local ActualTool1 = wx.wxGetNumberFromUser("What tool is in the turret?", "Tool # ","Current Tool", 1, 1, 6) -- Default, Min, Max.
until (ActualTool1 ~= -1)

mc.mcToolSetCurrent(inst, ActualTool1)

end --m110

if (mc.mcInEditor() == 1) then
 m110()
end
Title: Re: Message Box with Cancel?
Post by: DazTheGas on February 10, 2018, 02:01:59 PM
Oh I see now, der me....   you cannot run any of the getnumberfromuser or textfromuser from a macro. just remembered I tried to use this a while back. the lua stack is a cutdown version and wont allow dialogs like this.

DazTheGas
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 10, 2018, 02:33:07 PM
 This was allowing me to get text and numbers and use them.   Just not inside the repeat command.

 
Title: Re: Message Box with Cancel?
Post by: Chaoticone on February 10, 2018, 02:53:36 PM
You might try this.... instead of doing the repeat loop in the macro, have a separate function to check the number entered and another function to pop up the message box if needed.
Title: Re: Message Box with Cancel?
Post by: DazTheGas on February 10, 2018, 03:19:09 PM
Here you go http://www.dazthegas.co.uk/forumanswers/m110.mp4 perhaps this will explain the error i was on about.

DazTheGas
Title: Re: Message Box with Cancel?
Post by: Cbyrdtopper on February 10, 2018, 05:10:27 PM
Daz,
Thanks for the video.   I had that error pop up yesterday, didn't know what it was.   I'm guessing once you close it out the first time, you're good to go until Mach gets shutdown and turned back on again?