Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: DAAD on April 23, 2020, 03:38:43 AM

Title: Cancel function not working in messagebox?
Post by: DAAD on April 23, 2020, 03:38:43 AM
If've added ,16 to the function and get the ok and cancel option, but it aparantly does not matter.
When pushing ok or cancel, it both executes the script. So i have no option to abort when needed.

Code: [Select]
function ProbeMBZ()
mc.mcCntlSetLastError(inst, "Goto machine bed probe point")
local val = mc.mcCntlGetPoundVar(inst, mc.SV_MOD_GROUP_14) --PoundVar 4014 coordinates sytem
local msg = "G00 G90 G53 Z-20 A-20\n G59 X0 Y0\nG"..val--Use G59 as workpiece zero for position Return to current fixture
mc.mcCntlGcodeExecute(inst, msg) --Use G59 as workpiece zero for position Return to G54
wx.wxMessageBox("Load the correct tool & lower the bit within 50mm before probing. Attach the MAGNET!!!","Abort if needed",16)
mc.mcCntlSetLastError(inst, "Probing in progress!")
mc.mcCntlGcodeExecuteWait(inst,"G91 G31 Z-50 F200")
--local ToolSet = -12.8 -- Toolset plate difference machine bed / plate
mc.mcAxisSetPos(inst, mc.Z_AXIS, ToolSet)
mc.mcCntlGcodeExecute(inst,"G00 G90 G53 Z-20")
end

if (mc.mcInEditor() == 1) then
ProbeMBZ()
end

Suggestions?

Keef Safe,

David
Title: Re: Cancel function not working in messagebox?
Post by: mcardoso on April 23, 2020, 11:43:39 AM
David,

When you use a message box and want the program to do something based on which button is clicked, you need to manually code those conditions. When a button in the message box is clicked, the message box function returns with a value and continues to execute the script. If you don't check the return value, then the program just keeps chugging along.

If you want the cancel button to actually cancel the function, then you need to program an IF statement than looks if the returned value is equal to "cancel" and return from the function early. The message box does not have any of this functionality built in automatically.

https://docs.wxwidgets.org/stable/group__group__funcmacro__dialog.html#ga193c64ed4802e379799cdb42de252647

Copied from that site:

Code: [Select]
int answer = wxMessageBox("Quit program?", "Confirm", wxYES_NO | wxCANCEL, main_frame);
if (answer == wxYES)
    main_frame->Close();

Now obviously the syntax needs to be modified and adjusted for LUA, but see how they capture the return value in "answer"? This lets them check if it is equal to wxYES (really just a constant integer value like 1 or 2, I just forgot which - have a cheat sheet somewhere), and do something based on that information.

Title: Re: Cancel function not working in messagebox?
Post by: jbuehn on April 23, 2020, 12:38:33 PM
Adding to mcardoso's great explanation, if I remember correctly the style "16" you're using will return either wx.wxOK or wx.wxCANCEL
Title: Re: Cancel function not working in messagebox?
Post by: DAAD on April 26, 2020, 09:53:07 AM
thanks for the info.

I've found this topic with some more info:

https://www.machsupport.com/forum/index.php?topic=36207.0 (https://www.machsupport.com/forum/index.php?topic=36207.0)

So i need RC 4 and 16.
at the moment i got it partially working see code blow. Not perfect yet, because i've get Two times the messagebox. The first one reacts onto the choise, the second one does not matter. How do i get lost of the second box?

