----------------------------------------------------------------------------- -- Name: Lathe Drill -- Author: David Patton -- Modified by: -- Created: 02/18/2015 ----------------------------------------------------------------------------- function GetNextID() m_id = m_id+1 return m_id end --global var to hold the frame mainframe = nil panel = nil m_id = 0 m_iniName = "LatheDrill" ID_GENGCODE_BUT = GetNextID() ID_CLOSE_BUTTON = GetNextID() m_image = wx.wxGetCwd() .. "\\Wizards\\LatheDrill.png" function Setupinputs() --Add all the inputs local val m_z_home = AddInputControl("Z Home", nil) m_z_home:SetValue( mc.mcProfileGetString(0 , tostring(m_iniName), "ZHome", "5") ) m_z_depth = AddInputControl("Hole Depth", nil) m_z_depth:SetValue( mc.mcProfileGetString(0 , tostring(m_iniName), "Depth", "-1.1200") ) m_rapid_height = AddInputControl("Rapid Z In", nil) m_rapid_height:SetValue( mc.mcProfileGetString(0 , tostring(m_iniName), "RapidHeight", "1.000") ) m_retract_height = AddInputControl("Retract Z Out", nil) m_retract_height:SetValue( mc.mcProfileGetString(0 , tostring(m_iniName), "RetractHeight", ".100") ) m_peck_depth = AddInputControl("Peck Depth", nil) m_peck_depth:SetValue( mc.mcProfileGetString(0 , tostring(m_iniName), "PeckDepth", ".125") ) m_cycletype, ID_CYCLE_TYPE = AddSelectControl("Drill cylce", {"G81 Single pass", "G83 Peck Drill"}, ID_CYCLE_TYPE) local val = mc.mcProfileGetString(0 , tostring(m_iniName), "Cycle", "0") m_cycletype:SetSelection(tonumber(val)) m_feedtype, ID_FEED_TYPE = AddSelectControl("Feed Type", {"FPM G98", "FPR G99"}, ID_FEED_TYPE) local val = mc.mcProfileGetString(0 , tostring(m_iniName), "Feedt", "0") m_feedtype:SetSelection(tonumber(val)) m_feedrate = AddInputControl("Feed", nil) m_feedrate:SetValue( mc.mcProfileGetString(0 , tostring(m_iniName), "Feed", "10") ) m_spindle = AddSelectControl("Spindle Dir", {"M03", "M04"}, nil) m_rpm = AddInputControl("Spindle Speed", nil) m_rpm:SetValue( mc.mcProfileGetString(0 , tostring(m_iniName), "RPM", "100") ) end function SaveSettings() mc.mcProfileWriteString(0 , tostring(m_iniName), "RPM", tostring(m_rpm:GetValue())) mc.mcProfileWriteString(0 , tostring(m_iniName), "ZHome", tostring(m_z_home:GetValue())) mc.mcProfileWriteString(0 , tostring(m_iniName), "Depth", tostring(m_z_depth:GetValue())) mc.mcProfileWriteString(0 , tostring(m_iniName), "RapidHeight", tostring(m_rapid_height:GetValue())) mc.mcProfileWriteString(0 , tostring(m_iniName), "RetractHeight", tostring(m_retract_height:GetValue())) mc.mcProfileWriteString(0 , tostring(m_iniName), "PeckDepth", tostring(m_peck_depth:GetValue())) mc.mcProfileWriteString(0 , tostring(m_iniName), "Feedt", tostring(m_feedtype:GetCurrentSelection())) mc.mcProfileWriteString(0 , tostring(m_iniName), "Feed", tostring(m_feedrate:GetValue())) mc.mcProfileWriteString(0 , tostring(m_iniName), "Cycle", tostring(m_cycletype:GetCurrentSelection())) end function GenGcode() local zhome = m_z_home:GetValue() local RapidHeight = m_rapid_height:GetValue() local retheight = m_retract_height:GetValue() local peck = m_peck_depth:GetValue() local feed = m_feedrate:GetValue() local depth = m_z_depth:GetValue() local RPM = m_rpm:GetValue() local gcode = string.format("G54 G17 G40 G80 G90 G94 ") local drilltype = m_cycletype:GetCurrentSelection() local feedtype = m_feedtype:GetCurrentSelection() local spin = m_spindle:GetCurrentSelection() if(feedtype == 0)then gcode = gcode .. string.format("G98\n", feedt ) elseif(feedtype == 1) then gcode = gcode .. string.format("G99\n", feedt ) end local gcode = gcode .. string.format("S%.d\n", RPM) if(spin == 0) then gcode = gcode .. "M03 (Spindle CW)\n" elseif(spin == 1) then gcode = gcode .. "M04 (Spindle CCW)\n" end gcode = gcode .. "G00 Z.1\nG00 X0.0\n" if(drilltype == 0)then gcode = gcode .. string.format("G81 Z%.4f R%.4f F%.4f\n", depth, retheight, feed ) elseif(drilltype == 1) then gcode = gcode .. string.format("G83 Z%.4f R%.4f Q%.4f F%.4f\n", depth, retheight, peck, feed ) end gcode = gcode .. "G00 Z.1\n" local gcode = gcode .. string.format ("G00 Z%.4f\n", zhome) gcode = gcode .. "G80\nM05\nM30\n" local file = wx.wxFileDialog(panel, "Select Gcode File", "", "", "Text files (*.txt)|*.txt|Tap files (*.tap)|*.tap", wx.wxFD_SAVE,wx.wxDefaultPosition,wx.wxDefaultSize, "File Dialog" ); if(file:ShowModal() == wx.wxID_OK)then local path = file:GetPath() --wx.wxMessageBox(tostring(path)) io.output(io.open(path,"w")) io.write(gcode) io.close() mc.mcCntlLoadGcodeFile( 0, tostring(path)) end SaveSettings() end function main() if(mcLuaPanelParent == nil)then -- create the wxFrame window mainframe = wx.wxFrame( wx.NULL, -- no parent wx.wxID_ANY, -- whatever for wxWindow ID "Lathe Drill Wizard", -- frame caption wx.wxDefaultPosition, -- place the frame in default position wx.wxDefaultSize, -- default frame size wx.wxDEFAULT_FRAME_STYLE ) -- use default frame styles -- create a panel in the frame panel = wx.wxPanel(mainframe, wx.wxID_ANY) -- create a simple file menu with an exit local fileMenu = wx.wxMenu() fileMenu:Append(wx.wxID_EXIT, "E&xit", "Quit the wizard") -- create a simple help menu local helpMenu = wx.wxMenu() helpMenu:Append(wx.wxID_ABOUT, "&About", "About Lathe Drill Wizard") -- create a menu bar and append the file and help menus local menuBar = wx.wxMenuBar() menuBar:Append(fileMenu, "&File") menuBar:Append(helpMenu, "&Help") -- attach the menu bar into the frame mainframe:SetMenuBar(menuBar) -- create a simple status bar mainframe:CreateStatusBar(1) mainframe:SetStatusText("No Error.") -- connect the selection event of the exit menu item to an -- event handler that closes the window mainframe:Connect(wx.wxID_EXIT, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) mainframe:Close(true) end ) -- connect the selection event of the about menu item mainframe:Connect(wx.wxID_ABOUT, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) wx.wxMessageBox("Lathe Drill Wizard \n\nAuthor: David Patton\nDate: 02/18/15", "About wxLua", wx.wxOK + wx.wxICON_INFORMATION, mainframe) end ) else panel = mcLuaPanelParent end --Set up the main sizer so we can start adding controls local mainSizer = wx.wxBoxSizer(wx.wxVERTICAL) local InputsGridSizer = wx.wxFlexGridSizer( 2, 4, 0, 0 ) InputsGridSizer:AddGrowableCol(1, 0) function AddInputControl(name_string, width) if(width == nil)then width = 100 end local textCtrlID = GetNextID() local staticText = wx.wxStaticText( panel, wx.wxID_ANY, name_string) local textCtrl = wx.wxTextCtrl( panel, textCtrlID, "0.000", wx.wxDefaultPosition, wx.wxSize(width, -1), wx.wxTE_PROCESS_ENTER ,wx.wxTextValidator(wx.wxFILTER_NUMERIC)) InputsGridSizer:Add( staticText, 0, wx.wxALIGN_CENTER_VERTICAL+wx.wxALL+wx.wxALIGN_RIGHT, 2) InputsGridSizer:Add( textCtrl, 0, wx.wxGROW+wx.wxALIGN_CENTER+wx.wxALL+wx.wxALIGN_LEFT, 2) return textCtrl, textCtrlID end function AddSelectControl(name_string, selections, selCtrlID) local selCtrlID = GetNextID() local staticText = wx.wxStaticText( panel, wx.wxID_ANY, name_string ) local selCtrl = wx.wxComboBox(panel, selCtrlID, "", wx.wxDefaultPosition, wx.wxSize(100, -1), selections) selCtrl:SetSelection(0) InputsGridSizer:Add( staticText, 0, wx.wxALIGN_CENTER_VERTICAL+wx.wxALL+wx.wxALIGN_RIGHT, 2) InputsGridSizer:Add( selCtrl, 0, wx.wxGROW+wx.wxALIGN_CENTER, 2) return selCtrl, selCtrlID end -- Add image to the top local hbmp = wx.wxBitmap(m_image) local TopImage = wx.wxStaticBitmap(panel, wx.wxID_ANY, hbmp ) --Setup the inputs Setupinputs() -- make the bottom buttons local buttonSizer = wx.wxBoxSizer( wx.wxHORIZONTAL ) local genGcode = wx.wxButton( panel, ID_GENGCODE_BUT, "&PostGcode") genGcode:SetBackgroundColour(wx.wxColour(0,255, 128)) buttonSizer:Add( genGcode, 0, wx.wxALIGN_CENTER+wx.wxALL, 2 ) if(mcLuaPanelParent == nil)then local closeButton = wx.wxButton( panel, ID_CLOSE_BUTTON, "E&xit") buttonSizer:Add( closeButton, 0, wx.wxALIGN_CENTER+wx.wxALL, 2 ) end --Set up the sizers mainSizer:Add( TopImage, 0, wx.wxALIGN_CENTER+wx.wxALL, 2 ) mainSizer:Add( InputsGridSizer, 0, wx.wxALIGN_CENTER+wx.wxALL, 2 ) mainSizer:Add( buttonSizer, 0, wx.wxALIGN_CENTER+wx.wxALL, 2 ) panel:SetSizer( mainSizer ) panel:Connect(ID_GENGCODE_BUT, wx.wxEVT_COMMAND_BUTTON_CLICKED, function(event) GenGcode() end) panel:Connect(ID_CYCLE_TYPE, wx.wxEVT_COMMAND_COMBOBOX_SELECTED, function(event) if(m_cycletype:GetCurrentSelection() == 0)then m_peck_depth:SetEditable(false) m_peck_depth:SetBackgroundColour(wx.wxColour("LIGHT GRAY")) else m_peck_depth:SetEditable(true) m_peck_depth:SetBackgroundColour(wx.wxColour(wx.wxNullColour)) end end) -- Connect a handler for pressing enter in the textctrls panel: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) -- show the frame window if(mcLuaPanelParent == nil)then panel:Connect(ID_CLOSE_BUTTON, wx.wxEVT_COMMAND_BUTTON_CLICKED, function(event) mainframe:Destroy() end) panel:Fit() mainframe:Fit() mainframe:Show(true) end end main() wx.wxGetApp():MainLoop()