Here's the code daz have to forgive me i'm new to lua so there is a lot of extra junk in there while I was testing...
like I said just seems to crash when run from a button press script
Gary
[-------------------------------------------------------------------------=---
-- Name: Mach4 caclulate
--
-- Author: Gary
-------------------------------------------------------------------------=---
inst = mc.mcGetInstance()
function GetNextID()
next_id = next_id+1
return next_id
end
WhatAxis = {
XaxisButton = { 'X' , "Motor0", 0 },
YaxisButton = { 'Y' , "Motor1", 1 },
ZaxisButton = { 'Z' , "Motor2", 2 },
AaxisButton = { 'A' , "Motor3", 3 },
BaxisButton = { 'B' , "Motor4", 4 },
CaxisButton = { 'C' , "Motor5", 5 },
}
next_id = 0
-- IDs of the controls in the dialog
ID_AUTOCALCUATE_BUTTON = GetNextID() -- NOTE: We use the fact that the textctrl ids
ID_AUTOCALCUATE_TEXTCTRL = GetNextID()
ID_ABOUT_BUTTON = GetNextID()
ID_CLOSE_BUTTON = GetNextID()
ID_SAVE_TEXTCTR = GetNextID()
ID_SAVE_BUTTON = GetNextID()
ID_ABORT_TEXTCTR = GetNextID()
ID_ABORT_BUTTON = GetNextID()
ID__MAX = GetNextID() -- max of our window ids
WhereAreWe = 0
-- Create the dialog
dialog = wx.wxDialog(wx.NULL, wx.wxID_ANY, "Mach4 Auto Caculate Motor Counts Per Units",
wx.wxDefaultPosition, wx.wxDefaultSize)
-- Create a wxPanel to contain all the buttons
panel = wx.wxPanel(dialog, wx.wxID_ANY)
-- Layout all the buttons using wxSizers
local mainSizer = wx.wxBoxSizer(wx.wxVERTICAL)
local staticBox = wx.wxStaticBox(panel, wx.wxID_ANY, "Axis Calculater")
local staticBoxSizer = wx.wxStaticBoxSizer(staticBox, wx.wxVERTICAL)
local flexGridSizer = wx.wxFlexGridSizer( 3, 3, 5, 0 )
local radioGridSizer = wx.wxFlexGridSizer( 3,6,0, 0 )
flexGridSizer:AddGrowableCol(1, 0)
-- Make a function to reduce the amount of duplicate code.
function AddConverterControl(name_string, button_text, textCtrlID, buttonID)
local staticText = wx.wxStaticText( panel, wx.wxID_ANY, name_string)
local textCtrl = wx.wxTextCtrl( panel, textCtrlID, "00.00", wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxTE_PROCESS_ENTER )
local button = wx.wxButton( panel, buttonID, button_text)
flexGridSizer:Add( staticText, 0, wx.wxALIGN_CENTER_VERTICAL+wx.wxALL+wx.wxST_NO_AUTORESIZE, 5 )
flexGridSizer:Add( textCtrl, 0, wx.wxGROW+wx.wxALIGN_CENTER+wx.wxALL, 5 )
flexGridSizer:Add( button, 0, wx.wxGROW+wx.wxALIGN_CENTER+wx.wxALL, 5 )
return textCtrl, button, staticText
end
CalculateTextCtrl, Calculate, staticTextCTRL = AddConverterControl("Enter Distabce to move Axis ", "Travel Diatance", ID_AUTOCALCUATE_TEXTCTRL, ID_AUTOCALCUATE_BUTTON)
SaveTextCtrl, Savebutton, SavestaticTextCTRL = AddConverterControl("Save ", "Save Units", ID_SAVE_TEXTCTR, ID_SAVE_BUTTON)
AbortTextCtrl, Abortbutton, AbortstaticTextCTRL = AddConverterControl("Abort ", "Abort Save", ID_ABORT_TEXTCTR, ID_ABORT_BUTTON)
flexGridSizer:Hide(Abortbutton)
flexGridSizer:Hide(Savebutton)
flexGridSizer:Hide(SavestaticTextCTRL)
flexGridSizer:Hide(AbortstaticTextCTRL)
flexGridSizer:Hide(SaveTextCtrl)
flexGridSizer:Hide(AbortTextCtrl)
staticBoxSizer:Add( flexGridSizer, 0, wx.wxGROW+wx.wxALIGN_CENTER+wx.wxALL, 5 )
mainSizer:Add( staticBoxSizer, 1, wx.wxGROW+wx.wxALIGN_CENTER+wx.wxALL, 5 )
local buttonSizer = wx.wxBoxSizer( wx.wxHORIZONTAL )
local aboutButton = wx.wxButton( panel, ID_ABOUT_BUTTON, "&About")
local closeButton = wx.wxButton( panel, ID_CLOSE_BUTTON, "E&xit")
buttonSizer:Add( aboutButton, 0, wx.wxALIGN_CENTER+wx.wxALL, 5 )
buttonSizer:Add( closeButton, 0, wx.wxALIGN_CENTER+wx.wxALL, 5 )
mainSizer:Add( buttonSizer, 0, wx.wxALIGN_CENTER+wx.wxALL, 5 )
function AddRadioControl(name_string)
local ID = GetNextID()
local sizer = wx.wxBoxSizer(wx.wxHORIZONTAL )
local Ctrl = wx.wxRadioButton( panel, ID, name_string, wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxTE_PROCESS_ENTER ,wx.wxTextValidator(wx.wxFILTER_NUMERIC))
radioGridSizer:Add( sizer, 0, wx.wxALIGN_CENTER_VERTICAL+wx.wxALL+wx.wxALIGN_RIGHT, 5)
radioGridSizer:Add( Ctrl, 0, wx.wxGROW+wx.wxALIGN_CENTER+wx.wxALL+wx.wxALIGN_LEFT, 5)
return Ctrl, ID
end
-- make the check boxs
--m_Test = AddCheckControl("Check me")
X_Axis = AddRadioControl("X Axis")
Y_Axis = AddRadioControl("Y Axis")
Z_Axis = AddRadioControl("Z Axis")
A_Axis = AddRadioControl("A Axis")
B_Axis = AddRadioControl("B Axis")
C_Axis = AddRadioControl("C Axis")
--mainSizer:Add( flexGridSizer, 0, wx.wxALIGN_CENTER+wx.wxALL, 2 )
staticBoxSizer:Add( radioGridSizer, 0, wx.wxGROW+wx.wxALIGN_CENTER+wx.wxALL, 5 )
panel:SetSizer( mainSizer )
mainSizer:SetSizeHints( dialog )
--Check what radio button is clicked if any and return all the axis info needed or nul
function CheckAxisButton()
if X_Axis:GetValue() == true then
return WhatAxis.XaxisButton[1], WhatAxis.XaxisButton[2], WhatAxis.XaxisButton[3]
elseif Y_Axis:GetValue() == true then
return WhatAxis.YaxisButton[1], WhatAxis.YaxisButton[2], WhatAxis.YaxisButton[3]
elseif Z_Axis:GetValue() == true then
return WhatAxis.ZaxisButton[1], WhatAxis.ZaxisButton[2], WhatAxis.ZaxisButton[3]
elseif A_Axis:GetValue() == true then
return WhatAxis.AaxisButton[1], WhatAxis.AaxisButton[2], WhatAxis.AaxisButton[3]
elseif B_Axis:GetValue() == true then
return WhatAxis.BaxisButton[1], WhatAxis.BaxisButton[2], WhatAxis.BaxisButton[3]
elseif C_Axis:GetValue() == true then
return WhatAxis.CaxisButton[1], WhatAxis.CaxisButton[2], WhatAxis.CaxisButton[3]
end
return nul
end
function AxisMove()
--wx.wxMessageBox("Here = "..WhereAreWe)
-- CalculateTextCtrl:SetValue("00.000")
-- staticTextCTRL:SetLabel("Enter Distance to move Axis")
-- Calculate:SetLabel("Travel Diatance")
AxisLetter, AxisName, AxisNumber = CheckAxisButton()
if AxisLetter == nul then return
end
AxisEnteredDistance = CalculateTextCtrl:GetValue() --Distance to move the axis
-- wx.wxMessageBox( 'Distanced = ' .. AxisEnteredDistance )
mc.mcAxisSetPos(inst, AxisNumber, 0); --"Zero Dro (0 is x, 1 y, and so on)
mc.mcCntlGcodeExecuteWait(inst, 'G01 '.. AxisLetter .. AxisEnteredDistance..'F4'); --G31. Machine will stop on probe strike or go to distance set
return 1
end
function AxisCalculate(AxisEnteredDistance)
--distance, rc = mc.mcAxisGetPos(inst ,0); --"Zero Z" (0 is x, 1 y, and so on)
--Steps, rc = mc.mcProfileGetDouble(inst,"Motor0", "CountsPerUnit", 0)
--Steps, rc = mc.mcProfileGetDouble(inst,"Motor0", "CountsPerUnit", 2000)
AxisDistanceTraveled = CalculateTextCtrl:GetValue()
--local distance = distance_travled
Steps = 1800
--local dro_pos = Distance_to_travel
local traveled = (AxisEnteredDistance * Steps)
local NewUnits = (traveled / AxisDistanceTraveled)
wx.wxMessageBox('Newsteps = ' ..tostring(NewUnits) .. '\nDistanced steps travled = ' .. tostring(AxisDistanceTraveled))
return NewUnits
end
function SaveUnits()
staticTextCTRL:SetLabel("Save units")
-- CalculateTextCtrl:SetValue("0.0023 ")
Calculate:SetLabel("Save")
--flexGridSizer:Hide(button)
AxisDistanceTraveled = CalculateTextCtrl:GetValue() --Distance to move the axis
wx.wxMessageBox( "Travel Distance = " .. AxisEnteredDistance .. "\nDistance Treveled = " ..AxisDistanceTraveled)
return 2
end
function AbortSaveUnits()
staticTextCTRL:SetLabel("Abort units")
CalculateTextCtrl:SetValue("0.000 ")
Calculate:SetLabel("Abort") --Distance to move the axis
HideItems()
return 3
end
function HideItems()
flexGridSizer:Hide(Abortbutton)
flexGridSizer:Hide(Savebutton)
flexGridSizer:Hide(SavestaticTextCTRL)
flexGridSizer:Hide(AbortstaticTextCTRL)
flexGridSizer:Hide(SaveTextCtrl)
flexGridSizer:Hide(AbortTextCtrl)
--CalculateTextCtrl:SetValue(" ")
--Calculate:SetLabel("Save")
return 0
end
function ShowItems()
flexGridSizer:Hide(Abortbutton)
flexGridSizer:Hide(Savebutton)
flexGridSizer:Hide(SavestaticTextCTRL)
flexGridSizer:Hide(AbortstaticTextCTRL)
flexGridSizer:Hide(SaveTextCtrl)
flexGridSizer:Hide(AbortTextCtrl)
--CalculateTextCtrl:SetValue(" ")
--Calculate:SetLabel("Save")
--CalculateTextCtrl:SetValue("0.0025 ")
return 0
end
--HideItems()
-- ---------------------------------------------------------------------------
-- Connect a handler for pressing enter in the textctrls
dialog:Connect(wx.wxID_ANY, wx.wxEVT_COMMAND_TEXT_ENTER,
function(event)
-- Send "fake" button press to do calculation.
-- Button ids have been set to be -1 from textctrl ids.
dialog:ProcessEvent(wx.wxCommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, event:GetId()-1))
end)
-- ---------------------------------------------------------------------------
-- Connect a central event handler that responds to all button clicks.
-- NOTE: Since we Connect() the about and close buttons after this they will be
-- called first and unless we call event:Skip() in their handlers the
-- events will never reach this function. Therefore we don't need to
-- check that the ids are only from buttons.
dialog:Connect(wx.wxID_ANY, wx.wxEVT_COMMAND_BUTTON_CLICKED,
function(event)
-- NOTE: A wxID_CANCEL event is sent when the close button on the
-- dialog is pressed.
if event:GetId() >= ID__MAX then
event:Skip()
return
end
-- We know that the textctrl window ids are +1 from the button ids
Number = tonumber(dialog:FindWindow(event:GetId()+1):DynamicCast("wxTextCtrl"):GetValue())
if Number == nil then
wx.wxMessageBox("The input is invalid, enter a number.",
"Error!",
wx.wxOK + wx.wxICON_EXCLAMATION + wx.wxCENTRE,
dialog)
else
-- Create a "case" type statement
local AutoCalculate = {
[ID_AUTOCALCUATE_BUTTON] = function() if (WhereAreWe == 0 and CheckAxisButton() ~= nul) then AxisMove()
staticTextCTRL:SetLabel("Enter Distance Axis has Traveled")
Calculate:SetLabel("Calculate")
CalculateTextCtrl:SetValue("") return 1
elseif (WhereAreWe == 1 and CheckAxisButton() ~= nul) then NewUnits = AxisCalculate(AxisEnteredDistance)
CalculateTextCtrl:SetValue(tostring(NewUnits))
Calculate:SetLabel("Save")
staticTextCTRL:SetLabel(" New Motor Counts Per Units = ".."\nDistance= " .. AxisEnteredDistance.. " Distance Traveled= " ..AxisDistanceTraveled)
return 2
elseif (WhereAreWe == 2 and CheckAxisButton() ~= nul) then SaveUnits()
staticTextCTRL:SetLabel("Enter Distance to move Axis")
CalculateTextCtrl:SetValue("00.00")
Calculate:SetLabel("Travel Diatance") return 0
end
end
}
-- call the "case" statement
WhereAreWe = AutoCalculate[event:GetId()]()
end
end)
-- ---------------------------------------------------------------------------
-- Attach an event handler to the Close button
dialog:Connect(ID_CLOSE_BUTTON, wx.wxEVT_COMMAND_BUTTON_CLICKED,
function(event) dialog:Destroy() end)
dialog:Connect(wx.wxEVT_CLOSE_WINDOW,
function (event)
dialog:Destroy()
event:Skip()
end)
-- ---------------------------------------------------------------------------
-- Attach an event handler to the About button
dialog:Connect(ID_ABOUT_BUTTON, wx.wxEVT_COMMAND_BUTTON_CLICKED,
function(event)
wx.wxMessageBox("Mach4 Axis Calulate Motor Units.\n",
"\n",
wx.wxOK + wx.wxICON_INFORMATION,
dialog)
end)
dialog:Connect(ID_SAVE_BUTTON, wx.wxEVT_COMMAND_BUTTON_CLICKED,
function(event)
SaveUnits()
end)
dialog:Connect(ID_ABORT_BUTTON, wx.wxEVT_COMMAND_BUTTON_CLICKED,
function(event)
AbortSaveUnits()
end)
-- ---------------------------------------------------------------------------
-- Send a "fake" event to simulate a button press to update the textctrls
--dialog:ProcessEvent(wx.wxCommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED,ID_AUTOCALCUATE_BUTTON))
-- ---------------------------------------------------------------------------
-- Centre the dialog on the screen
dialog:Centre()
-- Show the dialog
dialog:Show(true)
-- Call wx.wxGetApp():MainLoop() last to start the event loop,
-- MainLoop is already running or will be started.
wx.wxGetApp():MainLoop()
/code]script...