Code: [Select]
function zControl.ProbeMBZ()
mc.mcCntlSetLastError(inst, "Goto machine bed probe point")
local val = mc.mcCntlGetPoundVar(inst, mc.SV_MOD_GROUP_14) --PoundVar 4014 coordinates sytem
local msg = "G00 G90 G53 Z-20 A-20\n G59 X0 Y0\nG"..val--Use G59 as workpiece zero, return to current fixture offset
mc.mcCntlGcodeExecute(inst, msg) --Use G59 as workpiece zero for position Return to G54
local rc = wx.wxMessageBox("Load the correct tool & lower the bit within 50mm before probing. Attach the MAGNET!!!",16)
wx.wxMessageBox ("Load the correct tool & lower the bit within 50mm before probing. Attach the MAGNET!!!",16)
if (rc == 4) then
mc.mcCntlSetLastError(inst, "Probing in progress!")
mc.mcCntlGcodeExecuteWait(inst,"G91 G31 Z-50 F200")
mc.mcAxisSetPos(inst, mc.Z_AXIS, ToolSet)
mc.mcCntlGcodeExecute(inst,"G00 G90 G53 Z-20")
elseif (rc==16) then
end
end
Title: Re: Cancel function not working in messagebox?
Post by: mcardoso on April 27, 2020, 09:17:53 AM
Well you have (2) copies of the message box (and the second one doesn't do anything or collect a return code).

I think you want to delete or comment out the line that says exactly:
Code: [Select]
wx.wxMessageBox ("Load the correct tool & lower the bit within 50mm before probing. Attach the MAGNET!!!",16)
I think the line you intended to use is right above it:)

Try to add some spaces between the lines of your code and add lots of comments - even line by line if needed. This will force you to really understand what you are doing with each line of code rather than it looking like a giant block of "magic".
Title: Re: Cancel function not working in messagebox?
Post by: mcardoso on April 27, 2020, 09:19:34 AM
Typically, I handle errors and optional user cancel operations by checking them first and returning from the function early. Pseudo code would look like this

Code: [Select]
function doThis()

errorCode = myFunction()

if errorCode then
    return  -Exit the program early
else
    *do the rest of the program*
    return  --Program would normally exit here
end
end

Ideally I would also include an error code in the return so I could figure out if it returned normally, or with errors. Something like this:

Code: [Select]
function doThis()

myError = 0
errorCode = myFunction()

if errorCode then
    myError = 1
    return  myError -Exit the program early (error code is 1)
else
    *do the rest of the program*
    return myError --Program would normally exit here (error code is 0)
end
end
Title: Re: Cancel function not working in messagebox?
Post by: DAAD on May 04, 2020, 12:49:24 AM
Well you have (2) copies of the message box (and the second one doesn't do anything or collect a return code).

I think you want to delete or comment out the line that says exactly:
Code: [Select]
wx.wxMessageBox ("Load the correct tool & lower the bit within 50mm before probing. Attach the MAGNET!!!",16)
I think the line you intended to use is right above it:)

Try to add some spaces between the lines of your code and add lots of comments - even line by line if needed. This will force you to really understand what you are doing with each line of code rather than it looking like a giant block of "magic".


Thanks for the reply,

I've tried different possibilities but can't get the cancel button working.
at the moment it's only working when i use the code as posted, but then i got the double message.

If i remove one line or edit the second line i get an lua chunck error.

To bad there is no manual on howto get this setup properly. It should be something simple...

Title: Re: Cancel function not working in messagebox?
Post by: joeaverage on May 04, 2020, 12:58:55 AM
Hi

wxMessageBox is a wxWidgets function, it has nothing to do with Mach4.....and there is plenty of documentation about wxWidgets.

The documentation is for Geeks, written by Geeks and in pure Geekese, but there plenty of it.

https://docs.wxwidgets.org/3.0/ (https://docs.wxwidgets.org/3.0/)

Craig
Title: Re: Cancel function not working in messagebox?
Post by: DAAD on May 04, 2020, 12:59:58 AM
thanks,

i learn something new every day. will look into it!
Title: Re: Cancel function not working in messagebox?
Post by: bcoop on May 04, 2020, 07:37:17 PM
This works for me.


local rc = wx.wxMessageBox("Load the correct tool & lower the bit within 50mm before probing. Attach the MAGNET!!!","Abort if needed",16)

if rc == 4 then
wx.wxMessageBox('Returned 4 pressed OK')
elseif rc == 16 then
wx.wxMessageBox('Returned 16 pressed cancel')

end

all the best

Bob
Title: Re: Cancel function not working in messagebox?
Post by: DAAD on May 14, 2020, 11:02:50 AM
thanks for the input.

did mess around with it, but even if i copy paste the gcode above i still get an chunk error.