This M6 function seems to be working fine. I found this code from this video:
https://www.youtube.com/watch?v=tCU1bzuDNvI. I simplified and removed some unnecessary (for me) code.
function m6()
local inst = mc.mcGetInstance() ;
local currentTool = mc.mcToolGetCurrent(inst)
local selectedTool = mc.mcToolGetSelected(inst)
local posmode = mc.mcCntlGetPoundVar(inst, mc.SV_MOD_GROUP_3) --get the current mode so we can return to it when macro ends
--Get positions before moving to do tool change
local valX, rc = mc.mcAxisGetMachinePos(inst, mc.X_AXIS) --Get the position of the X axis in Machine Position
local valY, rc = mc.mcAxisGetMachinePos(inst, mc.Y_AXIS) --Get the position of the Y axis in Machine Position
local valZ, rc = mc.mcAxisGetMachinePos(inst, mc.Z_AXIS) --Get the position of the Z axis in Machine Position
if selectedTool == currentTool then
return
mc.mcCntlSetLastError(inst, "Current tool == Selected tool so there is nothing to do")
else
mc.mcCntlSetLastError(inst, "Change to tool " .. tostring(selectedTool) .. " and press start to continue") --Message at beginning of tool change
mc.mcCntlToolChangeManual(inst, true) --This will pause the tool change here and wait for a press of cycle start to continue
mc.mcCntlSetLastError(inst, "Current tool == " .. tostring(selectedTool) .. " Previous Tool == " .. tostring(currentTool)) --Message that shows after Cycle Start
mc.mcToolSetCurrent(inst, selectedTool) --sets the current tool displayed in mach to the selected tool
local probedz = mc.mcCntlGetPoundVar(inst, 5073) --this retreives the Saved varible of the z position (#5073) in machine coordinates and assigns it to the name probedz
local ToolLength = math.abs(9.5238 + probedz) -- this calculates the value of the tool lenght by using a gage line off of my spindle to the tool setter. uses the absolute value fuction
mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT, selectedTool, ToolLength) --- this sets the tool length value into the tool table into the selected tool position number, Note - i havent reassigned current tool varible so i have to use the "selectedTool" name
mc.mcCntlGcodeExecute(inst, string.format('G ' .. posmode))--return to pre macro mode G90, or G91
end
end
if (mc.mcInEditor() == 1) then
m6()
enda