This is what I came up with. It is working in the editor. Well see in the morning if it will run on the machine. I would like to add a little more on the math end though dealing with negative numbers. Well see how it does on the machine and go from there. I changed a few things to make it easier to change variables if I need to as well. again, if anyone sees any issues, or has some advice on what I could do to improve it. Don't hesitate to post.
function m6()
local inst = mc.mcGetInstance()
local posmode = mc.mcCntlGetPoundVar(inst, mc.SV_MOD_GROUP_3) --get the current mode so we can return to it when macro ends
local selectedtool = 1 --mc.mcToolGetSelected(inst)
local currenttool = 2 --mc.mcToolGetCurrent(inst)
local xstart = mc.mcAxisGetPos(inst, 0) --X Axis--
local ystart = mc.mcAxisGetPos(inst, 1) --Y Axis--
local xchangepos = 12
local ychangepos = -46.0857
local xprobepos = .6831
local yprobepos = -46.0857
local reg = mc.mcRegGetHandle(inst, "iRegs0/TSoffset") --Get register handle
local regval = mc.mcRegGetValueString(reg) --Get the string
local tsoffset = tonumber(regval) --The value set in the Tool Offset Gage Block Height
if selectedtool == currenttool then
mc.mcCntlSetLastError(inst, "No Tool Changed Required")
else
mc.mcCntlGcodeExecuteWait(inst, "G53 G0 Z-0.2\nG53 G0 X"..xchangepos.." Y"..ychangepos.."") --Move the Z to Home.Then to the X, Y Coords for our tool change.
mc.mcCntlSetLastError(inst, "Current tool == " .. tostring(selectedtool) .. " Previous Tool == " .. tostring(currenttool)) --Specify which tools changed--
wx.wxMessageBox("Remove Dust Boot and Change Tool \nThen Press Ok")
mc.mcCntlGcodeExecuteWait(inst, "G53 G0 X"..xprobepos.."Y"..yprobepos.."") --Move to Tool Setter position--
wx.wxMessageBox("Click Ok to Begin Probing the New Tool","Click OK to continue")
mc.mcToolSetCurrent(inst, selectedtool) --change selected tool to current
mc.mcCntlSetLastError(inst, "Probing in Progress!")
mc.mcCntlGcodeExecuteWait(inst, " G91 G31 Z-5. F10.")--probe the new tool
local ProbedZ = mc.mcCntlGetPoundVar(inst, mc.SV_PROBE_POS_Z) -- Z Probe position in Machine coords
mc.mcCntlGcodeExecute(inst, string.format('G ' .. posmode))--return to pre macro mode G90, or G91
mc.mcCntlGcodeExecuteWait(inst, "G00 G53 Z-0.2")--Retract
local NewOffset = math.abs(ProbedZ - tsoffset)
mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT, selectedtool, NewOffset)
mc.mcCntlSetLastError(inst, string.format("Auto tool setting complete, Offset = %.4f", NewOffset))
mc.mcCntlGcodeExecuteWait(inst,"G53 G0 X"..xchangepos.."")
wx.wxMessageBox("Toolchange Complete.\nReplace dust boot")
mc.mcCntlGcodeExecuteWait(inst, "G90 G0 X"..xstart.."Y"..ystart)
end
end
if (mc.mcInEditor() == 1) then
m6()
end